我通常使用此代码将项目从datebase添加到列表视图:
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
但是,在我的项目中,我将图像添加到listview,如下所示:
<?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();
}
}
removearrayadapter代码:
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;
}
}
答案 0 :(得分: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);
基本上只是将光标传递到简单光标适配器,它将从您的布局填充列表,当您已经拥有信息时无需创建listadapter
编辑:自定义适配器示例
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);
}
}
}
这两部分代码来自我的程序的两个不同部分,所以请不要认为它们在一起
答案 1 :(得分: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
}
});
答案 2 :(得分: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
方法中初始化 db :
DBAdapter DB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewevents);
DB = new DBAdapter(this);
//... rest of the code
此外,您应该尝试使用基于Cursor
的适配器,这样您就不必在列表中解析光标。
修改:
修改您的RemoveArrayAdapter
以接受使用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));