Android:SimpleCursorAdapter.ViewBinder,listview,错误

时间:2013-12-05 20:27:41

标签: android listview layout simplecursoradapter

我想在ListView中显示两个textView,具体取决于数据库值。 但结果没有显示(错误) 这是我的代码:

public class ListbookActivity extends ListActivity implements OnQueryTextListener{

private DBHelper database;
private Intent intent;
private ListView listav;
private SimpleCursorAdapter adapter;
private String strclickmenu;
private Cursor cursor;
private Toast t;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    String[] from = new String[] { "titolo", "autore" };
    int[] to = new int[] { R.id.name, R.id.surname};


    database = new DBHelper(getApplicationContext());
    SQLiteDatabase db = database.getReadableDatabase();

    //query
    cursor = db.query("libreria", null, null, null, null, null, "titolo", null);

    cursor.moveToFirst();   

    listav = (ListView) findViewById(R.id.list);
    adapter =  new SimpleCursorAdapter(this,R.layout.list_row, cursor,from,to,0);
    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
             if (view.getId() == R.id.name) {
                    String userFullName = cursor.getString(cursor.getColumnIndex("titolo"));
                    TextView tit = (TextView)view;
                    tit.setText(userFullName);
                    return true;
                } else if (view.getId() == R.id.surname) {
                    String userEmail = cursor.getString(cursor.getColumnIndex("titolo"));
                    TextView aut = (TextView)view;
                    aut.setText(userEmail);
                    return true;
                } else {
                    return false;
                }
        }
    });
    listav.setAdapter(adapter);

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal" >

<TextView
    android:id="@+id/name"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1.51"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
    android:id="@+id/surname"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1.51"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />         

 </LinearLayout>

activity_listbook.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <ListView android:id="@+id/list"
          android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:smoothScrollbar="false"
            android:fadingEdge="none"/>

</LinearLayout>

这是错误:

无法启动活动componentInfo {com.example.mybooks.ListBookActivity}:Java.lang.nullPointerException

0 个答案:

没有答案