i would like to know the best possible way do delete a TextView from ListView,
But i want to do it from Options Menu.
so i click “Delete Country” – it will wait untill i will tap a country than delete the tapped country.
i am new to programming. thanks in advance
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.omAddCountry:
Intent addCountryIntent = new Intent(MainActivity.this, AddCountryActivity.class);
startActivityForResult(addCountryIntent, 11);
break;
case R.id.omDeleteCountry:
break;
the ListView is using SQLite and it gets the first view from DB and the TextViews is added by an Vector from an adapter.
public class CountryAdapter extends BaseAdapter {
private Context mContext;
protected Vector<Country> mVector;
protected SQLiteDatabase mDb;
public void setmContext(Context mContext){
this.mContext = mContext;
}
public CountryAdapter(Context mContext){
this.mContext = mContext;
mVector = new Vector<Country>();
CountryOpenHelper helper = new CountryOpenHelper(mContext);
mDb = helper.getWritableDatabase();
Cursor cursor = mDb.rawQuery("SELECT * FROM COUNTRIES", null);
if(cursor.getCount() > 0){
cursor.moveToFirst();
}
do {
Country country = new Country();
country.setmCountryIndex(cursor.getInt(0));
country.setmCountryName(cursor.getString(2));
country.setmCountryTextSize(cursor.getInt(1));
country.setmCountryColor(cursor.getInt(3));
mVector.add(country);
} while (cursor.moveToNext());
}
public Vector<Country> getmVector() {
return mVector;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mVector.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView == null){
tv = new TextView(mContext);
}else{
tv = (TextView) convertView;
}
tv.setText(mVector.get(position).getmCountryName());
tv.setTextColor(mVector.get(position).getmCountryColor());
tv.setTextSize(mVector.get(position).getmCountryTextSize());
return tv;
}
public void ChangeColor(int newcolor, String name) {
mDb.execSQL("update COUNTRIES set color = " + newcolor + " where name = '" + name + "' " );
}
public void addCountry(int mId, String myCountry, int myColorNum){
mDb.execSQL("insert into countries values(" + mId + " , ' " + myCountry+"' , "+ myColorNum + ")");
}
}
Make a global boolean:
then in
onOptionsItemSelected(), do:And whereever you implement
onListItemClick():You will have to make a
delete()method in your adapter.