[英]How to create own custom row layout using simple adapter and listview
I need help in creating a custom listview that allows me to have 2 strings/textviews per row. 我需要帮助创建一个自定义列表视图,允许我每行有2个字符串/文本视图。 I have been researching a lot, but I cannot seem to understand how to do this. 我一直在研究很多,但我似乎无法理解如何做到这一点。 Any sample code and help would be appreciated. 任何示例代码和帮助将不胜感激。 I know how to use simple_list_item_1, but not my own layout. 我知道如何使用simple_list_item_1,但不是我自己的布局。 Thank YOU. 谢谢。 My (Still Non-Functioning) Code 我的(仍然无法运作)代码
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;
}
}
With reference to this question, use this code. 参考此问题,请使用此代码。
EDIT: Added hashmap definition 编辑:添加了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
with a couple of TextViews
使列表视图可以是带有几个TextViews
的LinearLayout
R.layout.yourlayoutname
in this line SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
在此行中使用R.layout.yourlayoutname
引用此列表布局SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
The good thing about this approach is that it avoids you having to create any new objects, and it doesn't involve much code. 这种方法的好处是它避免了你必须创建任何新对象,并且它不涉及太多代码。
At first you need to create view to hold your custom list item 首先,您需要创建视图来保存自定义列表项
<?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>
After that you will need to create a view for your list item 之后,您需要为列表项创建一个视图
<?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>
And you gonna need a custom class to implement new view 而且你需要一个自定义类来实现新视图
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;
}
}
Reference: 参考:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.