关于listView的绑定列表的咨询

时间:2012-10-25 12:10:15

标签: android

我从我的WebService获取数组。

我得到了这个:

WS w=new WS();
 w.WebServiceCallExampleGetArray();

返回此内容:List<Category>

我有listView ListView list;

如何将 w.WebServiceCallExampleGetArray()绑定到 listView

2 个答案:

答案 0 :(得分:0)

您可以延长ArrayAdapter并覆盖getView()。一些更深入的ListView教程(如this)似乎也很有帮助。

答案 1 :(得分:0)

我编写了一个通用类来将列表视图绑定到可能对您有用的列表。你可以随心所欲地扩展这个想法。 首先,我假设您接受我的建议并使用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;
}
}

另请注意,我没有对其性能进行测试,因此请谨慎使用大量行。