【发布时间】:2013-04-09 07:55:42
【问题描述】:
我正在尝试将数据从 sqlite 获取到 listview,因此我按照此 url 中的答案中的示例进行操作 Make listview from SQLite database
CustomCursorAdapter 是这样的
public class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.topics, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
//1 is the column where you're getting your data from
String name = c.getString(1);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.listLabel);
if (name_text != null) {
name_text.setText(name);
}
}
}
但是 onCreate 我有这个代码
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
用xml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="@+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="15sp" >
</TextView>
</LinearLayout>
问题是.. listView 是空的!! ..我不知道为什么!!
【问题讨论】:
-
发布您的 logcat 错误。
-
在获取 listview 参考之前是否设置了 ContentView
-
你的布局中的 ListView 在哪里?
-
你的 ListView 在 xml 中的什么位置??
-
您是否正在寻找自定义列表视图,其中包含每个 roe 的图像和文本视图?
标签: android sqlite nullpointerexception simplecursoradapter