I am working on a demo app for learning retrofit,I have used retrofit in app and went step by step.can anyone help me? I have used @onclick annotation for making click event but it is never executing when i am clicking a button.So can anyone tell me what is the wrong from myside?
void getContacts() {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<ContactRespones> call = apiService.getContacts();
call.enqueue(new Callback<ContactRespones>() {
@Override
public void onResponse(Call<ContactRespones> call, Response<ContactRespones> response) {
System.out.print("RESPONES---------------->" + response.body().toString());
List<ContactRequest> movies = response.body().getContacts();
Log.d(TAG, "Number of movies received: " + movies.size());
// recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.list_item_movie, getApplicationContext()));
}
@Override
public void onFailure(Call<ContactRespones> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
**
Add annotationProcessor
to your gradle, and rebuild gradle and project:
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
In your casae it will be like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.jakewharton:butterknife:8.5.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.android.support:recyclerview-v7:23.3.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
}
your code should be like below.
private static final String TAG = MainActivity.class.getSimpleName();
@BindView(R.id.btn_1)
Button button;
@BindView(R.id.lv_contacts)
RecyclerView lv_contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn_1)
public void buttonclick() {
button.setText("Clicked!");
}
onCreate: Button btn = (Button) findViewById(R.id.btn_1); btn.setOnClickListener(this);
Button btn = (Button) findViewById(R.id.btn_1); btn.setOnClickListener(this);
get an OnClickListener: Override/implement Methods... then in the OnClick: if(view.getId()==btn_1){ do sth. }
if(view.getId()==btn_1){ do sth. }
You can try adding this in Button view
android:onClick="buttonclick"
so It will look like:
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:onClick="buttonclick"
android:text="Get Contacts"
android:textSize="20dp"
android:layout_alignParentTop="true" />
## In your java code use: ##
@InjectView(R.id.btn_1)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
@OnClick(R.id.btn_1)
void submitButton(View view) {
if (view.getId() == R.id.btn_1) {
Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
}
}
## in your activity_main.xml : ##
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btn_1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp"
android:layout_marginTop="132dp"
android:onClick="submitButton"/>
</RelativeLayout>
## in build.gradle file(app) ##
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.jakewharton:butterknife:6.1.0'
}
我发现旧版本的butterknif使用@Bind注释可以正常工作,我已在gradle中更改了butterknife的依赖版本,如下所示,并且它的工作非常好。
compile 'com.jakewharton:butterknife:7.0.1'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.