Я разрабатываю приложение, в котором я использую RecyclerView
и CardView
. В этом случае, когда я нажимаю на элемент, я хочу, чтобы это значение элемента передавалось другому Activity
. Я хочу, если я нажму 1-й элемент, он должен передать значение 1-го элемента, и когда я нажму на 2-й элемент, он должен передать значение 2-го элемента. Но он передает только значение 1-го элемента, если я нажимаю на любой предмет.Как передать значение в recylerview
Вот мой код MainActivity
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_tab_1, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swifeRefresh);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
getData();
upcomingJobs.clear();
upcomingJobs.addAll(upcomingJobs);
// fire the event
adapter.notifyDataSetChanged();
}
});
// Make call to AsyncTask
getData();
return v;
}
private void getData() {
final boolean checkConnection=new Application_utility().checkConnection(getActivity());
if(!checkConnection) {
mSwipeRefreshLayout.setRefreshing(false);
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(
getContext()).create();
// Setting Dialog Title
alertDialog.setTitle("No Connection");
// Setting Dialog Message
alertDialog.setMessage("Please Check Your Internet Connection");
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
getActivity().moveTaskToBack(true);
}
});
// Showing Alert Message
alertDialog.show();
} else {
class GetData extends AsyncTask<Void, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(getContext(), "Fetching Data", "Please wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
parseJSON(s);
mSwipeRefreshLayout.setRefreshing(false);
}
@Override
protected String doInBackground(Void... params) {
BufferedReader bufferedReader = null;
try {
URL url = new URL(Config.GET_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
Toast.makeText(getContext(), "" + e, Toast.LENGTH_LONG).show();
return null;
}
}
}
GetData gd = new GetData();
gd.execute();
}
}
public void showData() {
adapter = new CardAdapter(Config.offer, Config.offprice, Config.bitmaps, Config.price, Config.urls);
recyclerView.setAdapter(adapter);
}
private void parseJSON(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray array = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
config = new Config(array.length());
for (int i = 0; i < array.length(); i++) {
JSONObject j = array.getJSONObject(i);
Config.offer[i] = getoffer(j);
Config.urls[i] = geturl(j);
Config.offprice[i] = getoffprice(j);
Config.price[i] = getprice(j);
Config.urls[i] = geturl(j);
final int finalI = i;
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent a=new Intent(view.getContext(),Details.class);
a.putExtra("Config.offer", Config.offer[finalI]);
startActivity(a);
}
}));
}
} catch (JSONException e) {
e.printStackTrace();
}
GetBitmap gb = new GetBitmap(getContext(), this, Config.urls);
gb.execute();
}
private String getoffer(JSONObject j) {
String name = null;
try {
name = j.getString(Config.TAG_Offer);
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
private String getoffprice(JSONObject j) {
String name = null;
try {
name = j.getString(Config.TAG_offprice);
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
private String getprice(JSONObject j) {
String name = null;
try {
name = j.getString(Config.TAG_price);
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
private String geturl(JSONObject j) {
String url = null;
try {
url = j.getString(Config.TAG_IMAGE_URL);
} catch (JSONException e) {
e.printStackTrace();
}
return url;
}
}
А вот код DetailActivity
:
public class Details extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
TextView t1 = (TextView) findViewById(R.id.textView2);
Intent intent = getIntent();
String Name = intent.getStringExtra("Config.offer");
t1.setText(Name);
}
}
И вот мой код класса адаптера:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyDataHolder> {
public List<ListItem> upcomingJobs;
public class MyDataHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public View view;
public TextView offer, offprice, price, url;
public MyDataHolder(View view){
super(view);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
offer = (TextView) itemView.findViewById(R.id.offer);
offprice = (TextView) itemView.findViewById(R.id.Offerprice);
price = (TextView) itemView.findViewById(R.id.price);
url = (TextView) itemView.findViewById(R.id.url);
price.setPaintFlags(price.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
}
}
public CardAdapter(String[] offer, String[] offprice, Bitmap[] image, String[] price, String[] url) {
super();
upcomingJobs = new ArrayList<ListItem>();
for (int i = 0; i < offer.length; i++) {
ListItem item = new ListItem();
item.setoffer(offer[i]);
item.seturl(url[i]);
item.setoffprice(offprice[i]);
item.setprice(price[i]);
item.setimage(image[i]);
upcomingJobs.add(item);
}
}
@Override
public MyDataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_list, parent, false);
return new MyDataHolder(itemView);
}
@Override
public void onBindViewHolder(MyDataHolder holder, int position) {
final ListItem uj = upcomingJobs.get(position);
holder.offer.setText(uj.getoffer());
holder.offprice.setText(uj.getoffprice());
holder.price.setText(uj.getprice());
holder.url.setText(uj.geturl());
holder.imageView.setImageBitmap(uj.getimage());
}
@Override
public int getItemCount() {
return upcomingJobs.size();
}
}
Где вы настройки адаптера на вашем recyclerview? – Bhargav
просто потому, что вы передаете только одно значение в 'Intent'. поэтому в итоге вы получаете только одно значение. – Ironman
Здесь я устанавливаю адаптер: public void showData() { adapter = new CardAdapter (Config.offer, Config.offprice, Config.bitmaps, Config.price, Config.urls); recyclerView.setAdapter (адаптер); @Bhargav – user