0

ListViewからTextViewを削除する最善の方法を知りたいのですが、オプションメニューから削除したいと思います。だから私は「国を削除」をクリックします-それは私がタップされた国を削除するよりも国をタップするまで待ちます。私はプログラミングに不慣れです。前もって感謝します

public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){

case R.id.omAddCountry:
    Intent addCountryIntent = new Intent(MainActivity.this, AddCountryActivity.class);
    startActivityForResult(addCountryIntent, 11);
    break;

case R.id.omDeleteCountry:

    break;

ListViewはSQLiteを使用しており、DBから最初のビューを取得し、TextViewsはアダプターからのベクターによって追加されます。

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 + ")");
}

}

4

1 に答える 1

2

グローバルブール値を作成します。

boolean isDeleting = false;

次に、でonOptionsItemSelected()、実行します。

case R.id.omDeleteCountry:
isDeleting = true;
break;

そして、あなたが実装するところはどこでもonListItemClick()

@Override
public void onListItemClick (ListView listView,View view, int pos, long id)
{
  if (isDeleting){
    yourCustomAdapter.delete(pos)
    yourCustomAdapter.notifyDataSetChanged();
    isDeleting = false;
  }
  else {
    //do other stuff
  }
}

delete()アダプタでメソッドを作成する必要があります。

于 2013-01-12T22:43:36.893 に答える