android is new for me. I am trying to develop a program on 1.5 platform but still in progress, plz guide me.
I have some information in following format
Solution you choose strictly depends on your needs. I will try to gather requirements and provide you with implementation which I think is the most suitable.
Ok, so lets enumerate some requirements:
Things worth to mention:
- If you have custom data that you need to display, and it's not just vector of strings, then good idea is to provide your own type for data. It's quite convenient to keep all row data in one place, even if some of the data is not displayed on list. Reflecting to your example: you have "item" and "description" - you may want to display just "item" but you want to have a possibility to get the description. Keep that in instance of one class. In example below it is RowData class. Please see RowData class.
- In case you want a custom layout, and it's not just a TextView, then you should implement your own adapter - probably the subclass of ArrayAdapter. Please take a look at CustomAdapter class.
- If you need to filter the list and you are using custom data type - provide toString() method for your type. Thanks to this method you will be able to use build-in Filter class. Its responsibility is to filter out items from the list that doesn't match text you entered. It just take text representation of items from your adapter, and use it with the filter. Please see the toString() method from RowData class.
- If list of items can be long, the good idea would be to reuse row views and use wrapper pattern. See getView() method and ViewHolder class.
public class CustomList extends ListActivity {
private LayoutInflater mInflater;
private Vector data;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector();
RowData rd = new RowData("aaa", "description1");
data.add(rd);
rd = new RowData("bbb", "description2");
data.add(rd);
rd = new RowData("ccc", "description3");
data.add(rd);
CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_row,R.id.item, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
RowData row = adapter.getItem(position);
Builder builder = new AlertDialog.Builder(this);
builder.setTitle(row.mItem);
builder.setMessage(row.mDescription + " -> " + position );
builder.setPositiveButton("ok", null);
builder.show();
}
/**
* Data type used for custom adapter. Single item of the adapter.
*/
private class RowData {
protected String mItem;
protected String mDescription;
RowData(String item, String description){
mItem = item;
mDescription = description;
}
@Override
public String toString() {
return mItem + " " + mDescription;
}
}
private class CustomAdapter extends ArrayAdapter {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//widgets displayed by each item in your list
TextView item = null;
TextView description = null;
//data from your adapter
RowData rowData= getItem(position);
//we want to reuse already constructed row views...
if(null == convertView){
convertView = mInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//
holder = (ViewHolder) convertView.getTag();
item = holder.getItem();
item.setText(rowData.mItem);
description = holder.getDescription();
description.setText(rowData.mDescription);
return convertView;
}
}
/**
* Wrapper for row data.
*
*/
private class ViewHolder {
private View mRow;
private TextView description = null;
private TextView item = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView getDescription() {
if(null == description){
description = (TextView) mRow.findViewById(R.id.description);
}
return description;
}
public TextView getItem() {
if(null == item){
item = (TextView) mRow.findViewById(R.id.item);
}
return item;
}
}
}
Custom item layout:
<TextView android:text="text" android:id="@+id/item"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button" android:paddingRight="10dip"
android:paddingLeft="10dip"></TextView>
<TextView android:text="text" android:id="@+id/description"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@+id/item" android:paddingLeft="10dip"
android:paddingRight="10dip"></TextView>