我正在开发一个我正在使用RecyclerView
和CardView
的应用。当我点击某个项目时,我希望该项目值传递给另一个Activity
。我想如果我点击第1项,它应该传递第1项的值,当我点击第2项时它应该传递第2项的值。但是如果我点击任何项目它只传递第1项值
以下是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();
}
}
答案 0 :(得分:1)
只需将其他信息放入Intent
传递给DetailsActivity
即可。
所以你的OnItemClickListener
应该是这样的。
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]);
a.putExtra("Config.urls", Config.urls[finalI]);
a.putExtra("Config.offprice", Config.offprice[finalI]);
a.putExtra("Config.price", Config.price[finalI]);
startActivity(a);
}
}));
从DetailsActivity
开始,只从您收到的意图中提取所有值。
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 Offer = intent.getStringExtra("Config.offer");
String Urls = intent.getStringExtra("Config.urls");
String OffPrice = intent.getStringExtra("Config.offprice");
String Price = intent.getStringExtra("Config.price");
// Now set your textviews here.
// ...
}
}
<强>更新强>
因此,如果我已正确理解您的问题,您必须在OnItemClickListener
循环之外取for
并将听众设置为此类。另一件事是,您需要使用position
作为Config.offer
的索引,如下所示:a.putExtra("Config.offer", Config.offer[position]);
所以这里有更新的代码
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);
}
// Move the click listener outside
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[position]);
startActivity(a);
}
}));