ListView CursorAdapter问题

时间:2015-12-11 19:37:21

标签: android listview android-cursoradapter

我在Android Studio中使用Java和XML来使用CursorAdapter填充ListView。我对此有点新意并尝试解决问题。我真的很绿,所以任何提示都会有所帮助。

我的DBHandler看起来像这样:

public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 10;
private static final String DATABASE_NAME = "jogging.db";
public static final String TABLE_TRIP = "Trip";
public static final String TUR_COLUMN_ID = "_id";
public static final String TUR_COLUMN_DISTANCE = "Ditance";
public static final String TUR_COLUMN_SCORE = "Fastest";


public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_TRIP + "(" +
            TUR_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            TUR_COLUMN_DISTANCE + " TEXT, " +
            TUR_COLUMN_SCORE + " TEXT " +
            ");";
    db.execSQL(query);
}

CursorAdapter我试图用来获取ListView数据:

public class TripCursorAdapter extends CursorAdapter {

public TripCursorAdapter(Context context, Cursor cursor, int flags) {
    super(context, cursor, flags);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.content_trips, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tripId = (TextView) view.findViewById(R.id.tripIdView);
    TextView tripDistance = (TextView) view.findViewById(R.id.tripDistanceView);
    TextView tripScore = (TextView) view.findViewById(R.id.tripScoreView);

    String id = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
    String distance = cursor.getString(cursor.getColumnIndexOrThrow("Ditance"));
    String score = cursor.getString(cursor.getColumnIndexOrThrow("Fastest"));

    tripId.setText(id);
    tripDistance.setText(distance);
    tripScore.setText(score);
}

设置ListView的活动

public class TripsActivity extends AppCompatActivity {

DBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trips);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    dbHandler = new DBHandler(this);
    SQLiteDatabase db = dbHandler.getWritableDatabase();
    Cursor tripCursor = db.rawQuery("SELECT * FROM Trip", null );

    ListView lvItems = (ListView) findViewById(R.id.listView);
    TripCursorAdapter tripAdapter = new TripCursorAdapter(this, tripCursor, 0);
    lvItems.setAdapter(tripAdapter);
}

XML文件:

trips_content.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="hmk.fitness_app.TripsActivity"
tools:showIn="@layout/activity_trips">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

我还有item_trip.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ID"
    android:id="@+id/tripIdView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Distance"
    android:id="@+id/tripDistanceView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Score"
    android:id="@+id/tripScoreView"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

不太确定为什么我会收到关于null对象referances的错误消息。我应该做什么提示?

1 个答案:

答案 0 :(得分:3)

更改

return LayoutInflater.from(context).inflate(R.layout.content_trips, parent, false);

return LayoutInflater.from(context).inflate(R.layout.item_trip, parent, false);