我正在使用这种类型的数据结构
static List<String> displaypart = new ArrayList<String>();
static List<String> displayitalic = new ArrayList<String>();
static List<String> displayA_name = new ArrayList<String>();
static List<String> displayAS_name = new ArrayList<String>();
我已通过此代码将数据库的值添加为数据库
String strquery="select * from Articles where AS_name LIKE '%"+querytext+"%'";
cur=db.rawQuery(strquery,null);
if(cur!=null&&cur.moveToFirst())
{
do
{
Flag.displaypart.add(cur.getString(7));
Flag.displayA_name.add(cur.getString(2));
Flag.displayitalic.add(cur.getString(8));
Flag.displayAS_name.add(cur.getString(3));
Flag.displayDesc_art.add(cur.getString(4));
i++;
}
while(cur.moveToNext());
}
}
我的要求是在listview中显示这些数据。每个单元格由4个文本视图组成。为此我做了
query_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:id="@+id/querylinearlayout">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Small Text"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView // this textview just gives the black color margin
android:id="@+id/textView5"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000"
android:text="TextView" />
</LinearLayout>
我想在Listview中充气!
query_fragment.xml 由listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:id="@+id/queryrelativelayout">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
我的问题是如何将query_cell.xml
扩展到此列表视图中?
为此,我在onCreateView
这样的
listview1 = (ListView)rootView.findViewById(R.id.listView1);
我看过一些教程使自定义适配器扩展BaseAdapter,一些扩展了ArrayList,我也很困惑它的构造函数应该是什么样的,&amp;也应该使用或不使用持有人getView
。
我的CustomAdapter应该是什么样的。你会怎么做?如果您有自己的实施方式,我也会欣赏不同的编码风格。
请帮忙
先谢谢
AFTET SQUONK ANSWER Logcat
07-28 23:13:14.979: E/AndroidRuntime(3040): FATAL EXCEPTION: main
07-28 23:13:14.979: E/AndroidRuntime(3040): java.lang.IllegalArgumentException: column '_id' does not exist
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
07-28 23:13:14.979: E/AndroidRuntime(3040): at com.vivekwarde.indianconstitution.DBAdapter.queryArticles(DBAdapter.java:271)
07-28 23:13:14.979: E/AndroidRuntime(3040): at com.vivekwarde.indianconstitution.QueryFragment.onCreateView(QueryFragment.java:44)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.os.Handler.handleCallback(Handler.java:615)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.os.Looper.loop(Looper.java:137)
07-28 23:13:14.979: E/AndroidRuntime(3040): at android.app.ActivityThread.main(ActivityThread.java:4794)
07-28 23:13:14.979: E/AndroidRuntime(3040): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 23:13:14.979: E/AndroidRuntime(3040): at java.lang.reflect.Method.invoke(Method.java:511)
07-28 23:13:14.979: E/AndroidRuntime(3040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-28 23:13:14.979: E/AndroidRuntime(3040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-28 23:13:14.979: E/AndroidRuntime(3040): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
使用SimpleCursorAdapter
...
public void initAdapter(Context ctx, ListView myListView) {
String strquery = "SELECT unique_column_name AS _id,column1,column2,column3,column4 FROM MY_TABLE";
cur = db.rawQuery(strquery,null);
if (cur != null && cur.getCount() > 0) {
String[] fromColumns = { "column1", "column2", "column3", "column4" };
int[] toViews = { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(ctx,
R.layout.query_cell, cur, fromColumns, toViews, 0);
myListView.setAdapter(adapter);
}
}
从Activity
使用...
initAdapter(this, listView1);
从Fragment
使用...
initAdapter(getActivity(), listView1);
在上面,fromColumns
数组是要映射到TextViews
的数据库列的名称,toViews
数组是每个TextView
的资源标识符。在列表项布局中(您的query_cell,xml布局文件)。
在SimpleCursorAdapter
构造函数中,您只需传入列表项布局资源ID(R.layout.query_cell),列和视图数组以及SimpleCursorAdapter
将完成所有其余工作对你而言。