我从我的 WebService 获取数组。
我懂了:
WS w=new WS();
w.WebServiceCallExampleGetArray();
返回这个:List<Category>
我有列表视图 ListView list;
如何将w.WebServiceCallExampleGetArray()绑定 到 listView
我从我的 WebService 获取数组。
我懂了:
WS w=new WS();
w.WebServiceCallExampleGetArray();
返回这个:List<Category>
我有列表视图 ListView list;
如何将w.WebServiceCallExampleGetArray()绑定 到 listView
我编写了一个通用类来将列表视图绑定到可能对您有用的列表。你可以根据需要扩展这个想法。首先,我假设您接受我的建议并使用 ORM。这意味着您的数据库查询最终会转换为 T 类的可枚举数组。
例如,假设您有一个来自 PointClass 的项目列表和一个名为 (mylistView) 的 ListView,并且您希望使用以下行样式将此类绑定到列表视图(有趣的部分是 tag="Bind={}"):
listrowitems.xml:
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/itemName"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:tag="Bind={Name}" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/itemPoint"
android:layout_width="wrap_content"
android:gravity="right"
android:tag="Bind={Point}" />
<TextView
android:id="@+id/itemScale"
android:layout_width="wrap_content"
android:gravity="right"
android:tag="Bind={UnitScale}" />
</LinearLayout>
</LinearLayout>
使用 ListViewAdapter 类(代码在下面),这只是两行代码:
listadapter = new ListViewAdapter(ActivityDailyRecord.this,items,R.layout.listrowitems);
mylistView.setAdapter(listadapter);
有趣的部分是tag="Bind={FieldName}" ...它遍历整个 listrowitems 布局并使用反射设置字段值。你应该小心xml文件标签中的字段名。
public class ListViewAdapter extends BaseAdapter {
private List<?> items;
private Context context = null;
private int rowLayoutResId;
public interface RowChildClickCallBack {
public void OnClick(int position, View v);
}
private HashMap<Integer, RowChildClickCallBack> eventMappers = null;
/**
*
*/
public ListViewAdapter(Context context, List<?> items, int rowLayourId,
HashMap<Integer, RowChildClickCallBack> eventMappers) {
this.items = items;
this.context = context;
this.rowLayoutResId = rowLayourId;
this.eventMappers = eventMappers;
// items.get(0).getClass().getName()
}
public ListViewAdapter(Context context, List<?> items, int rowLayourId) {
this.items = items;
this.context = context;
this.rowLayoutResId = rowLayourId;
this.eventMappers = null;
// items.get(0).getClass().getName()
}
public void setItems(List<?> items) {
this.items = items;
}
public void rebind(List<?> items) {
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return (Object) items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(rowLayoutResId, null);
} else {
gridView = (View) convertView;
// Log.e("ListViewAdapter",String.valueOf(position));
}
ViewGroup vg = (ViewGroup) gridView;
// Log.e("ListViewAdapter",String.valueOf(position));
traverseControls(vg, position);
return gridView;
}
private boolean hasBindValue(String tagStr) {
// // *Bind={sth}*
Pattern pBindMathcher = Pattern.compile(".*Bind=\\{(\\w+)\\}.*");
Matcher mBind = pBindMathcher.matcher(tagStr);
return mBind.matches();
}
private String getBindValue(String tagStr) {
// // Bind={sth},Style={fontName:{}}
Pattern pBindMathcher = Pattern.compile(".*Bind=\\{(\\w+)\\}.*");
Matcher mBind = pBindMathcher.matcher(tagStr);
String bind = "";
if (mBind.matches()) {
bind = mBind.group(1);
}
return bind;
}
private View traverseControls(ViewGroup v, int position) {
View invalid = null;
for (int i = 0; i < v.getChildCount(); i++) {
View child = v.getChildAt(i);
if (child.getTag() != null) {
if (hasBindValue(child.getTag().toString())) {
String fName = getBindValue(child.getTag().toString());
try {
Field f = items.get(position).getClass()
.getField(fName);
// //////////// Set Text if it has bindings
// ////////////
if (child instanceof EditText) {
EditText e = (EditText) child;
e.setText(String.valueOf(f.get(items.get(position))));
} else if (child instanceof TextView) {
TextView e = (TextView) child;
e.setText(String.valueOf(f.get(items.get(position))));
}
} catch (Exception ex) {
}
}
} else if (child instanceof ViewGroup) {
invalid = traverseControls((ViewGroup) child, position); // Recursive
// call.
if (invalid != null) {
break;
}
}
if (eventMappers != null) {
if (eventMappers.containsKey(child.getId())) {
final View vw = child;
final int resID = child.getId();
final int pos = position;
child.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
eventMappers.get(resID).OnClick(pos, vw);
}
});
}
}
}
return invalid;
}
}
另请注意,我还没有测试过它的性能,所以在使用大量行时要小心。
您可以扩展ArrayAdapter并覆盖getView()。像这样的一些更深入的 ListView 教程似乎也很有帮助。