我正在尝试将表格中的数据显示到listview中,但是当我打开模拟器时它会立即停止,我认为我有一个错误的nullpointer或类似的东西......或者我现在不是真的..
这是我的代码:
MainActivity.java:
public class MainActivity extends Activity {
ListView PersonsList;
DataBaseAdapter helper;
private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new DataBaseAdapter(this);
displayListView();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void displayListView() {
Cursor cursor = helper.fetchAllPersons();
// The desired columns to be bound
String[] columns = new String[]{
DataBaseAdapter.Helper.PERSON_NAME,
DataBaseAdapter.Helper.GENDER,
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.personName,
R.id.personGender,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.single_person_item,
cursor,
columns,
to,
0);
PersonsList = (ListView) findViewById(R.id.personsListView);
PersonsList.setAdapter(dataAdapter);
}
}
DataBaseAdapter.java ::
中的fetcAllPpersons() public Cursor fetchAllPersons() {
SQLiteDatabase db = helper.getWritableDatabase();
Cursor mCursor = db.query(Helper.PERSONS_TABLE, new String[] {Helper.PERSON_ID,Helper.PERSON_NAME,Helper.DATE_OF_BIRTH,Helper.GENDER,Helper.ADDRESS},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
的xml:
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView2"
android:layout_below="@+id/addnewperson"
android:layout_alignLeft="@+id/addnewperson"
android:layout_alignStart="@+id/addnewperson">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/personsListView"
android:layout_below="@+id/scrollView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</ScrollView>
single_person_item.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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/personName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/personGender" />
</LinearLayout>
其中PERSONS_TABLE是我希望显示来自
的信息的表格 问题在哪里?(问题很可能在这里:
dataAdapter = new SimpleCursorAdapter(
this, R.layout.single_person_item,
cursor,
columns,
to,
0);
)
但我无法察觉 thx提前
答案 0 :(得分:0)
我在代码中看到的一个问题(也可能有其他代码),ListView应该在scrollview中。如果您可以附加错误日志
也会很有帮助<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView2"
android:layout_below="@+id/addnewperson"
android:layout_alignLeft="@+id/addnewperson"
android:layout_alignStart="@+id/addnewperson">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/personsListView"
android:layout_below="@+id/scrollView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</ScrollView>