在我的应用程序中,我试图解析json对象以在recyclerview中显示相关信息。我已经使用gson Library实现了对象的反序列化。现在我想设置点击每个项目以显示列表的详细信息。为此尝试将setOnItemClicklistener实现为recyclerView。但我不知道为什么我收到此消息错误:(102,29)错误:找不到符号方法setOnItemClickListener()。这是我的主要活动的完整代码
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.testgrid.MainActivity;
import com.testgrid.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MyColleaguesPage.this);
recyclerView.setLayoutManager(layoutManager);
new JSONTask().execute();
}
public class JSONTask extends AsyncTask<String, String, List<MyColleageModel>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<MyColleageModel> doInBackground(String... params) {
colleagueObject = new ArrayList<MyColleageModel>();
try {
JSONObject parentObject = new JSONObject(loadJSONFromAsset());
JSONArray parentArray = parentObject.getJSONArray("contacts");
Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
MyColleageModel model = gson.fromJson(finalObject.toString(), MyColleageModel.class);
colleagueObject.add(model);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final List<MyColleageModel> result) {
super.onPostExecute(result);
if (result != null) {
adapter = new MyColleaguesAdapter(colleagueObject, getApplicationContext());
recyclerView.setAdapter(adapter);
//Problem in this setOnItemClickListener
recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //problem in this line
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyColleageModel myColleageModel=result.get(position);
Intent intent = new Intent(MyColleaguesPage.this, DetailMyColleague.class);
intent.putExtra("contacts", new Gson().toJson(myColleageModel));
startActivity(intent);
}
});
} else {
Toast.makeText(getApplicationContext(), "Not able to fetch data from server", Toast.LENGTH_SHORT).show();
}
}
}
private String loadJSONFromAsset() {
String json = null;
try {
................
return json;
}
}
答案 0 :(得分:0)
您不应在recyclerView上使用setOnItemClickListener
。相反,请使用addOnItemTouchListener
。您应该创建一个实现RecyclerView.OnItemTouchListener
的新类。请参阅this以供参考。