Android
custom SimpleAdapter
venustas
2011. 11. 17. 16:16
Layout
<?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="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/listRow"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<TextView android:id="@+id/writeDate"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/title"
android:paddingLeft="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Java
private class ListAdapter extends SimpleAdapter{
private ArrayList<HashMap<String, String>> arrayList;
private int resource; // Layout ID
public ListAdapter(Context context,
ArrayList<HashMap<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
this.arrayList = data;
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(resource, null);
}
LinearLayout listRow = (LinearLayout)convertView.findViewById(R.id.listRow);
TextView writeDate = (TextView)convertView.findViewById(R.id.writeDate);
TextView title = (TextView)convertView.findViewById(R.id.title);
listRow.setTag(arrayList.get(position).get("link"));
listRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String link = (String)v.getTag();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
}
});
writeDate.setText(arrayList.get(position).get("writeDate"));
title.setText(arrayList.get(position).get("title"));
return convertView;
}
}