Reputation: 1212
look to my custom adapter that extends BaseAdapter:
public class DrawerAdapter extends BaseAdapter{
static class ViewHolder{
TextView cat_Title;
TextView cat_Count;
}
private Activity mActivity;
private ArrayList<HashMap<String, String>> mData;
private static LayoutInflater mInflater;
private JsonArray Categories = null;
public JsonObject Cat_Items = null;
private static String CATEGORY_NAME;
private static String CATEGORY_CNT;
private static String CATEGORY_ID;
public DrawerAdapter(Activity a, ArrayList<HashMap<String, String>> d){
mActivity = a;
mData = d;
mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View dView = convertView;
ViewHolder holder = null;
HashMap<String, String> dCategorise = new HashMap<String, String>();
dCategorise = mData.get(position);
CATEGORY_NAME = dCategorise.get("CATEGORY_NAME");
CATEGORY_CNT = dCategorise.get("CATEGORY_CNT");
CATEGORY_ID = dCategorise.get("CATEGORY_ID");
if (convertView == null){
dView = mInflater.inflate(R.layout.layout_drawer_list_row, null);
holder = new ViewHolder();
holder.cat_Title = (TextView) dView.findViewById(R.id.category_title);
holder.cat_Count = (TextView) dView.findViewById(R.id.category_counter);
dView.setTag(holder);
}else{
holder = (ViewHolder) dView.getTag();
}
holder.cat_Title.setTag(position);
holder.cat_Title.setTypeface(MyriadRegular);
holder.cat_Title.setText(CATEGORY_NAME);
holder.cat_Count.setTypeface(Yagut);
holder.cat_Count.setText(CATEGORY_CNT);
holder.cat_Title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
String cat_ID = mData.get(pos).get(CATEGORY_ID);
}
});
return dView;
}
}
This is my custom adapter and I'm using of this in my navigation drawer. I want to get category id by clicking on category title and send that to the fragment and changes fragment data base on this. But it's return null for cat_ID
Now my questions: 1.Why it return null? 2.Is it right way to getting category id for sending to fragment?
Upvotes: 2
Views: 2058
Reputation: 26198
I would recommend directly add the CATEGORY_ID
to the button's tag by the time you got it from your HashMap
sample:
change this:
holder.cat_Title.setTag(position);
to:
holder.cat_Title.setTag(CATEGORY_ID);
public void onClick(View v) {
String cat_ID = (String) v.getTag();
}
Upvotes: 4
Reputation: 1210
int pos = (Integer) v.getTag();
Are you sure this is right? v.getTag()
will get a ViewHolder
, you shouldn't convert it into a integer.
Upvotes: 1