我正在做我的最后一个项目,我遇到了一个问题。我想将数据从我的数据库(DatabaseBon)传递到列表视图。有人可以帮帮我吗?
我的活动看起来像:
public class BlackNumbersBlockDb extends Activity {
String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blocknumbersdb);
@SuppressWarnings("unused")
Button imBT= (Button)findViewById(R.id.importBL);
DataBaseBON info= new DataBaseBON(this);
info.open();
Cursor cursor=info.getcursor();
startManagingCursor(cursor);
ContactCursorAdapter adapter = new ContactCursorAdapter(null, cursor);
ListView contactLV = (ListView) findViewById(R.id.listviewblDB);
contactLV.setAdapter(adapter);
info.close();
imBT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent ("android.intent.action.IMPORTM");
startActivity(i);
}
});
我的光标适配器看起来像:
**import vahid.engineer.com.DataBaseBON** ( is it c orrect??)
.
.
.
public class ContactCursorAdapter extends CursorAdapter {
public ContactCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView)view.findViewById(R.id.blacklistDB1);
name.setText(cursor.getString(cursor.getColumnIndex(DataBaseBON.N_NAME)));
TextView phone = (TextView)view.findViewById(R.id.blacklistDB2);
phone.setText(cursor.getString(cursor.getColumnIndex(DataBaseBON.KEY_NUMBER)));}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.lv, parent, false);
bindView(v, context, cursor);
return v;
我的 LV XML 文件看起来像
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/blacklistDB1"
android:layout_width="33dp"
android:layout_height="wrap_content"
android:text="Large Text"
/>
<TextView
android:id="@+id/blacklistDB2"
android:layout_width="19dp"
android:layout_height="wrap_content"
android:text="Large Text"
/>
</LinearLayout>
我的 blockornot XML 文件看起来像
< 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/listviewblDB"
android:layout_width="match_parent"
android:layout_height="499dp"
android:layout_weight="0.72" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="129dp"
android:gravity="bottom|center_horizontal|fill|top"
android:orientation="horizontal" >
<Button
android:id="@+id/importBL"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="10sp"
android:layout_marginTop="20sp"
android:ems="10"
android:text="Import"
android:textSize="20sp"/>
<Button
android:id="@+id/deleteBL"
android:layout_width="126dp"
android:layout_height="90dp"
android:layout_marginLeft="10sp"
android:layout_marginTop="20sp"
android:ems="10"
android:text="Delete"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/blacklistDB1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/blacklistDB2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="match_parent"
android:layout_height="626dp"
android:gravity="bottom|center_horizontal|fill|top"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="129dp"
android:gravity="bottom|center_horizontal|fill|top"
android:orientation="horizontal" >
<Button
android:id="@+id/importBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Import" android:textSize="30sp"/>
<Button
android:id="@+id/deleteBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:text="Delete" android:textSize="30sp"/>
</LinearLayout>
我的错误
java.lang.NullPointerException
at android.view.LayoutInflater.from(LayoutInflater.java:171)
at vahid.engineer.com.ContactCursorAdapter.newView(ContactCursorAdapter.java:40)
at android.widget.CursorAdapter.getView(CursorAdapter.java:182)
at android.widget.AbsListView.obtainView(AbsListView.java:1315)
at android.widget.ListView.makeAndAddView(ListView.java:1727)