I have here a baseadapter based on what I saw in the API demos. I want to dynamically add/remove items from the list, in this case adding from an intent called by a button, and removing from clicking on an imageview in the listview. And in this case, I would be adding/removing items from DATA[]. I have looked around for various AddItem() and or Remove() methods on SO and google, but have not really come up with much for this situation. Any help would be great. Here is the code:
public class myActivity extends Activity{
private static final int CONTACT_PICKER_RESULT = 1001;
private static final String TAG = myActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new myAdapter(this));
Button bAdd = (Button)findViewById(R.id.bAdd);
bAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
........xxxx....xxxx....
}
public class myAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public myAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return DATA.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row,null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.txtTitle);
holder.icon = (ImageView) convertView.findViewById(R.id.imgIcon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[position]);
holder.icon.setImageResource(android.R.drawable.ic_delete);
return convertView;
}
}
static class ViewHolder {
TextView text;
ImageView icon;
}
//this will not be hard-coded, jsut included for clarity
private static final String[] DATA ={
"one","two","three" };