实际上我的问题是当我点击Recyclerview项目时,它只显示基于使用id的下一个活动中的项目数据。但是在recyclerview中没有Onitemclick方法以及如何纠正此错误任何人都可以解决此错误?< / p>
MainActivity.java
public class MainActivity extends Activity {
RecyclerView recyclerView;
List<Your> yourss;
String Tag_id="category_id";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycle);
LinearLayoutManager ll = new LinearLayoutManager(MainActivity.this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(ll);
new JsonTask().execute();
}
public class JsonTask extends AsyncTask<String, String, List<Your>> {
@Override
protected List<Your> doInBackground(String... params) {
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL("http://yoursubshop.com/webservices/categories.php");
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String finaljson = sb.toString();
JSONObject firstobject = new JSONObject(finaljson);
JSONArray firstarray = firstobject.getJSONArray("data");
List<Your> yo = new ArrayList<>();
for (int i = 0; i < firstarray.length(); i++) {
JSONObject finalobject = firstarray.getJSONObject(i);
Your yos = new Your();
yos.category_id = finalobject.getString("Tag_id");
yos.category_name = finalobject.getString("category_name");
yo.add(yos);
}
return yo;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<Your> result) {
super.onPostExecute(result);
Rvadapter listAdapter = new Rvadapter(MainActivity.this, result);
recyclerView.setAdapter(listAdapter);
recyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,SeconActivity.class);
i.putExtra("data",Tag_id);
startActivity(i);
}
});
}
}
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
List<Your> yours = Collections.emptyList();
Context c;
public class Myadapter extends RecyclerView.ViewHolder {
TextView id;
TextView name;
CardView cv;
public Myadapter(final View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.texts);
name = (TextView) itemView.findViewById(R.id.textsa);
cv = (CardView)itemView.findViewById(R.id.card);
}
}
public Rvadapter(Context c, List<Your> yours) {
this.yours = yours;
this.c = c;
}
@Override
public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lists, parent, false);
Myadapter m = new Myadapter(v);
return m;
}
@Override
public void onBindViewHolder(Myadapter holder, int position) {
Your y = yours.get(position);
holder.id.setText(y.category_id);
holder.name.setText(y.category_name);
}
@Override
public int getItemCount() {
return yours == null ? 0 : yours.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
}
SecondActivity.java
public class SeconActivity extends Activity
{
RecyclerView recyclerView;
List<Shop> shops;
String serverUrl = "http://yoursubshop.com/webservices/categories-shop.php?category=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seocnd_main);
recyclerView = (RecyclerView) findViewById(R.id.secondmain);
LinearLayoutManager ll = new LinearLayoutManager(SeconActivity.this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(ll);
Intent in = getIntent();
String serverUrlId = in.getStringExtra("data");
serverUrl = serverUrl.concat(serverUrlId);
new JsonTask().execute();
}
public class JsonTask extends AsyncTask<String, String, List<Shop>> {
@Override
protected List<Shop> doInBackground(String... params) {
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(serverUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String finaljson = sb.toString();
JSONObject firstobject = new JSONObject(finaljson);
JSONArray firstarray = firstobject.getJSONArray("data");
List<Shop> yo = new ArrayList<>();
for (int i = 0; i < firstarray.length(); i++) {
JSONObject finalobject = firstarray.getJSONObject(i);
Shop yos = new Shop();
yos.shop_id = finalobject.getString("shop_id");
yos.shop_name= finalobject.getString("shop_name");
yo.add(yos);
}
return yo;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<Shop> result) {
super.onPostExecute(result);
Rvadapter listAdapter = new Rvadapter(SeconActivity.this, result);
recyclerView.setAdapter(listAdapter);
}
}
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
List<Shop> yours = Collections.emptyList();
Context c;
public class Myadapter extends RecyclerView.ViewHolder {
TextView id;
TextView name;
CardView cv;
public Myadapter(final View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.shopt);
name = (TextView) itemView.findViewById(R.id.shopr);
cv = (CardView)itemView.findViewById(R.id.card);
}
}
public Rvadapter(Context c, List<Shop> yours) {
this.yours = yours;
this.c = c;
}
@Override
public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopss, parent, false);
Myadapter m = new Myadapter(v);
return m;
}
@Override
public void onBindViewHolder(Myadapter holder, int position) {
Shop y = yours.get(position);
holder.id.setText(y.shop_id);
holder.name.setText(y.shop_name);
}
@Override
public int getItemCount() {
return yours == null ? 0 : yours.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
}
答案 0 :(得分:0)
将点击监听器设置为项目布局的根视图,我更改了您的onBindViewHolder()
方法
@Override
public void onBindViewHolder(Myadapter holder, int position) {
Shop y = yours.get(position);
holder.id.setText(y.shop_id);
holder.name.setText(y.shop_name);
holder.ROOT_VIEW.setTag(position); // here ROOT_VIEW is your inflated layout root
holder.ROOT_VIEW.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (int) v.getTag(); // use this pos to get this perticular view related data
Intent i = new Intent(MainActivity.this,SeconActivity.class);
i.putExtra("data",Tag_id);
startActivity(i);
}
});
}
在你的持有人类中声明ROOT_VIEW,从列表中获取tag_id或你有什么
答案 1 :(得分:0)
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> implements View.OnClicklistener{
List<Your> yours = Collections.emptyList();
Context c;
public class My
private int CURRENT_POSITION = 0;
public class Myadapter extends RecyclerView.ViewHolder {
TextView id;
TextView name;
CardView cv;
public Myadapter(final View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.texts);
name = (TextView) itemView.findViewById(R.id.textsa);
cv = (CardView)itemView.findViewById(R.id.card);
cv.setOnClicklistener(this);
}
}
...
@Override
public void onBindViewHolder(Myadapter holder, int position) {
CURRENT_POSITION = position;
...
//your code
}
@Override
public void onClick(View view){
Your y = yours.get(CURRENT_POSITION);
if(y != null){}
Intent i = new Intent(this, ,SeconActivity.class);
i.putExtra("id", y.category_id);
startActivity(i);
}
}
...
//your code
}
在第二个活动中,从意图中获取类别ID并从中获取详细信息。