我从webservice获取数据并将它们存储在sqlite中并将它们显示在网格中。数据包含项目ID,项目名称,项目图像现在我想从sqlite获取项目的id当我点击网格中的任何项目以及项目位置!!我怎么能这样做?
我的代码是::
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
System.err.println("cache_id=="+id);
System.err.println("position=="+position);
Intent i = new Intent(getApplicationContext(), BookContent.class);
i.putExtra("id", position);
System.err.println("this is item id::"+v.getId());
startActivity(i);
}
});
我的imageAdapter类是::
private class ItemsAdapter extends BaseAdapter implements ListAdapter
{
String[] items;
public ItemsAdapter(Context context,int textViewResourceId,String[] items)
{
this.items = items;
}
public View getView( int POSITION, View convertView,ViewGroup parent)
{
TextView cap = null ;
View view = convertView;
ImageView img = null;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.grid, null);
}
img = (ImageView) view.findViewById(R.id.image);
cap = (TextView) view.findViewById(R.id.caption);
String bookTitle = mapNames.get(POSITION);
cap.setText(mapNames.get(POSITION));
img.setImageBitmap(BitmapFactory.decodeByteArray(map.get(POSITION), 0, map.get(POSITION).length));
return view;
}
public int getCount()
{
return items.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
}
答案 0 :(得分:1)
单击网格项时,您不需要再次从sqlite数据库中携带itemid。您可以为view.setTag(String.valueOf(itemid));
方法中的每个网格视图元素设置类似getView()
的内容。
在onItemClick()
中,获取标记。
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
......................
.........................
String itemid = (String) v.getTag();
..............................
i.putExtra("itemid", itemid);
}
答案 1 :(得分:0)
抱歉,我使用了错误的方法。请看下面的答案。 只需更改此方法
public long getItemId(int position)
{
return items.get(position).getId();
}
现在,您可以在gridView的项目点击中获取id的值。
答案 2 :(得分:0)
String items[] items; //your array...
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
//System.err.println("cache_id=="+id);
//System.err.println("position=="+position);
String name =item.get(position).toString();
Intent i = new Intent(getApplicationContext(), BookContent.class);
i.putExtra("id", position);
System.err.println("this is item id::"+v.getId());
startActivity(i);
}
});
答案 3 :(得分:0)
为什么不将ID保存在数组中
String[] items;
int[] ids;
public ItemsAdapter(Context context,int textViewResourceId,String[] items,int[] ids)
{
this.items = items;
this.ids=ids;
}
然后在你的oncliklistener
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
System.err.println("cache_id=="+id);
System.err.println("position=="+position);
Intent i = new Intent(getApplicationContext(), BookContent.class);
//in this part
i.putExtra("itemid", itemid);
i.putExtra("id", this.ids[position]);
startActivity(i);
}
});