samedi 3 octobre 2015

how to push new values in ListView with SQlite in a diffrent activity

I have a main activity that has listview with a name(person/friend) and number(debt)

what i want to do is when you click a name it will move to another activity to show all history of number inputs that same name had, so when i update the number and enter the new activity it should show the initial number in first row of listview and the number i updated in the second row, and for that i even made a second table that always adds and doesn't update.

what happens is the second line does get added in when i update but then both lines are the same number (the updated number) and of course i checked the database table and it does show the number's correctly as i entered them, but doesn't matter how many numbers i input, all lines in the ListView will show the same number which is the last number i updated for that name.

i am passing the values in an intent, so maybe its not the right method to do it but i don't know any other way

here is the relevant code: first from the main activity where i pass the data:

private void listViewItemClick() {
    ListView listView = (ListView) findViewById(R.id.my_debts_list);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(MainActivity.this, ViewFriend.class);
            Cursor c = (Cursor) parent.getItemAtPosition(position);
            intent.putExtra(NAME_EXTRA, c.getString(c.getColumnIndex(String.valueOf(DBAdapter.KEY_NAME))));
            intent.putExtra(DEBT_EXTRA, c.getString(c.getColumnIndex(String.valueOf(DBAdapter.KEY_DEBT))));
            intent.putExtra(DATE_EXTRA, c.getString(c.getColumnIndex(String.valueOf(DBAdapter.KEY_DATE))));
            intent.putExtra(LABEL_EXTRA, c.getString(c.getColumnIndex(String.valueOf(DBAdapter.KEY_LABEL))));

            startActivity(intent);
        }
    });
}

now the second activity with the listview that it is passed to:

//populate ListView
public void populateView(String name){
    Cursor cursor = myDb.getByName(name);
    customAdapter = new ViewCursorAdapter(ViewFriend.this,cursor,0);
    ListView listView = (ListView)findViewById(R.id.recent_debt_list);
    listView.setAdapter(customAdapter);
}

public class ViewCursorAdapter extends CursorAdapter {

    private LayoutInflater cursorInflater;

    public ViewCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);

        cursorInflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {

        TextView debt = (TextView) view.findViewById(R.id.debt_amount);
        TextView date = (TextView) view.findViewById(R.id.date_inserted);
        TextView label = (TextView) view.findViewById(R.id.debt_label);

        String fLabel = getIntent().getStringExtra(MainActivity.LABEL_EXTRA);
        String fDate = getIntent().getStringExtra(MainActivity.DATE_EXTRA);
        String fDebt  = getIntent().getStringExtra(MainActivity.DEBT_EXTRA);

        debt.setText(fDebt);
        label.setText(fLabel);
        date.setText(fDate);

        String debtColor = debt.getText().toString();

        if (debtColor.charAt(0) == '+'){
            debt.setTextColor(Color.parseColor("#00A802"));
        }else {
            debt.setTextColor(Color.parseColor("#FF0000"));
        }

    }

now the method that gets the relevant rows for that person name from the database helper:

public Cursor getByName(String name) {
    Cursor cursor =     db.rawQuery("select * from " + FRIEND_TABLE + " where " + KEY_NAME + "='" + name + "'" , null);

    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

Aucun commentaire:

Enregistrer un commentaire