我有一个recyclelerview,其中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);
答案 0 :(得分:1)
在setOnClickListener
内使用onBindViewHolder
,请在下面修改您的代码并从setOnClickListener
RecyclerViewHolder
@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);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// below way you can pass any value
projectid = PROJECT_ID.get(i);
Intent in = new Intent(v.getContext(), HomeRecyclerDetailActivity.class);
in.putExtra("PID",projectid);
v.getContext().startActivity(in);
}
});
ImageLoader.getInstance().displayImage(projectimage, viewHolder.mImage, options);
}
}
答案 1 :(得分:1)
尝试成为OPP的家伙,它将为您节省时间和喧嚣。
在你的onBindViewHolder()
中使用项目的位置来获取ArrayList中的项目,
Item someItem = list.get(i);
获得该项目后,您可以从该项目中获取PROJECT_ID
someItem.getProjectID();
我假设您将为Item
创建一个模型类来表示adapter
您可以像这样填写您的列表
ArrayList<Item> items = new ArrayList<>();
for (int j = 0, count = data.length(); j < count; j++) {
try {
JSONObject json = data.getJSONObject(j);
Item item = new Item();
item.setProject_ID(""+json.getInt(TAG_ID));
item.setProject_ID(""+json.getString(TAG_PROJECT_TITLE));
item.setProject_ID(""+json.getString(TAG_PROJECT_CITY));
item.setProject_ID(""+json.getString(TAG_PROJECT_TYPE));
item.setProject_ID(""+json.getString(TAG_PROJECT_IMAGE));
item.setProject_ID(""+json.getString(TAG_PROJECT_STATUS));
items.add(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
类似这样的事情
public class Item {
private String project_ID;
private String project_Title;
private String project_City;
private String project_Type;
private String project_Image;
private String project_Status;
public String getProject_ID() {
return project_ID;
}
public void setProject_ID(String project_ID) {
this.project_ID = project_ID;
}
public String getProject_Title() {
return project_Title;
}
public void setProject_Title(String project_Title) {
this.project_Title = project_Title;
}
public String getProject_City() {
return project_City;
}
public void setProject_City(String project_City) {
this.project_City = project_City;
}
public String getProject_Type() {
return project_Type;
}
public void setProject_Type(String project_Type) {
this.project_Type = project_Type;
}
public String getProject_Image() {
return project_Image;
}
public void setProject_Image(String project_Image) {
this.project_Image = project_Image;
}
public String getProject_Status() {
return project_Status;
}
public void setProject_Status(String project_Status) {
this.project_Status = project_Status;
}
}