2011-06-13 33 views
0

我有一个Android的ListView和它有一个two_line_list_item布局....文本1和文本的毗连结果到一个单一的列表视图元素

我有一个SQL查询返回了我....光标在下面的例子我已经设置NAMEA从SQL到文本1和NameB到文本2

 // 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查询),所以文本1将“NAMEA v NameB” ...

在此先感谢

回答

0

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

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中的一切。

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 pattern在链接中建议。

1

在查询不

NameA || "v" || NameB AS NameAB 

然后卸下第二的TextView(android.R.text2)

在你的回报预测把 “NameAB” 离开了其他列(保持KEY_ID)为您将不再需要他们。

相关问题