3
在我的應用我想保存使用下面ArrayList<HashMap<String, String>>
數據是我的代碼:如何獲取點擊gridview項目時自定義gridview適配器hashmap的數據?
JsonArrayRequest req = new JsonArrayRequest(Constants.DARSHAN_URL+Constants.MAIN_CATEGORY,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response.get(i);
String material_id = person.getString("material_id");
String material_desc = person.getString("material_desc");
HashMap<String, String> maim_category_array = new HashMap<>();
maim_category_array.put("material_id", material_id);
maim_category_array.put("material_desc", material_desc);
categoryName.add(maim_category_array);
}
adapter = new GridviewAdapter(MainCategoryActivity.this, categoryName);
grid.setAdapter(adapter);
而下面是我的GridViewCoustomAdapter:
public class GridviewAdapter extends BaseAdapter
{
private ArrayList<HashMap<String, String>> listCountry;
private Activity activity;
public GridviewAdapter(Activity activity, ArrayList<HashMap<String, String>> listCountry) {
super();
this.listCountry = listCountry;
this.activity = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public TextView txtViewTitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.button_layout, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.btn_select);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = new HashMap<String, String>();
map = listCountry.get(position);
view.txtViewTitle.setText(map.get("material_desc"));
return convertView;
}}
現在我想要得到的material_Id發送到下一個活動。即我想matrial_Id onItemClick GridView控件 的例如:
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(MainCategoryActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
Intent i = new Intent(MainCategoryActivity.this, ProductNameActivity.class);
i.putExtra("MATERIAL_ID", <material_id>);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
我怎樣才能做到這一點?
請幫忙!!
不工作。它拋出錯誤.. – Android
和錯誤是什麼? – Raghunandan
'預期表達'在HashMap – Android