我有一个国家 TextViews 的 ListView,并且在单击其中一个 TextViews 上的短按后我做了一个 OptionsMenu 项,它将删除按下的 textview。我想知道是否有办法做到这一点,当我按下“删除国家”项目时,每个 TextView 旁边都会出现一个按钮,就像在 iphone 示例中一样
ListView 是由适配器创建的。我不需要完整的答案,只需要如何完成的概念(从选项菜单中选择删除国家/地区后,删除每个 TextView 旁边创建的按钮)。
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 + ")");
}
public void delete(int position) {
int mDeletedId = mVector.get(position).getmCountryIndex();
mVector.remove(position);
mDb.execSQL("DELETE FROM countries WHERE id ="+mDeletedId);
}
public void ChangeTextSize(int textSize, int mIndex) {
System.out.println(textSize);
System.out.println(mIndex);
mDb.execSQL("update COUNTRIES set font = " + textSize + " where id =" + mIndex);
}
}
我希望当我从 optionMenu“删除国家/地区”单击时,它将以编程方式在每个 TextView 旁边添加删除按钮(或其他东西),并且所有这些按钮都将应用相同的方法(删除)