0

我有一个 android listview,它有一个 two_line_list_item 布局.... text1 和 text2

我有一个 SQL 查询,它返回我的光标....在下面的示例中,我将 SQL 中的 NameA 设置为 text1,将 NameB 设置为 text2

        // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{"NameA", "NameB"};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{android.R.id.text1, android.R.id.text2};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter matches = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, MatchesCursor, from, to);
    setListAdapter(matches);

我怎么能去连接这两个名字(不改变我的 SQL 查询)所以 text1 将是“NameA v NameB”......

提前致谢

4

4 回答 4

1

在您的查询中

NameA || "v" || NameB AS NameAB

然后删除第二个 textView (android.R.text2)

在您的返回预测中,将“NameAB”省略其他列(保留 KEY_ID),因为您将不再需要它们。

于 2013-03-06T03:39:55.483 回答
0

在我看来,您需要编写扩展 BaseAdapter 的自定义适配器。

于 2011-06-13T21:53:06.657 回答
0

一种肮脏的方式是在您的 xml 中使用 3 个视图:

<TextView
        android:id="@+id/nameA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp" />
<TextView
        android:id="@+id/separator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" vs "
        android:textSize="30dp" />
<TextView
        android:id="@+id/nameB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp" />

将所有内容包装在水平 LinearLayout 中。

于 2012-10-18T09:12:22.883 回答
0

您需要编写自己的适配器来扩展BaseAdapter

public class CustomAdapter extends BaseAdapter {

    private Context context;
    private int listItemLayout;
    private String[] nameAs;
    private String[] nameBs;

    public CustomAdapter(Context context, int listItemLayout, String[] nameAs, String[] nameBs) {
        this.context = context;
        this.listItemLayout = listItemLayout;
        this.nameAs = nameAs;
        this.nameBs = nameBs;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView==null)
            convertView = LayoutInflater.from(context).inflate(listItemLayout, null);

            TextView textView1 = (TextView)findViewById(android.R.id.text1);
            textView1.setText(nameAs[position] + " v " + nameBs[position]);
        return convertView;
    }

}

现在你需要做的就是修改一些你的数据库访问函数来返回你的两个名字数组,并将它们传递给CustomAdapter

最后,调用:

CustomAdapter myAdapter = new CustomAdapter(this, android.R.layout.two_line_list_item, nameAs, nameBs);
setListAdapter(myAdapter);

请注意,还请尝试遵循链接中建议的ViewHolder 模式

于 2012-10-18T09:36:20.983 回答