Reputation: 35
I have a ListView of TextViews of countries and I did a OptionsMenu item after clicking a short press on one of the TextViews it will delete the pressed textview. I would like to know if there is a way to do that when I press the item "Delete Country" a button will appear next to every TextView like in iphone example
the ListView is created by an Adapter. I don't need the whole answer just the concept of how can it be done (delete button that is created next to every TextView after you choose from Optionsmenu to delete a country).
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);
}
}
I want when i click from optionMenu "Remove Country" it will programmatically add delete button(or something else) next to every TextView and all those buttons will apply the same Method(delete)
Upvotes: 0
Views: 1499
Reputation: 61
Create Button in XML file:
<?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>
Create a View holder and Add button in the Adapter class:
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;
}
Upvotes: 2
Reputation: 4383
Instead of trying to add the button programmatically and making a mess, try this.
I'm assuming your using a custom Array Adapter for the ListView. Add the button to the row and align it to the right of the text view.
Make the delete button invisible with android:visibility="gone"
in the xml file defining the row layout.
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
}
});
}
});
Upvotes: 2