【问题标题】:Get custom itemsAdapter id using cursor android使用光标android获取自定义itemsAdapter id
【发布时间】:2012-04-07 10:36:18
【问题描述】:

我正在尝试使用带有 sqlite 的自定义 itemsAdapter 将点击的项目传递给另一个活动,即从 A 到 B。

我怎样才能实现以下目标?

1) 使用光标获取项目的点击位置 2)将点击的项目传递给另一个活动

我正在尝试与this 示例类似,但使用我自己的自定义适配器 到目前为止,我已经完成了以下操作。

活动 A:

public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this,F32Activity.class);
Cursor cursor = (Cursor) itemsAdapter.getItem(position);
intent.putExtra("PROPERTY_ID", cursor.getInt(cursor.getColumnIndex("_id")));

startActivity(intent);
return;
 }

活动 B:

      propertyId = getIntent().getIntExtra("PROPERTY_ID", 0);
    System.out.println(employeeId);
    SQLiteDatabase db = (new Helper(this)).getWritableDatabase();
    Cursor cursor = db
            .rawQuery("SELECT * from my_table WHERE pro._id = ?",
                    new String[] { "" + propertyId });

添加了适配器

private void getItemId(int position) {
    // TODO Auto-generated method stub

}

private void getDataAndPopulate() {
    // TODO Auto-generated method stub
    image = new ArrayList<byte[]>();       
    bedrooms= new ArrayList<String>();
    address= new ArrayList<String>();
    propType= new ArrayList<String>();

    Cursor cursor = getEvents(" gall,properties where  properties._id = gall._id  " );

    while (cursor.moveToNext()) {
        //String temp_id = cursor.getString(0);
        byte[] temp_image = cursor.getBlob(2);
        String temp_identifier = cursor.getString(1);
        String temp_price = cursor.getString(3);
        String temp_bedrooms = cursor.getString(4);
        String temp_address = cursor.getString(5);
        String temp_propType = cursor.getString(6);


        image.add(temp_image);
        //System.out.println(image);

        bedrooms.add(temp_bedrooms);
        address.add(temp_address);
        propType.add(temp_propType);


}
    String[] identifierArray = (String[]) bedrooms.toArray(new String[bedrooms
                                                                    .size()]);
   itemsAdapter = new ItemsAdapter(PropertyResult.this,
   cursor, R.layout.activity_f9, identifierArray);
   setListAdapter(itemsAdapter);

}

private class ItemsAdapter extends BaseAdapter {
    String[] items;

    public ItemsAdapter(Context context, Cursor cursor,int textViewResourceId,
            String[] items) {
        this.items = items;
    }

    public View getView(final int POSITION, View convertView,
            ViewGroup parent) {
        TextView desc;
        TextView cap;
        TextView ident;
        TextView pric;
        TextView bedroom;
        TextView addres;
        TextView propertytyp;
        View view = convertView;
        ImageView img;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.activity_f9, null);

        }
        img = (ImageView) view.findViewById(R.id.image);        

        bedroom = (TextView) view.findViewById(R.id.bedrooms);
        addres = (TextView) view.findViewById(R.id.address);
        propertytyp = (TextView) view.findViewById(R.id.propertytype);          


        bedroom.setText("£"+bedrooms.get(POSITION));
        addres.setText(address.get(POSITION));
        propertytyp.setText(propType.get(POSITION));

        img.setImageBitmap(BitmapFactory.decodeByteArray(
        image.get(POSITION), 0, image.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;
    }
}

请给我一个解决方案。

【问题讨论】:

  • @Adil 是的,我收到以下内容 04-07 10:43:13.348: E/AndroidRuntime(12696): java.lang.ClassCastException: java.lang.Integer 04-07 10:43 :13.348: E/AndroidRuntime(12696): at com.studentAccommodationFinder.PropertyResult.onListItemClick(PropertyResult.java:87) 04-07 10:43:13.348: E/AndroidRuntime(12696): at

标签: android database cursor cursor-position custom-adapter


【解决方案1】:

onListItemClick() 中,您已经拥有游标的行ID,是第4 个参数id(这只适用于游标适配器)。

首先还要创建 ArrayList 来保存 long 值(来自光标的行 ID):

ids = new ArrayList<Long>();
//...
while (cursor.moveToNext()) {
   String temp_id = cursor.getString(0);// if 0 is your column containing the id(check it)
   ids.add(temp_id);
   //...
}

然后在您的适配器中覆盖方法 getItemId 并根据提供的位置从 ids ArrayList 返回 long 值:

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

然后在您的onListItemClick() 中只需使用id 参数:

public void onListItemClick(ListView parent, View view, int position, long id) {
   Intent intent = new Intent(this,F32Activity.class);
   intent.putExtra("PROPERTY_ID", id);
   startActivity(intent);
}

然后在接收活动中:

propertyId = getIntent().getLongExtra("PROPERTY_ID", 0);

【讨论】:

  • 好的,如果这只适用于光标适配器,我如何在我自己的自定义项目适配器中使用它?,你对这个
  • @user1176963 你没有说你的自定义适配器扩展了什么,ArrayAdapterBaseAdapterSimpleCursorAdapter 等等?
  • 非常感谢您的帮助,您的解决方案将有效,但这就是我得到的 ArrayList 类型中的方法 add(Long) 不适用于参数(字符串)那是我尝试做 ids.add(temp_id);
  • @user1176963 抱歉,应该是long temp_id = cursor.getLong(0);
【解决方案2】:

你的 on Item 应该是这样的

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {

    String strEventName, strEventDate;
    int Id;
    Cursor c = (Cursor) arg0.getAdapter().getItem(position);
    intent.putExtra("PROPERTY_ID", cursor.getInt(cursor.getColumnIndex("_id")));
    startActivity(intent);}

【讨论】:

  • 这不会给我一个错误但是永远不会被点击!,ONItemClicked 不起作用。
  • 检查您是否已注册监听器并检查您的视图是否有按钮而不是使其可聚焦为 false
猜你喜欢
  • 2016-03-03
  • 1970-01-01
  • 2016-05-03
  • 2011-02-17
  • 1970-01-01
  • 2014-09-29
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多