2012-05-22 62 views
1

我必须创建一个listview,其中单行有4个textview。 我的问题是我不明白我该如何使用适配器,所以有人可以帮我使用适配器及其实现?listview with 4 textview

这是我的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="center" 
android:orientation="vertical" > 




<TextView 
    android:id="@+id/textView1" 
    android:layout_width="192dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginBottom="20dp" 
    android:text="Grazie per aver parcheggiato da noi!" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<ListView 
    android:id="@+id/mylist" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
</ListView> 


<Button 
    android:id="@+id/btnEsci" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Torna a scelta" /> 

</LinearLayout> 

这是单行(single_feed)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<!-- TEXT OF FEED --> 

<TextView 
     android:id="@+id/txtId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Numero macchina: #" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/txtTarga" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Targa: #" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/txtOra" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ora ingresso: #" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 


    <TextView 
     android:id="@+id/txtCosto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Prezzo uscita: #" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:lines="1" 
     /> 

这是我的适配器:

public class MioAdapter extends ArrayAdapter<String> { 
     private final Context context; 
     private final String[] values; 

     public MioAdapter(Context context, String[] values) { 
      super(context, R.layout.single_feed, values); 
      this.context = context; 
      this.values = values; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View rowView = inflater.inflate(R.layout.single_feed, parent, false); 
      TextView textView = (TextView) rowView.findViewById(R.id.label); 
      textView.setText(values[position]); 
      // Change the icon for Windows and iPhone 


      return rowView; 
     } 

最后,这是我的活动:

public class checkout extends Activity{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     //apro la view 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.checkout); 

     //gestsco l'adapter per creare la lista 
     public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
        "Linux", "OS/2" }; 
      MioAdapter adapter = new MioAdapter(this, values); 
      setListAdapter(adapter); 
}; 

有人可以帮我使用适配器吗? 感谢所有帮助我在这个简单的麻烦:)

+0

提示:每个listview应该有'height'作为'fill_parent' – amp

+0

你想要4列(TextViews彼此相邻)吗? –

回答

2

See this Custom listView

并且在连续两个textviews,你只是添加四个TextView的如此行布局添加file.And添加在TextView的android:maxLines="1"单行textview。

1

对于单行(single_feed):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

还留着layout_width固定所有文本的意见,让他们可以适应单行。

1
  1. 在single_feed.xml布局中添加4个文字视图。这将是你的listitem的布局
  2. 找到所有的文字视图在获得MioAdapter的ID
  3. 通过以int为实例变量在适配器中维护索引。在getView
0

首先

  • 将文本分配有什么地方不对劲。如果你想要一个ListView,你需要一个元素列表(一个元素,一行)。

    所以,如果你想要每行多个TextView,你将需要每个元素多个值。所以你需要一个数组数组。

    您也可以尝试SimpleCursorAdapter。为您打造一个MatrixCursor与您的数据:MatrixCursor mc = new MatrixCursor(your_column_names);和添加元素(每行一个):mc.addRow(row_values);(其中your_column_namesrow_valuesString[]型)

    然后你使用一个SimpleCursorAdapter,其中String[] from = your_column_valuesint[] to = your_TextView_ids(从single_feed.xml IDS )

  • +0

    不,我不需要一个数组数组,因为我的信息是有序的,所以我知道在每个文本视图中会看到什么 – Lorenzo