0

我有一个国家 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 旁边添加删除按钮(或其他东西),并且所有这些按钮都将应用相同的方法(删除)

4

2 回答 2

2

与其尝试以编程方式添加按钮并弄得一团糟,不如试试这个。

我假设您为 ListView 使用自定义数组适配器。将按钮添加到行并将其与文本视图的右侧对齐。

android:visibility="gone"在定义行布局的 xml 文件中使删除按钮不可见。

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
    }
});
    }
});
于 2013-01-13T05:19:53.740 回答
2
  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. 在 Adapter 类中创建一个 View holder 和 Add 按钮:

    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;
    }
    
于 2013-05-21T00:32:32.567 回答