我通常使用此代码将项目从数据库添加到列表视图:
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;
}
}