我应该在哪里调用数据库中的方法来删除listView的项目?

时间:2015-06-14 23:59:35

标签: android database listview

我需要帮助从listView和数据库中删除项目。我在这里看到了类似于我的一些问题,但是我无法从他们的答案中做任何事情,因为我是新手并仍然抓住这一切。

顺便说一句,listView是片段,这里是片段活动代码的一部分:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_moj_spisak, container, false);
    final EditText unos = (EditText)view.findViewById(R.id.editText1);
    final Button dodajDugme = (Button)view.findViewById(R.id.button1);
    final ListView lv = (ListView)view.findViewById(R.id.listView1);
    final DodajProizvodListAdapter dpl = new DodajProizvodListAdapter(getActivity());

    dpl.addAll(DBHelper.getConnection(getActivity()).getAllProizvodi());

    lv.setAdapter(dpl);

    dodajDugme.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            DBHelper.getConnection(getActivity())
            .saveProizvod(unos.getText().toString());

            ArrayList<String> allProizvodi = DBHelper.getConnection(getActivity()).getAllProizvodi();

            dpl.addAll(allProizvodi);

            unos.setText("");

            Toast.makeText(getActivity(), "Proizvod je dodat", Toast.LENGTH_SHORT).show();
        }
    });


    return view;


}   

}

以下是适配器代码的一部分:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.dodaj_proizvod_list_item, parent, false);
    }

    ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);
    iv.setTag(position);
    iv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Integer index = (Integer) v.getTag();
            lv.remove(index.intValue());
            notifyDataSetChanged();

        }
    });

    final CheckedTextView ctv = (CheckedTextView)convertView.findViewById(R.id.textView1);
    ctv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (ctv.isChecked())

                ctv.setChecked(false);

            else

                ctv.setChecked(true);


        }
    });
    ctv.setText(lv.get(position));
    return convertView;
}

}

这是来自dataBase的代码:

public class DBHelper extends SQLiteOpenHelper  {
    private static final String DATABASE_NAME = "proizvod.db";
    private static final int DATABASE_VERSION = 1;

    private final String TABLE_PROIZVODI = "proizvodi";

    private final String COLUMN_ID = "id";
private final String COLUMN_PROIZVOD = "proizvodi";

private static DBHelper dbh;

private DBHelper(Activity activity) {
    super(activity, DATABASE_NAME, null, DATABASE_VERSION);
}

public static synchronized DBHelper getConnection(Activity activity) {
    if (dbh == null)
        dbh = new DBHelper(activity);
    return dbh;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String upitZaPravljanjeBaze = 
            "CREATE TABLE "+TABLE_PROIZVODI+"("+COLUMN_ID+" INTEGER PRIMARY KEY, "+COLUMN_PROIZVOD+" TEXT);";       
    db.execSQL(upitZaPravljanjeBaze);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion) {
        db.execSQL("DROP TABLE" + "TABLE_PROIZVODI");
        onCreate(db);
    }

}

public ArrayList<String> getAllProizvodi() {
    ArrayList<String> toRet = new ArrayList<String>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM " + TABLE_PROIZVODI, null);
    c.moveToFirst();

    while(c.isAfterLast() == false) {
        toRet.add(c.getString(c.getColumnIndex(COLUMN_PROIZVOD)));
        c.moveToNext();

    }

    return toRet;
}

public long saveProizvod(String proizvod) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_PROIZVOD, proizvod);
    return db.insert(TABLE_PROIZVODI, null, cv);
}

public void deleteByName(String name) {          
       SQLiteDatabase db= this.getWritableDatabase();
       db.delete(TABLE_PROIZVODI, COLUMN_ID +"=?", new String [] { name });
       db.close();
    }

1 个答案:

答案 0 :(得分:0)

  

<强> Activity.java

type "c:\file" | findstr /i /e /c:"=x" >part1
type "c:\file" | findstr /i /b /c:"2525" >part2
  

CustomAdapter.java

    ListView userList;
 UserCustomAdapter userAdapter;
 ArrayList<User> userArray = new ArrayList<User>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  /**
   * add item in arraylist
   */
  userArray.add(new User("Mumer", "Spain", "Spain"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  /**
   * set item into adapter
   */
  userAdapter = new UserCustomAdapter(MainActivity.this, R.layout.row,
    userArray);
  userList = (ListView) findViewById(R.id.listView);
  userList.setItemsCanFocus(false);
  userList.setAdapter(userAdapter);
  /**
   * get on item click listener
   */
  userList.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View v,
     final int position, long id) {
    Log.i("List View Clicked", "**********");
    Toast.makeText(MainActivity.this,
      "List View Clicked:" + position, Toast.LENGTH_LONG)
      .show();
   }
  });

 }