我已经汇总了一些代码,如下所示,它从服务器上托管的JSON文件中获取数据并将其放入RecyclcerView中。这一直工作正常,但任何方式我尝试添加一个单击方法它不起作用。我希望这样做,如果我点击其中一行,它会告诉我它的国家。我不知道我哪里出错了,但我会在下面附上我的代码,获取JSON数据并将其放入RecyclerView。
由于
package com.example.curtisboylan.myapplication;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Objects;
public class MainRecycler extends AppCompatActivity {
private ArrayList countries;
private static String url = "http://curtisboylan.me/mygeek/mygeektest.php";
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
countries = new ArrayList<>();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_recycler);
initViews();
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
TextView txtView = (TextView) findViewById(R.id.textView5);
if (Objects.equals(message, "di1")){
txtView.setText("Diagnostics");
}
else if (Objects.equals(message, "screen1")){
txtView.setText("Screen Repair");
}
setTitle("Available Technicians");
}
private void initViews(){
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainRecycler.this);
pDialog.setMessage("Please Wait..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("MyGeek");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
//String email = c.getString("email");
//String address = c.getString("address");
//String gender = c.getString("gender");
// Phone node is JSON Object
// JSONObject phone = c.getJSONObject("phone");
// String mobile = phone.getString("mobile");
// String home = phone.getString("home");
// String office = phone.getString("office");
countries.add(c.getString("name"));
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.hide();
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
RecyclerView.Adapter adapter = new DataAdapter(countries);
recyclerView.setAdapter(adapter);
}
}
}
答案 0 :(得分:1)
不要在Recycler视图上添加单击侦听器,而是尝试在onBindViewHolder方法中的适配器类中添加单击侦听器。这就是你将如何获得执行点击的确切项目。
@Override
public void onBindViewHolder(Viewholder holder, final int position) {
YourClassObject item = YourClassObjectList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Recycle Click" + position, Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
此链接可以帮助您为recyclerview实现OnItemClick类似的侦听器。
答案 2 :(得分:0)
在Adapter类中创建一个接口
public interface EventsCatalogsAdapter{
void onClickImageCategory(Catalogs catalog);
}
创建EventsCatalogsAdapter的属性
private EventsCatalogsAdapter event;
创建构造函数
public CatalogsAdapter(EventsCatalogsAdapter event) {
this.event = event;
}
在 onBindViewHolder
中添加setOnClickListenerpublic void onBindViewHolder(final CatalogsViewHolder holder, final int position){
holder.imageCatalog.setOnClickListener(holder);
}
在你 RecyclerView.ViewHolder 中实现onClickListener事件。
class CatalogsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView imageCatalog;
private TextView tvCatalogName;
private View vLegendCatalog;
public CatalogsViewHolder(View itemView) {
super(itemView);
imageCatalog = (ImageView) itemView.findViewById(R.id.i =vCatalogItem);
tvCatalogName = (TextView)itemView.findViewById(R.id.tvCatalogName);
vLegendCatalog = itemView.findViewById(R.id.vLegendCatalog);
}
@Override
public void onClick(View v) {
event.onClickImageCategory(catalogsList.get(getAdapterPosition()));
}
}