0

我通常使用此代码将项目从数据库添加到列表视图:

public class ViewEvents extends Activity {

    DBAdapter DB=new DBAdapter(this);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewevents);

        final ListView myListView = (ListView)findViewById(R.id.MyList);

          final ArrayList<String> todoItems = new ArrayList<String>();
          final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems);

           DB.open();
         Cursor c=DB.select();
          c.moveToFirst();
          Integer n=new Integer(c.getCount());

        for(int i=0;i<c.getCount();i++)
        {
             todoItems.add(0, c.getString(0));
             c.moveToNext();
        }

            aa.notifyDataSetChanged();
            myListView.setAdapter(aa);
enter code here

但是,在我的项目中,我将图像添加到列表视图,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >
 <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    <ImageView
        android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:drawablePadding="0dip"
        android:src="@drawable/icon_remove" >
    </ImageView>

    <TextView
        android:id="@+id/label"
       android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/logo"
        android:text="@+id/label"
        android:textSize="25px" >
    </TextView>
</RelativeLayout>
</LinearLayout>

这会导致 arrayadapter 出现问题,我无法像往常一样捕获 listview:

ListView myListView = (ListView)findViewById(R.id.MyList);

请有任何建议

新代码:

public class RemoveEvent extends ListActivity {

    DBAdapter DB=new DBAdapter(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        final ArrayList<String> todoItems = new ArrayList<String>();
         final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems);


         DB.open();
         Cursor c=DB.select();
          c.moveToFirst();
          Integer n=new Integer(c.getCount());
        for(int i=0;i<c.getCount();i++)
        {
             todoItems.add(0, c.getString(0));
             c.moveToNext();
        }


           // todoItems.add(0, c.getString(0));
            aa.notifyDataSetChanged();
            setListAdapter(new RemoveArrayAdapter(this, .......));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}

删除数组适配器代码:

public class RemoveArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public RemoveArrayAdapter(Context context, String[] values) {
        super(context, R.layout.removeevent, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.removeevent, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);

        return rowView;
    }
}
4

3 回答 3

0

这会导致 arrayadapter 出现问题,我无法像往常一样捕获 listview:

这究竟意味着什么?你得到什么例外(如果有的话)?

一些问题:

如果您使用android.R.layout.simple_list_item_1,那么您发布的 xml 布局是什么?如果您打算使用该布局文件,请使用以下构造函数ArrayAdapter

final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this, R.layout.Name_of_layout_you_posted, R.id.label, todoItems);

此外,如果您打算通过Context

 DBAdapter DB=new DBAdapter(this);

然后你应该在方法中初始化数据库onCreate

DBAdapter DB;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewevents);
    DB = new DBAdapter(this);
    //... rest of the code

此外,您应该尝试使用Cursor基于适配器的适配器,这样您就不必解析列表中的光标。

编辑: 修改您RemoveArrayAdapter以接受您使用以下数据填充的 ArrayList Cursor

    public class RemoveArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<String> values;

        public RemoveArrayAdapter(Context context, ArrayList<String> values) {
            super(context, R.layout.removeevent, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.removeevent, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.label);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
            textView.setText(values.get(position));
            // I'm guessing you want to modify the Logo?!? if yes pass another ArrayList to this adapter
           //contaning the info to set the ImageView  
            return rowView;
        }

}

并且在RemoveActivity

for(int i=0;i<c.getCount();i++) {
    todoItems.add(c.getString(0));
    c.moveToNext();
}
setListAdapter(new RemoveArrayAdapter(this, todoItems));
于 2012-05-16T20:20:29.413 回答
0

你可以做到这一点更容易

ListView lv = (ListView)d.findViewById(R.id.dialog_list);
            Cursor c = getContentResolver().query(BowlersDB.CONTENT_URI,new String[] {BowlersDB.ID,BowlersDB.NAME},null,null,BowlersDB.NAME + " COLLATE LOCALIZED ASC");
            if(c.moveToFirst() && c != null){
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.names_listview,c
                        ,new String[] {BowlersDB.NAME},new int[] {R.id.bListTextView});
                lv.setAdapter(adapter);

基本上只需将光标传递给简单的光标适配器,它将从您的布局中填充列表,当您已经拥有信息时无需制作列表适配器

编辑:自定义适配器示例

public class ImageAdapter extends SimpleCursorAdapter {
Context context;

public ImageAdapter(Context context,int textViewResourceId,Cursor c,String[] from,int[] to){
    super(context,textViewResourceId,c,from,to);

    this.context = context;
}

@Override
public void setViewText(TextView tv,String text){
    Cursor c = context.getContentResolver().query(BowlersDB.CONTENT_URI,new String[] {BowlersDB.ID,BowlersDB.NAME}
    ,BowlersDB.ID+"="+text,null,null);
    if(c.moveToFirst() && c != null){
        tv.setText(c.getString(1));
    }
    c.close();
}
@Override
public void setViewImage(ImageView iv,String text){
    if(text.equals("1")){
        iv.setImageResource(R.drawable.rate_star_small_on_holo_light);
    }
}

}

这 2 部分代码来自我程序的两个不同部分,所以请不要认为它们放在一起

于 2012-05-16T20:08:32.220 回答
0

你可以使用我的代码

        Cursor cursor = myDBClass.returnResult();

    ArrayList<String> result = new ArrayList();
     for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext())
      {
       result.add( cursor.getString(cursor.getColumnIndex(myDBClass.BI_Attr2)));
      }
    String[] Values= new String[result.size()];

    for(int i=0;i<result.size();i++)
    {
        Values[i]=result.get(i);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice, android.R.id.text1, Values);
    // set this adapter as your ListActivity's adapter
    ListView LV=(ListView)findViewById(R.id.LVV);
    LV.setAdapter(adapter);
    LV.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
        {
        //Do whatever you want onClick of item of ListView
        }
    });
于 2012-05-16T20:12:01.007 回答