用于卡列表视图的多个适配器

时间:2014-08-12 01:22:53

标签: android listview android-listview

如何将多个适配器添加到列表视图?在阅读this问题并按照教程后,我仍然没有得到我想要的东西。

方案

将两种类型的文本添加到SQLite数据库表中,并以卡片形式在另一项活动中检索它们,标题位于顶部,分隔符和下面的内容。

视觉效果

将内容添加到主列表: Adding title and content

主要名单上的卡片: Main Card view

我的代码

我认为没有人真正需要看到edittext部分的布局,所以我会直接跳到主卡行项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:background="@drawable/card_selecter_background">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample"
        android:textSize="30sp"/>

    <View
        android:id="@+id/separator"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="2dp"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample"
        android:textSize="22sp"/>

</LinearLayout>

这是我将edittext字符串添加到数据库的地方:

db.execSQL("CREATE TABLE IF NOT EXISTS Lists (Title VARCHAR, Content VARCHAR);");

addListBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        boolean  cancel = false;
        String listName = listNameET.getText().toString();
        String content = contentET.getText().toString();

        if (TextUtils.isEmpty(listName)) {
            listNameET.setError("Please enter a list name.");
            cancel = true;
        } else if (listName.contains("'") || listName.contains("\"")) {
            listNameET.setError("Please enter alphanumeric (abc123) characters only.");
            cancel = true;
        }

        if (TextUtils.isEmpty(content)) {
            contentET.setError("Field cannot be left blank.");
            cancel = true;
        } else if (content.contains("'") || content.contains("\"")) {
            contentET.setError("Please enter alphanumeric (abc123) characters only.");
            cancel = true;
        }

        if (cancel) {
            //Nothing happens
        } else {
            db.execSQL("INSERT INTO Lists VALUES('" + listName + "', '" + content + "');");
            db.close();
            finish();
        }
    }
});

以下是listview活动中检索的位置:

Cursor c = db.rawQuery("SELECT Title from Lists", null);
Cursor c2 = db.rawQuery("SELECT Content from Lists", null);

final ArrayList<String> titleList = new ArrayList<String>();
final ArrayList<String> contentList = new ArrayList<String>();

if (c.moveToFirst() && c2.moveToFirst()) {
    do {
        titleList.add(c.getString(c.getColumnIndex("Title")));
        contentList.add(c2.getString(c2.getColumnIndex("Content")));
    } while (c.moveToNext() && c2.moveToNext());
}

ArrayAdapter<String> titleAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.listname_row, R.id.tvTitle, titleList);
ArrayAdapter<String> contentAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.listname_row, R.id.tvContent, contentList);

ListView lv = (ListView) findViewById(R.id.listOfNames);
//Since Java works top to bottom, the contentAdapter is set even though
//this line is here.
lv.setAdapter(titleAdapter);
lv.setAdapter(contentAdapter);
db.close();
c.close();

我需要能够将标题和内容附加到同一列表视图中。

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

您可以像这样创建自定义适配器:

class ExampleAdapter extends BaseAdapter{

    List<String> mTitles;
    List<String> mContents;

    @Override
    public int getCount() {
        return mTitles.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v;

        //inflate view if needed
        ...

        TextView tv_title;
        TextView tv_content;

        String title = mTitles.get(position);
        String content = mContents.get(position);

        tv_title.setText(title);
        tv_content.setText(content);


        return v;
    }

}

现在超过此适配器的2个String列表并调用ListView的setAdapter。