处理sqlite的结果并将它们传递给自定义列表适配器

时间:2012-01-24 15:59:25

标签: android sqlite custom-adapter

我使用sqlite,我从两列(名称,图标)获取结果。我想将它们传递给自定义适配器,以便将名称设置为(TextView)的文本,然后将该图标设置为带有 setCompoundDrawablesWithIntrinsicBounds 的drawableLeft。

首先我在db中的插入是这样的

  "Fruit / Vegetables,R.drawable.ic_launcher",
   "Meat / Fisth,R.drawable.ic_launcher"

在我的listActivity中,我打开数据库等后有了这段代码。

 mShopCatCursor = mDbHelper.fetchShoppingCategories();
 startManagingCursor(mShopCatCursor);

  String[] names,icons;
while(mShopCatCursor.moveToNext()){
    for(int i=-1,l=mShopCatCursor.getColumnCount(); ++i<l;){
        names[i]= mShopCatCursor.getString(0);
        icons[i]= mShopCatCursor.getString(1);
    }
}

    //this was taken from a tut 
    String[] from = new String[]{AppSQLite.KEY_NAME,AppSQLite.KEY_ICON};

    TextViewWithDrawableListAdapter adap = 
        new TextViewWithDrawableListAdapter(this, from);
    setListAdapter(adap);

我想要的是传递名称和图标,以便自定义适配器执行以下操作

  public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.main_list_item, null);
    }

    TextView textView = (TextView) v.findViewById(R.id.main_menu_list_item);

我想要这个!! 为每个列表项采取正确的值。

            textView.setText(names[0]); 
    textView.setCompoundDrawablesWithIntrinsicBounds(icons[0], 0, 0, 0);

    return v;
}

我知道namew [0]和图标不正确但我真的找不到方法,即使是在网上搜索了几个小时。

1 个答案:

答案 0 :(得分:0)

所有我都不明白你想要什么。如果要将名称和图标数组传递给自定义适配器类,则可以在自定义适配器类中使用构造函数来接收它们。或者阅读this tutorial