Hello,
I tried to have a customed listview with the itemclick listener. I
followed with some small modifications the tutorials from
http://www.androidpeople.com/android-custom-listview-tutorial-example/.
If I put in comment "adapter.notifyDataSetChanged();", all work well:
when I click on an item, the listener is fired. But if I use
"adapter.notifyDataSetChanged();" (I need it because my list will be
constructed dynamically), no listener is fired when I click on any
item.
Does someone have an idea about the error I made ? Or shall I use
another way to do what I want ?
Thanks !
Here are my codes:
public class CustomListViewOnClick extends Activity {
private CustomListView l1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
l1 = new CustomListView(this);
setContentView(l1);
}
}
public class CustomListView extends ListView {
private static Context context;
private EfficientAdapter adapter;
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context, int
textViewResourceId) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return country.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup
parent) {
ViewHolder holder;
if (convertView == null) {
convertView =
mInflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView
.findViewById(R.id.TextView02);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(curr[position]);
holder.text2.setText(country[position]);
return convertView;
}
class ViewHolder {
TextView text;
TextView text2;
}
}
public CustomListView(Context context) {
super(context);
CustomListView.context = context;
adapter = new EfficientAdapter(context, R.layout.listview);
setAdapter(adapter);
setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int
position,
long id) {
AlertDialog.Builder adb;
adb = new
AlertDialog.Builder(CustomListView.context);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "
+ getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
adapter.notifyDataSetChanged();
}
private static final String[] country = { "Iceland", "India",
"Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos",
"Latvia",
"Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
"Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR",
"IQD",
"EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ",
"LRD", "LYD",
"LTL ", "EUR"
};
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en