質問

I've been searching on this for a while now, but I can't find anything that could help me solve my problem.

I have a list of categories in a listview. I fetch these from a SQLiteDatabase and use a SimpleCursorAdapter to put them in the list. This works fine...

Now, if I click a category, I want to launch a new activity displaying all items with that specific category. Passing parameters to the new activity isn't a problem - found a nice tutorial on how to do this here: Passing data or parameter to another activity

My problem is that I can't get the id out of the selected view... I want the id from the selected category so I can get the items with that categoryId. This is where I try and get the id out of the view - I've used a lot of different methods (including some fiddling with listview, item and position, ... this is my most recent attempt) and don't know what to try next...

    @Override
    public void onItemClick(AdapterView<?> listview, View item, int position,
            long itemId) {
        Intent intent = new Intent();
        Bundle bun = new Bundle();

        bun.putInt("categoryId", (int) itemId);

        intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
        intent.putExtras(bun);          
        startActivity(intent);
    }

Has anybody encountered the same problem and if you have, do you have some advice for me on how to do this?

This is how the problem got solved:

        Cursor c = (Cursor)listview.getAdapter().getItem(position);
        int id = c.getInt(c.getColumnIndex(_ID)); //0 = index id
        //Log.d("Category id", id + "");

        Intent intent = new Intent();
        Bundle bun = new Bundle();

        bun.putInt("categoryId", id);

        intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
        intent.putExtras(bun);          
        startActivity(intent);
役に立ちましたか?

解決

Try following

@Override
public void onItemClick(AdapterView<?> listview, View item, int position,
        long itemId) {

    Cursor c = (Cursor)adatper.getItem(position); //adapter = cursor adapter object
    int id = c.getInt(0)// 0 is index of id.
    Intent intent = new Intent();
    Bundle bun = new Bundle();

    bun.putInt("categoryId", id);

    intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
    intent.putExtras(bun);          
    startActivity(intent);
}

他のヒント

For this,you have can use custom adapter and there,you can set ids in a HashMap.Here is an example:

    public class DemoAdapter extends SimpleCursorAdapter
    {
        Cursor dataCursor;
        LayoutInflater mInflater;
        Context context;

        public static HashMap<Integer,String> myList=new HashMap<Integer,String>();    

        public DemoAdapter(Context context, int layout, Cursor dataCursor, String[] from,int[] to)
        {
            super(context, layout, dataCursor, from, to);
            this.context=context;
            this.dataCursor = dataCursor;
            mInflater = LayoutInflater.from(context);
            this.fromWhere=fromWhere;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {

            final ViewHolder holder;

            if(convertView==null)
            {
                     convertView = mInflater.inflate(R.layout.my_list_item, null);
                     holder = new ViewHolder();

                     holder.cName=(TextView)convertView.findViewById(R.id.contactName);        
            convertView.setTag(holder);
            }
            else
                    holder=(ViewHolder)convertView.getTag();

            dataCursor.moveToPosition(position);

            String id=Integer.toString(dataCursor.getInt(dataCursor.getColumnIndexOrThrow("_id")));
            myList.put(position, id);

            holder.cName.setText(dataCursor.getString("contact_name"));

            return convertView;
       }

       static class ViewHolder
       {
          TextView cName;          
       }
  }  

Now,use it like-

listView.setOnItemClickListener(new OnItemClickListener{

                  @Override
              public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {

                         int id=Integer.parseInteger(DemoAdapter.myList.get(position));     
            }
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top