我有一個ListView的國家的TextViews,我做了一個OptionsMenu項目後點擊一個TextViews短按它將刪除按下的textview。 我想知道是否有辦法做到這一點,當我按項目「Delete國家」 在iphone例如 以編程方式在TextView旁邊添加按鈕
ListView控件通過適配器創造未來的每一個TextView的像一個按鈕就會出現。 我不需要整個答案只是它怎麼做的概念(刪除按鈕,在您從Options菜單中選擇刪除國家後,在每個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的,所有這些按鈕將採用同樣的方法(刪除)
的問題是,我從其他活動動態創建行以獲得結果我添加更多文本視圖我怎樣才能ake刪除按鈕,將自動適合每個文本視圖的需求,我的意思是在每一行.. –
@YonatanBalas爲什麼你想要動態創建行?爲什麼你不能創建一個row.xml文件並將其分配給適配器。讓生活變得更簡單。如果您仍然希望按照您的方法行事,那麼您必須爲行添加相對佈局,然後添加文本視圖和按鈕。 –
是的,我明白你的意思,我們剛剛在課堂上學到了這一點,我的老師給出了你的答案,感謝隊友:] –