Patrick260284
Patrick260284

Reputation: 89

android how to sync onitemclicklistener listview id and database id

this is a succesor to this question

At first everything worked fine but after 2 deletes the misery starts. The database entries start with ID 1, then 2 and so on. Lets say I have three database entries. The listview IDs for these three entries are 2,1 and 0. The database entries are 1,2 and 3. 1 and 2 will be deleted with the onitemclicklistener but since the listview doesn´t have an ID 3, the corresponding database entry will never ever be deleted.

So the question is, how can I "sync" those two IDs?

Listview-Class

public class anzeigen extends AppCompatActivity {

    private static final String TAG = anlegen.class.getSimpleName();
    private static final String FILENAME = TAG + ".kdf";
    private List valueList = new ArrayList<String>();
    final VorgangDataSource dataSource = new VorgangDataSource(this);


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

        Log.d(TAG,"Die Datenquelle wird geöffnet!");
        dataSource.open();

        final List<vorgangsdaten> vorgangsdatenList = dataSource.getAllVorgangsDaten();

        final ArrayAdapter<vorgangsdaten> VorgangArrayAdapter = new ArrayAdapter<>(
                this,
                R.layout.mylistlayout,
                vorgangsdatenList
        );

        final ListView lv = (ListView)findViewById(R.id.listView);
        lv.setAdapter(VorgangArrayAdapter);
        lv.setItemsCanFocus(false);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s_id = String.valueOf(id);
                Log.d(s_id,"id_in_onitem");

                String p_id = String.valueOf(position);
                Log.d(p_id,"position_on_item");

                int pos = position;
                vorgangsdatenList.remove(pos);
                dataSource.deleteRow(id);
                VorgangArrayAdapter.notifyDataSetChanged();
            }
        });


        //ActionBar Costumization
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        ab.setIcon(R.drawable.search1);
        ab.setDisplayShowHomeEnabled(true);
        ab.setDisplayUseLogoEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dataSource.close();
    }
}

deleteRow-Method:

public void deleteRow(long id){

        String s_id;
        s_id = String.valueOf(id);

        Log.d(s_id,"id_value");

        database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] {s_id});

        long deleteID = database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] {s_id});
        String s_del = String.valueOf(deleteID);
        Log.d(s_del,"delete_row_value");

    }

If you need more code just let me know :)

Upvotes: 0

Views: 197

Answers (2)

Patrick260284
Patrick260284

Reputation: 89

After trying Bonattis answer and experimenting a little bit I found another answer.

deleteRow-Method

public void deleteRow(long id){

        String s_id;                    //String for long id
        s_id = String.valueOf(id);      //assign String value of id
        String s_rowId = null;          //String for rowid
        long rowId = 0;
        long deleteId = 0;
        String s_deleteId = null;

        Log.d(s_id,"id_value");

        Cursor cursor = database.query(VorgangDbHelper.TABLE_VORGAENGE_LIST,columns,null,null,null,null,null);

        if(cursor.moveToFirst()) {
            s_rowId = cursor.getString(cursor.getColumnIndex(VorgangDbHelper.COLUMN_ID));
            rowId = Long.parseLong(s_rowId);
            deleteId = rowId + id;
            s_deleteId = String.valueOf(deleteId);
            Log.d(s_rowId,"rowID");
            Log.d(s_deleteId,"deleteID");
        }

        database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] {s_deleteId});
    }

How does it work now?

So the deleteRow-Method(dRM) gets the id of the first database entry (s_rowId) and converts it to long (rowId). Now I take the long id passed from the listview and add it to rowId which is my deleteId. With this I have the correct database value to what I clicked in the listview and can pass it over to database.delete

Upvotes: 0

Bonatti
Bonatti

Reputation: 2781

You have no synchronicity between your Database and the ListView items.

When selecting the data, select the ID from the table, and keep it in memory as well.

Since vorgangsdaten looks german or sweden, I am having trouble understanding the meaning of those words. The ListView has its own "id" for each item, to keep a list of them shown, and the rest "hidden". What you must do is have an Object with details that are syncronized with the database, and when a list item is clicked, the "list id" is used to query an Object and delete from the database.

An example would be:

ArrayList<MyObject> arrayListData = new ArrayList<>();
ArrayList<String> arrayListNames = new ArrayList<>();
MyObject stuffs = database.query(); // In here, fetch the data
/* I am simplifing the MyObject to [String name, String aValue, int aNumber] */
for(MyObject s: stuffs){
   arrayListData.add(s);
   arrayListNames.add(s.name);
}
arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                arrayListNames
        );

listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        MyObject thing = arrayListData.get(position);
                        database.delete(thing.aNumber);
                    }
                }
        );
listView.setAdapter(arrayAdapter);

Upvotes: 1

Related Questions