ListView 未填充
我创建了一个适配器来填充我的自定义 listView,当在模拟器上运行时,活动为空白。请帮忙。我确信我错过了一些东西,因为我是 java 和 java 的新手。安卓。一些纠正它的代码片段和指针将不胜感激。谢谢!
我的活动:
public class List_AC3 extends ListActivity {
/**
* -- Called when the activity is first created
* ===================================================================
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view2);
displayResultList();
}
private void displayResultList() {
Cursor databaseCursor = null;
DomainAdapter databaseListAdapter = new DomainAdapter(this, R.layout.list_item, databaseCursor,
new String[] {"label", "title", "description"},
new int[] { R.id.label, R.id.listTitle, R.id.caption });
databaseListAdapter.notifyDataSetChanged();
setListAdapter(databaseListAdapter);
}
}
我的适配器:
public class DomainAdapter extends SimpleCursorAdapter{
private LayoutInflater mInflater;
String extStorageDirectory;
public DomainAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.label);
holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
holder.text3 = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File dbfile = new File(extStorageDirectory+ "/Aero-Technologies/flyDroid/dB/flyDroid.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Cursor data = db.rawQuery("SELECT * FROM AC_list", null);
data.moveToPosition(position);
int label_index = data.getColumnIndex("label");
String label = data.getString(label_index);
int title_index = data.getColumnIndex("title");
String title = data.getString(title_index);
int description_index = data.getColumnIndex("description");
String description = data.getString(description_index);
holder.text1.setText(label);
holder.text2.setText(title);
holder.text3.setText(description);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
}
}
list_view2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="30dip"
android:padding="4dip"
android:background="@drawable/gradient" >
<ImageButton
android:id="@+id/homeBtn"
android:src="@drawable/ic_menu_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@null" />
<TextView
android:id="@+id/titleBarTitle"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="18sp" />
<ImageButton
android:id="@+id/toolBtn"
android:src="@drawable/ic_menu_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@null" />
</RelativeLayout>
<ListView
android:id="@id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
和我的 list_item.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/acItem"
style="@style/listItem" >
<TextView
android:id="@+id/label"
style="@style/listAcronym" />
<TextView
android:id="@+id/listTitle"
style="@style/listTitle" />
<TextView
android:id="@+id/caption"
style="@style/listDiscription"/>
<ImageView
style="@style/listNextIcon" />
</RelativeLayout>
I created an Adapter to populate my custom listView and when ran on the emulator the activity is blank. Plz help. I am sure I'm missing something 'cause I am new to java & Android. Some code snippets to correct it and pointers will be appreciated. Thnx!
My Activity:
public class List_AC3 extends ListActivity {
/**
* -- Called when the activity is first created
* ===================================================================
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view2);
displayResultList();
}
private void displayResultList() {
Cursor databaseCursor = null;
DomainAdapter databaseListAdapter = new DomainAdapter(this, R.layout.list_item, databaseCursor,
new String[] {"label", "title", "description"},
new int[] { R.id.label, R.id.listTitle, R.id.caption });
databaseListAdapter.notifyDataSetChanged();
setListAdapter(databaseListAdapter);
}
}
My Adapter:
public class DomainAdapter extends SimpleCursorAdapter{
private LayoutInflater mInflater;
String extStorageDirectory;
public DomainAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.label);
holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
holder.text3 = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File dbfile = new File(extStorageDirectory+ "/Aero-Technologies/flyDroid/dB/flyDroid.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Cursor data = db.rawQuery("SELECT * FROM AC_list", null);
data.moveToPosition(position);
int label_index = data.getColumnIndex("label");
String label = data.getString(label_index);
int title_index = data.getColumnIndex("title");
String title = data.getString(title_index);
int description_index = data.getColumnIndex("description");
String description = data.getString(description_index);
holder.text1.setText(label);
holder.text2.setText(title);
holder.text3.setText(description);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
}
}
The list_view2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="30dip"
android:padding="4dip"
android:background="@drawable/gradient" >
<ImageButton
android:id="@+id/homeBtn"
android:src="@drawable/ic_menu_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@null" />
<TextView
android:id="@+id/titleBarTitle"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="18sp" />
<ImageButton
android:id="@+id/toolBtn"
android:src="@drawable/ic_menu_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@null" />
</RelativeLayout>
<ListView
android:id="@id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
And my list_item.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/acItem"
style="@style/listItem" >
<TextView
android:id="@+id/label"
style="@style/listAcronym" />
<TextView
android:id="@+id/listTitle"
style="@style/listTitle" />
<TextView
android:id="@+id/caption"
style="@style/listDiscription"/>
<ImageView
style="@style/listNextIcon" />
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Google 记事本教程 也应该对您有帮助 IIRC 他们应该使用传递的光标到列表视图
the google notepad tutorials should also help you IIRC they should be using cursors passed to a listview
SimpleCursorAdapter 不需要扩展即可工作。将数据库逻辑从 getView 中取出,并使用它来构造一个实际指向数据库结果的游标。然后将该游标传递给 SimpleCursorAdapter 构造函数。事实上,我不认为 getView 实际上在任何地方被调用。
尝试让这个示例正常工作 http://thinkandroid.wordpress.com/2010 /01/09/simplecursoradapters-and-listviews/ 首先,然后对其进行编辑以执行您需要的操作。
如果您想做一些更复杂的事情(例如像在 getView 中尝试那样自己设置各种文本视图),请查看 CursorAdapter。
SimpleCursorAdapter doesn't need to be extended to work. Take your db logic out of getView and use it to construct a cursor which actually points to a db result. Then pass that cursor to the SimpleCursorAdapter constructor. In fact, I don't think getView is actually being called anywhere.
Try getting this example working http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/ first and then edit it to do what you need.
If you want to do something more complex (like setting the various textviews yourself like you're trying to do in getView) look into CursorAdapter.
可以在此处以及代码中找到正确的答案。
The correct answer can be found HERE along with the code.