在我的应用程序中,我试图使用ArrayList<HashMap<String, String>>
保存数据
以下是我的代码:
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);
下面是我的GridViewCoustoAdapter:
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;
}}
现在我想把材料ID发送到下一个活动。也就是说,我想让GridView的Matrial
例如:
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);
}
});
我该怎么做??
请帮助!!
最佳答案
在onItemClick
HashMap<String, String> map = (HashMap<String, String>)arg0.getItemAtPosition(position);
然后
String id = map.get("material_id");