0

私はAndroidリストビューを持っていて、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);

text1 が "NameA v NameB" になるように (SQL クエリを変更せずに) 2 つの名前を連結するにはどうすればよいでしょうか...

前もって感謝します

4

4 に答える 4

1

クエリで行う

NameA || "v" || NameB AS NameAB

次に、2番目のtextView(android.R.text2)を削除します

リターンプロジェクションでは、「NameAB」を入力し、他の列は不要になるため、他の列は省略します(KEY_IDは保持します)。

于 2013-03-06T03:39:55.483 に答える
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;
    }

}

あとは、データベース アクセス関数を少し変更して、名前の 2 つの配列を返し、それらをコンストラクターに渡すだけです。CustomAdapter

最終的に、次のように呼び出します。

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

注意として、リンクで提案されているViewHolder パターンに従うようにしてください。

于 2012-10-18T09:36:20.983 に答える
0

BaseAdapter を拡張するカスタム Adapter を作成する必要があるようです。

于 2011-06-13T21:53:06.657 に答える