数据库中的自定义ListView

时间:2011-04-27 15:41:42

标签: android database listview adapter listadapter

我需要帮助将数据库中的数据填充到自定义ListView中。我设法使用我自己的自定义XML文件从数据库中显示数据。我现在的问题是在每个TextView中返回所有三列 - 不是每列都解析为相应的TextViews。我将尝试尝试口头解释我需要的东西 - 为额外的西伯利亚而烦恼但是我看到了不必要的答案&对这些帖子的评论是因为有些成员在没有查看代码和答案的情况下回答通过问题假设(我承认大多数Q很难遵循)。谢谢你的时间和帮助。如果我不理解你的答案,我会提前道歉我是Java和Java的新手。的Android

list_view.xml有一个带有RealitiveLayout的LinearLayout(对于自定义标题栏)和一个带有“listItems”的资源ID的ListView。 ListView(R.id.listItems),即LinearLayout中的w /,使用list_item.xml。在list_item.xml中是一个RelativeLayout,其资源ID为“acItems”。这有三个TextViews,资源ID是“label”,“listTitle”,& “字幕”。每个人都有自己的风格。我需要查询位于设备SD卡上的数据库(xxx.db)。此数据库中的表格名为“AC_list”,其中包含“_id”,“label”,“title”和“amp”列。 “去”。这些列需要传递到ListView(R.id.listItems)中相应的TextViews(标签n2“标签”,tittle n2“listTitle”和& description n2“caption”),以使每个TextView具有一个列表项目每个数据库行都有自己的样式。见下文:

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);
    }
}

}

1 个答案:

答案 0 :(得分:2)

我有正确的答案HERE以及正确的代码。