看看我的自定義適配器,擴展BaseAdapter:通過onClickListener獲得通過自定義適配器的位置數據
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;
}
}
這是我的自定義適配器,我在我的抽屜式導航使用了這一點。我想通過點擊類別標題來獲得類別標識,並將其發送到片段並更改片段數據庫。 但它返回null爲cat_ID
現在我的問題: 1.爲什麼它返回null? 2.是否有正確的方式獲取發送到分段的類別ID?
CATEGO的結果是什麼RY_ID = dCategorise.get(「CATEGORY_ID」);? –
@Rod_Algonquin對不起,我對dCategorise感到困惑? – MAY3AM
結果爲空?對於CATEGORY_ID?嘗試log.d它 –