2013-01-13 128 views
0

我有一個ListView的國家的TextViews,我做了一個OptionsMenu項目後點擊一個TextViews短按它將刪除按下的textview。 我想知道是否有辦法做到這一點,當我按項目「Delete國家」 在iphone例如 delete以編程方式在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的,所有這些按鈕將採用同樣的方法(刪除)

回答

1
  1. 在XML文件中創建按鈕:

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <Button 
        android:id="@+id/row_category_button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Add" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="10dp" 
        android:layout_alignLeft="@+id/row_category_name" 
        android:textColor="@color/black" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" /> 
    </RelativeLayout> 
    
  2. 創建視圖支架和適配器類添加按鈕:

    ViewHolder holder ; 
    
    if(view ==null){ 
         view = inflater.inflate(R.layout.___, null); 
         holder = new ViewHolder(); 
         holder.tv = (TextView) view.findViewById(R.id.____); 
         holder.img =(ImageView) view.findViewById(R.id.______); 
         holder.btn =(Button) view.findViewById(R.id.button); 
         view.setTag(holder); 
        } 
    
    class ViewHolder{ 
        TextView tv; 
        ImageView img; 
        Button btn; 
    } 
    
2

而不是嘗試以編程方式添加按鈕,並使混亂,試試這個。

我假設您使用ListView的自定義數組適配器。將該按鈕添加到該行並將其對齊到文本視圖的右側。

使用定義行佈局的xml文件中的android:visibility="gone"使刪除按鈕不可見。

textView1.setOnLongClickListener(new OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 
     deleteButton.setVisibility(true); 
deleteButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public boolean onClick(View v) { 
     //delete the row here 
    } 
}); 
    } 
}); 
+0

的問題是,我從其他活動動態創建行以獲得結果我添加更多文本視圖我怎樣才能ake刪除按鈕,將自動適合每個文本視圖的需求,我的意思是在每一行.. –

+1

@YonatanBalas爲什麼你想要動態創建行?爲什麼你不能創建一個row.xml文件並將其分配給適配器。讓生活變得更簡單。如果您仍然希望按照您的方法行事,那麼您必須爲行添加相對佈局,然後添加文本視圖和按鈕。 –

+0

是的,我明白你的意思,我們剛剛在課堂上學到了這一點,我的老師給出了你的答案,感謝隊友:] –

相關問題