mercredi 9 décembre 2015

Delete item of database via ListView and id

I have an android app with a ListView and data of my local sql database. Now i would like to delete an item from my database with the ID

i would like to make an long press on any row, and this should delete the item.

 ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// DELETE ACTION
}

but how i get the item ID of my Database? At the moment i save with the query the ID in a hidden TextView and get this ID via findviewbyid().

but is this the correct way?

Adapter:

public class DatabaseListAdapter extends BaseAdapter {

Context context;
ArrayList<DatabaseListItems> databaseList;


public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
    this.context = context;
    databaseList = list;
}

@Override
public int getCount() {
    return databaseList.size();
}

@Override
public Object getItem(int position) {
    return databaseList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    DatabaseListItems databaseListItems = databaseList.get(position);


    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_listview_item, null);

    }

    TextView tvID = (TextView) convertView.findViewById(R.id.txtViewID);
    tvID.setText(databaseListItems.getID());

    return convertView;
}

}

Aucun commentaire:

Enregistrer un commentaire