将onClick添加到RecyclerView Android
我已经整理了一些代码,如下所示,这些代码从服务器上托管的JSON文件中获取数据,并将其放入RecyclcerView。这一直工作得很好,但我尝试添加单击方法的任何方式都不起作用。我希望这样做,如果我点击其中一行,它会告诉我它是哪个国家。我不知道哪里出了问题,但我会在下面附加代码,获取JSON数据并将其放入RecyclerView 谢谢将onClick添加到RecyclerView Android,android,json,android-recyclerview,Android,Json,Android Recyclerview,我已经整理了一些代码,如下所示,这些代码从服务器上托管的JSON文件中获取数据,并将其放入RecyclcerView。这一直工作得很好,但我尝试添加单击方法的任何方式都不起作用。我希望这样做,如果我点击其中一行,它会告诉我它是哪个国家。我不知道哪里出了问题,但我会在下面附加代码,获取JSON数据并将其放入RecyclerView 谢谢 package com.example.curtisboylan.myapplication; import android.app.ProgressDialo
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);
}
}
}
package com.example.curtisboylan.myapplication;
导入android.app.ProgressDialog;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListAdapter;
导入android.widget.simpledapter;
导入android.widget.Toast;
导入android.widget.TextView;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.ArrayList;
导入java.util.Objects;
公共类MainRecycler扩展AppCompative活动{
私营ArrayList国家;
专用静态字符串url=”http://curtisboylan.me/mygeek/mygeektest.php";
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
国家/地区=新阵列列表();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u main\u recycler);
initViews();
Bundle Bundle=getIntent().getExtras();
字符串消息=bundle.getString(“消息”);
TextView txtView=(TextView)findViewById(R.id.textView5);
if(Objects.equals(消息“di1”)){
setxt(“诊断”);
}
else if(Objects.equals(消息,“screen1”)){
setText(“屏幕修复”);
}
setTitle(“可用技术人员”);
}
私有void initViews(){
RecyclerView RecyclerView=(RecyclerView)findViewById(R.id.card\u recycler\u视图);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
新建GetContacts().execute();
}
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainRecycler.this);
pDialog.setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“MyGeek”);
//通过所有触点循环
对于(int i=0;i
在适配器类中创建接口
public interface EventsCatalogsAdapter{
void onClickImageCategory(Catalogs catalog);
}
创建EventsCatalogsAdapter的atribute
private EventsCatalogsAdapter event;
创建构造函数
public CatalogsAdapter(EventsCatalogsAdapter event) {
this.event = event;
}
在BindViewHolder中添加setOnClickListener
public void onBindViewHolder(final CatalogsViewHolder holder, final int position){
holder.imageCatalog.setOnClickListener(holder);
}
在你中回收
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()));
}
}
@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();
}
});