我有一个recyclelerview,用于填充来自api的json数据。
以下是每个recylerview项project_id
,title
等中的项目,每个recyclerview都有复选框。
以下是在recyclerview中填充数据的代码。
for (int j = 0, count = data.length(); j < count; j++) {
String name = json.getString(TAG_PROJECT_ID);
PROJECT_ID.add(name);
Log.e("array id added",name);
String name1 = json.getString(TAG_PROJECT_TITLE);
PROJECT_TITLE.add(name1);
}
点击每个recyclerviewitem上的复选框时,我想获得相应项目的project_id
。
//click listener
mCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//GET THE PROJECT ID
}
)}
更新了实际代码 (为简单起见,我在这里只添加了相关代码) 我的Recyclerview适配器
public class FavouriteManager extends RecyclerView.Adapter<FavouriteManager.RecyclerViewHolder> {
ArrayList<String> PROJECT_ID;
ArrayList<String> PROJECT_TITLE;
@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) {
projecttitle = PROJECT_TITLE.get(i);
viewHolder.mProjectName.setText(projecttitle);
@Override
public int getItemCount() {
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
set = pref.getStringSet("FAV", null);
if (set != null) {
selected = new ArrayList<String>(set);
Log.e("Item Added", "");
} else {
selected = new ArrayList<String>();
}
Length = selected.size();
if (Length == 0) {
RelativeLayout IMG = (RelativeLayout) getActivity().findViewById(R.id.fav_img);
IMG.setVisibility(View.VISIBLE);
}
return Length;
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView mProjectName;
CheckBox mCheck;
RecyclerViewHolder(final View itemView) {
super(itemView);
mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
mCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int h = getAdapterPosition();
if (!offlinejson.equals("")) {
try {
JSONObject jsonObject = new JSONObject(offlinejson);
JSONArray data = jsonObject.getJSONArray(TAG_DATA);
JSONObject jsondata = data.getJSONObject(h);
Log.e("getCheckedPos(getID)", String.valueOf(jsondata));
check = PROJECT_ID.get(h);
Log.e("getCheckedPos(getID)", String.valueOf(jsondata));
if (selected.contains(check)) {
selected.remove(check);
mCheck.setBackgroundResource(R.drawable.ic_favorite_white1_24dp);
Snackbar snackbar = Snackbar.make(v, "Property Unfavorited", Snackbar.LENGTH_SHORT);
snackbar.show();
notifyItemRemoved(h);
Log.e("FOUND","found");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
try {
JSONObject jsonObject = new JSONObject(offlinejson);
JSONArray data = jsonObject.getJSONArray(TAG_DATA);
PROJECT_ID = new ArrayList<String>();
PROJECT_TITLE = new ArrayList<String>();
for (int j = 0, count = data.length(); j < count; j++) {
json = data.getJSONObject(j);
//JSONArray jsonArray = new JSONArray(json);
// Log.e("JSON", String.valueOf(json));
if (selected.contains(String.valueOf(json.get(TAG_PROJECT_ID)))) {
try {
String name = json.getString(TAG_PROJECT_ID);
PROJECT_ID.add(name);
Log.e("array id added",name);
String name1 = json.getString(TAG_PROJECT_TITLE);
PROJECT_TITLE.add(name1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
注意: 我不想使用getAdapterPosition()方法,因为它不适用于我的json结构。
答案 0 :(得分:1)
在不同类型的解决方案中,您还可以使用setTag()和getTag()。 将项目ID设置为复选框,并在需要时获取。
Eg: mCheck.setTag(); //(key,value) or (value).
要获取ID,请使用getTag()并使用setTag()设置ID。
http://developer.android.com/reference/android/widget/CheckBox.html