来自数据库的自定义ListView

| 我需要帮助将数据从数据库填充到自定义ListView中。我设法使用自己的自定义XML文件从数据库中显示数据。我现在遇到的问题是,每个TextView中都返回了所有三列-并非每个列都解析为相应的TextViews。我将尝试在下面口头解释我的需要-抱歉,请稍等,但我看到这些帖子上的不必要的答案和评论,因为有些成员不愿看代码并回答问题(我承认大多数Q很难遵循)。谢谢您的时间和帮助。如果无法理解您对Java和Android的新知识的回答,我会提前道歉 list_view.xml中带有一个带有RealitiveLayout的LinearLayout(带有自定义标题栏)和一个带有\“ listItems \”资源ID的ListView。在LinearLayout中的ListView(R.id.listItems)使用list_item.xml。在list_item.xml中是带有\“ acItems \”资源ID的RelativeLayout。它具有三个TextView,带有\“ label \”,\“ listTitle \”和\“ caption \”的资源ID。每个都有自己的风格。我需要查询位于设备SD卡上的数据库(xxx.db)。该数据库中的表名为\'AC_list \',其中包含列“” _id \“,\” label \“,\” title \“和\” goto \“。这些列需要传递到ListView(R.id.listItems)中的相应TextViews中(标签n2 \“ label \”,标题n2 \“ listTitle \”和描述n2 \“标题\”)以创建一个列表ITEM是每个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);
    }
}
}     
已邀请:
我这里有正确的答案以及正确的代码。     

要回复问题请先登录注册