J'ai écrit une classe générale pour lier une vue de liste à une liste qui pourrait vous être utile. vous pouvez étendre l'idée comme vous le souhaitez. Tout d'abord, je suppose que vous suivez mes conseils et utilisez un ORM. Par exemple, Imaginez que vous ayez une liste d'éléments de PointClass et un ListView nommé (mylistView) et que vous souhaitiez lier cette classe à une vue de liste à l'aide de la commande List. suivant le style de ligne (partie intéressante est 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>
L'utilisation ListViewAdapter classe (code est en-dessous), c'est juste deux lignes de code:
listadapter = new ListViewAdapter(ActivityDailyRecord.this,items,R.layout.listrowitems);
mylistView.setAdapter(listadapter);
La partie intéressante est tag = "Bind = {FieldName}" ... elle parcourt l'ensemble de la présentation des listrowitems et définit les valeurs des champs en utilisant la réflexion. vous devriez juste faire attention aux noms de champs dans la balise du fichier 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;
}
}
Notez également que je ne l'ai pas testé pour la performance, alors faites attention à l'utiliser avec un grand nombre de lignes.
Avez-vous essayé quelque chose? – Korcholis
oui, j'essaie ceci: list.setAdapter (new ArrayAdapter (this, android.R.layout.simple_list_item_1, w.WebServiceCallExampleGetArray())); –
Gali
vous voudrez enregistrer une référence à ce ArrayAdapter. – Shark