我为 Listview 尝试了一个自定义简单光标适配器。我从数据库中获取数据并尝试显示我的列表视图但我遇到了一些异常。如果你帮助我,我会很高兴谢谢。这是代码:
public class cursorDeneme extends ListActivity{
Cursor c ;
String sutunlar[] = new String[]{"name"};
int to[] = new int[]{R.id.name_entry};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cursorden);
DB_E db = new DB_E(this);// database class
try {
db.openReadable();
c=db.veriAlCursor();// gets Cursor from database
mySimpleCursorAdapter adapter = new mySimpleCursorAdapter(this, R.layout.cursorden, c, sutunlar, to);
db.close();
this.setListAdapter(adapter);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
这是数据库类:
public class DB_E {
public static final String KEY_ROWID ="id";
public static final String KEY_WORK_OUT ="workout"; //Hangi workout
public static final String KEY_NAME ="name";
public static final String KEY_REP ="rep";
public static final String KEY_WEIGHT ="weight";
public Cursor veriAlCursor(){ //get datas
Cursor c = ourDataBase.query(DATABASE_TABLE, sutunlar, null, null, null, null, null);
return c;
}
public Cursor veriAlCursor(String workout){ //get datas
workout ="'%"+workout+"%'";
Cursor c = ourDataBase.query(DATABASE_TABLE, sutunlar, KEY_WORK_OUT+" LIKE "+workout, null, null, null, null);
return c;
}
和 mySimpleCustomAdapter:
public class mySimpleCursorAdapter extends SimpleCursorAdapter {
Cursor c;
Context context;
Activity activity;
cursorDeneme cursorD ;
public mySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c=c;
this.activity=(Activity)context;
this.context=context;
this.cursorD =(cursorDeneme)context;
}
@Override
public View getView(int position , View convertView , ViewGroup parent){
if(convertView == null)
convertView.inflate(context, R.layout.liste, null);
View row=convertView;
c.moveToPosition(position);
int iName =c.getColumnIndex("name");
TextView name =(TextView)convertView.findViewById(R.id.name_entry);
name.setText(c.getString(iName));
return (row);
}
和 XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#848484" >
<LinearLayout
android:id="@+id/linear1"
android:layout_width="240dp"
android:layout_height="350dp"
android:background="#CEF6E3"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
>
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#151515"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
></ListView>
</LinearLayout>
</LinearLayout>
错误日志:未处理的事件循环异常无效的首选项页面路径:XML 语法...谢谢。