我需要幫助將數據從數據庫填充到自定義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);
}
}
}