【发布时间】:2016-05-05 08:48:40
【问题描述】:
我有一个 recyclerview,onClicked 应该返回其当前的PROJECT_ID 并将其传递给另一个活动,但是现在我得到一个随机的 PROJECT_ID。
(请记住我没有告诉getAdapterPosition(),我需要来自我的ArrayList 的当前项目PROJECT_ID)
int i;
RecyclerViewHolder(final View itemView) {
super(itemView);
mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
mProjectCity = (TextView) itemView.findViewById(R.id.PROJECT_city);
mProjectType = (TextView) itemView.findViewById(R.id.PROJECT_type);
mImage = (ImageView) itemView.findViewById(R.id.PROJECT_image);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
mProjectStatus = (RelativeLayout) itemView.findViewById(R.id.label);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
projectid = PROJECT_ID.get(i);
Intent in = new Intent(v.getContext(), HomeRecyclerDetailActivity.class);
in.putExtra("PID",projectid);
v.getContext().startActivity(in);
}
});
}
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new RecyclerViewHolder(v1);
}
@Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
String offlinejson = pref.getString("OFFLINEJSON", "");
if (!offlinejson.equals("")) {
try {
JSONObject jsonObject = new JSONObject(offlinejson);
JSONArray data = jsonObject.getJSONArray(TAG_DATA);
Length = data.length();
PROJECT_ID = new ArrayList<Integer>();
PROJECT_TITLE = new ArrayList<String>();
PROJECT_CITY = new ArrayList<String>();
PROJECT_TYPE = new ArrayList<String>();
PROJECT_IMAGE = new ArrayList<String>();
PROJECT_STATUS = new ArrayList<String>();
for (int j = 0, count = data.length(); j < count; j++) {
try {
JSONObject json = data.getJSONObject(j);
int name = json.getInt(TAG_ID);
PROJECT_ID.add(name);
String name1 = json.getString(TAG_PROJECT_TITLE);
PROJECT_TITLE.add(name1);
String name2 = json.getString(TAG_PROJECT_CITY);
PROJECT_CITY.add(name2);
String name3 = json.getString(TAG_PROJECT_TYPE);
PROJECT_TYPE.add(name3);
String name4 = json.getString(TAG_PROJECT_IMAGE);
PROJECT_IMAGE.add(name4);
String name5 = json.getString(TAG_PROJECT_STATUS);
PROJECT_STATUS.add(name5);
Log.e("JSON OFFLINE NAME", String.valueOf(PROJECT_ID));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
projecttitle = PROJECT_TITLE.get(i);
projectcity = PROJECT_CITY.get(i);
projecttype = PROJECT_TYPE.get(i);
projectimage = PROJECT_IMAGE.get(i);
projectstatus = PROJECT_STATUS.get(i);
// Set the values
viewHolder.mProjectName.setText(projecttitle);
viewHolder.mProjectCity.setText(projectcity);
viewHolder.mProjectType.setText(projecttype);
ImageLoader.getInstance().displayImage(projectimage, viewHolder.mImage, options);
【问题讨论】:
-
从哪里得到
iinRecyclerViewHolder? -
PROJECT_ID 是你的对象,我是你的位置吗?
-
这里需要使用getPosition()而不是i。
-
更改 projectid = PROJECT_ID.get(getAdapterPosition()) 它将为您获取 arraylist 中当前项目的 PROJECT_ID 或在 onBindViewHolder() 中使用 OnClickListener
标签: android arraylist android-recyclerview