我需要帮助将数据从数据库填充到自定义ListView中。我设法使用我自己的自定义XML文件从数据库中获取数据。我现在的问题是在每个TextView中返回所有三列 - 并非每个列都解析为相应的TextView。我会尝试尝试口头解释我需要的东西 - 对于额外的jiberish,但我看到不必要的回答&对这些帖子的评论,因为一些成员回答没有看代码&假设的问题(我承认大多数Q难以遵循)。感谢您的时间和帮助。我提前道歉,如果我不理解你的答案我是一个新的Java & Android来自数据库的自定义列表视图
list_view.xml有一个LinearLayout w /一个RealitiveLayout W /在它(对于一个自定义标题栏)和一个ListView w /资源ID为“listItems”。 ListView(R.id.listItems),即LinearLayout中的w /,使用list_item.xml。在list_item.xml中有一个RelativeLayout,其资源ID为“acItems”。这有三个TextViews,资源ID是“label”,“listTitle”,&“caption”。每个人都有自己的风格。我需要查询位于设备SD卡上的数据库(xxx.db)。该数据库中的表格名为“AC_list”,其中包含列“_id”,“label”,“title”&“goto”。这些列需要传递到ListView(R.id.listItems)中相应的TextViews(标签n2“标签”,标题n2“listTitle”,&描述n2“caption”),以便使每个TextView具有它的一个列表项目自己的每个db行的样式。请看下图:
的list_view.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/listItems"
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>
我的适配器(AC_Adapter.java):
public class AC_Adapter extends ArrayAdapter<String> {
static List<String> Title = new ArrayList<String>();
static List<String> Label = new ArrayList<String>();
static List<String> Description = new ArrayList<String>();
Context myContext;
public AC_Adapter(Context context, int resource, List<String> aTitle,
List<String> aLabel, List<String> aDescription) {
super(context, resource, aTitle);
myContext = context;
Title = aTitle;
Label = aLabel;
Description = aDescription;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
TextView tv_label = (TextView) v.findViewById(R.id.label);
TextView tv_title = (TextView) v.findViewById(R.id.listTitle);
TextView tv_decription = (TextView) v.findViewById(R.id.caption);
if (tv_label != null) {
tv_label.setText(Label.get(position));
}
if (tv_title != null) {
tv_title.setText(Title.get(position));
}
if (tv_decription != null) {
tv_decription.setText(Description.get(position));
}
return v;
}
}
我的活动( List_AC.java):
public class List_AC extends ListActivity {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String extStorageDirectory;
private ArrayList<String> results = new ArrayList<String>();
/**
* -- 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);
openAndQueryDatabase();
displayResultList();
}
/**
* -- Connect to the AC_Adapter
* ===================================================================
**/
private void displayResultList() {
AC_Adapter adapter = new AC_Adapter(getApplicationContext(),
R.layout.list_item, results, results, results);
setListAdapter(adapter);
}
/**
* -- Open and Query the Database
* ====================================================================
**/
private void openAndQueryDatabase() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File dbfile = new File(extStorageDirectory
+ "/XXX/XXX/dB/XXX.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Log.i("tag", "db opened");
try {
Cursor c = db.rawQuery(
"SELECT label, title, discription, goto FROM AC_list", null);
if (c != null) {
if (c.moveToFirst()) {
do {
String i1 = c.getString(c.getColumnIndex("label"));
String i2 = c.getString(c.getColumnIndex("title"));
String i3 = c.getString(c.getColumnIndex("discription"));
results.add(i1);
} while (c.moveToNext());
}
}
} finally {
if (db != null)
Log.i("tag", "db closed");
db.close();
}
} else if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_UNMOUNTED)) {
Log.i("tag", "SDCard is NOT writable/mounted");
Alerts.sdCardMissing(this);
}
}
}