[英]Issues in recyclerview with onclick event values passed from one activity to another activity based on url id
Actually my issue is when i am clicking on Recyclerview items it shows only that item data in next activity based upon using id.but there is no Onitemclick method in recyclerview and how to rectify this error any one can solve this error? 实际上我的问题是当我单击Recyclerview项目时,它仅基于使用id在下一个活动中显示项目数据。但是recyclerview中没有Onitemclick方法,如何纠正此错误,任何人都可以解决此错误?
MainActivity.java 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 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);
}
}
}
set click listener to your root view of item layout, I change your onBindViewHolder()
method 将Click侦听器设置为项目布局的根视图,然后更改
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);
}
});
}
declare ROOT_VIEW in your holder class, get tag_id by pos from your list or what u have 在您的holder类中声明ROOT_VIEW,通过pos从您的列表中获取tag_id或您拥有的内容
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
}
In the second activity get the category id from the intent and get the details from it. 在第二个活动中,从意图中获取类别ID,并从中获取详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.