繁体   English   中英

如何使用简单的适配器和列表视图创建自己的自定义行布局

[英]How to create own custom row layout using simple adapter and listview

我需要帮助创建一个自定义列表视图,允许我每行有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;
     }
 }

参考问题,请使用此代码。
编辑:添加了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);
  • 使列表视图可以是带有几个TextViewsLinearLayout
  • 在此行中使用R.layout.yourlayoutname引用此列表布局SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
  • 传入您的数据

这种方法的好处是它避免了你必须创建任何新对象,并且它不涉及太多代码。

首先,您需要创建视图来保存自定义列表项

<?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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM