2011-06-27 63 views
2

我需要幫助創建自定義列表視圖,該列表視圖允許每行有2個字符串/文字查看。我一直在研究很多,但我似乎無法理解如何做到這一點。任何示例代碼和幫助將不勝感激。我知道如何使用simple_list_item_1,但不知道我自己的佈局。 謝謝你。 我(靜止無功能)代碼如何使用簡單的適配器和列表視圖創建自己的自定義行佈局

package com.painLogger; 
//ALL IMPORTS 

public class PainLoggerActivity extends Activity implements OnClickListener, 
OnKeyListener { 

    /** Called when the activity is first created. */ 
    EditText txtItem; 
    EditText txtItem2; 
    Button btnAdd; 
    ListView listItems; 
    ArrayAdapter <String> aa; 
    List < HashMap < String, String >> painItems = new ArrayList < HashMap < String, String >>(); 
    int[] to; 
    String[] from; 
    SimpleAdapter adapter; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     txtItem = (EditText) findViewById(R.id.txtItem); 
     txtItem2 = (EditText) findViewById(R.id.txtItem2); 

     btnAdd = (Button) findViewById(R.id.btnAdd); 
     listItems = (ListView) findViewById(R.id.listItems); 

     btnAdd.setOnClickListener(this); 

     from = new String[] { 
      "row_1", "row_2" 
     }; 
     to = new int[] { 
      R.id.row1, R.id.row2 
     }; 

     SimpleAdapter adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout, 
      from, to); 
     listItems.setAdapter(adapter); 
    } 

    private void addItem() { 
     HashMap < String, String > map = new HashMap < String, String >(); 

     map.put("row_1", txtItem.getText().toString()); 
     map.put("row_2", txtItem2.getText().toString()); 
     painItems.add(map); 
     adapter.notifyDataSetChanged(); 
    } 

    @Override 
    public void onClick(View v) { 
     if (v == this.btnAdd) { 
      addItem(); 
     } 
    } 

    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 

     if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == 
      KeyEvent.KEYCODE_DPAD_CENTER) {    this.addItem(); 

     } 
     return false; 
    } 
} 

回答

7

參照this問題,使用此代碼。
編輯:添加HashMap的定義

String[] from = new String[] {"row_1", "row_2"}; 
int[] to = new int[] { R.id.row1, R.id.row2}; 
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 

for (int j = 0; j < sourceObj.length(); j++) { 
     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("row_1", sourceObj.data1); 
     map.put("row_2", sourceObj.data2); 
     fillMaps.add(map); 
} 

SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.yourlayoutname, from, to); 
mListView.setAdapter(adapter); 
  • 讓你列表視圖這可能是一個LinearLayout與一對夫婦TextViews
  • 參考在這條線使用R.layout.yourlayoutname在數據SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
  • 通過這個列表佈局

這種方法的好處是,它避免了你不得不創建任何新的對象,並且它不涉及太多的代碼。

+0

在這種情況下,我將如何創建我的佈局以實現此解決方案?你參考這是'R.layout.result','R.layout.yourlayoutname'/ – Kgrover

+0

我的程序不識別字段「地圖」,我們說:map.put等 – Kgrover

+2

對不起 - 我的錯誤 - 查看我在for循環中定義了hashmap的位置。另外,我假設你已經在你的第一條評論中解決了這個問題,但是如果你還沒有,在@ HERO的答案中使用類似xml的東西。 –

3

首先,你需要創建視圖來保存您的自定義列表項

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 
<TextView 
    android:id="@+id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="No Item to display"/> 
</LinearLayout> 

之後,你需要創建一個視圖列表項目

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:id="@+id/toptext" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
     /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:id="@+id/bottomtext" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
     /> 
    </LinearLayout> 
</LinearLayout> 

而且你會需要一個自定義的類來實現新的視圖

private class OrderAdapter extends ArrayAdapter<Order> { 

    private ArrayList<Order> items; 

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      //inflate a new view for your list item 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.row, null); 
     } 
     Order o = items.get(position); 
     if (o != null) { 
      //set text to view 
      TextView tt = (TextView) v.findViewById(R.id.toptext); 
      TextView bt = (TextView) v.findViewById(R.id.bottomtext); 
      if (tt != null) { 
        tt.setText("Name: "+o.getOrderName());       } 
      if(bt != null){ 
        bt.setText("Status: "+ o.getOrderStatus()); 
      } 
     } 
     return v; 
    } 
} 

參考:

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

相關問題