2

我从 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;
    }
}
4

4 回答 4

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);

                }
于 2013-01-30T06:53:49.140 回答
0

对不起,我用错了方法。请看下面的回答。只需更改此方法

public long getItemId(int position) 
{
   return items.get(position).getId();
}

现在可以在gridView的onItemClick中获取id的值了。

于 2013-01-30T06:09:20.387 回答
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);

                    }
             });
于 2013-01-30T07:03:44.800 回答
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);

            }
     });
于 2013-01-30T07:06:33.700 回答