以编程方式添加TextView旁边的按钮

时间:2013-01-13 01:30:50

标签: android listview textview

我有一个国家TextViews的ListView,我在其中一个TextViews上点击一下它会删除按下的textview后我做了一个OptionsMenu项目。 我想知道当我按“删除国家”项目时是否有办法做到这一点 每个TextView旁边都会出现一个按钮,如iphone示例中所示 delete

ListView由适配器创建。 我不需要整个答案只是它可以如何完成的概念(从Optionsmenu中选择删除国家后,删除每个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旁边添加删除按钮(或其他内容),所有这些按钮将应用相同的方法(删除)

2 个答案:

答案 0 :(得分: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
    }
});
    }
});

答案 1 :(得分: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. 在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;
    }