lundi 2 mars 2015

Get correct id for checkbox for update value in SQlite

I have trouble getting right on my checkbox . My problem is that I do not get the correct id from the database and therefore do not know the ID / series I will update the database. I have tried various suggestions here , but nothing would work. For the most part , I notice that the index is out of ...


When I used ArrayList to collect information, it was easy. I just used int position from my View to get the id...


I deleted all my attempts , therefore , there is currently no direct code that tries to solve the problem.


Thanks in advance



**CustomAdapter.java**

package com.example.com.hermods.shoppinglistwithdb;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

public class CustomAdapter extends CursorAdapter {

DatabaseHandler DB_ADAPTER;
long dbID;
CheckBox chkbox;

public CustomAdapter(DatabaseHandler DB_ADAPTER, Context context, Cursor cursor) {
super(context, cursor, 0);

this.DB_ADAPTER = DB_ADAPTER;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
dbID = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String amount = String.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow("amount")));
int done = cursor.getInt(cursor.getColumnIndexOrThrow("done"));

TextView titleView = (TextView)view.findViewById(R.id.itemTitle);
titleView.setText(title);

TextView amountView = (TextView)view.findViewById(R.id.itemAmount);
amountView.setText(String.valueOf(amount) + " " + context.getString(R.string.piece));

chkbox = (CheckBox)view.findViewById(R.id.itemDone);
chkbox.setChecked(done != 0);

titleView.setFocusable(false);
titleView.setFocusableInTouchMode(false);
}

@Override
public View newView(Context context, Cursor arg1, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_row, parent, false);
}


}


List_Fragment.java



public class List_Fragment extends ListFragment {

ShowDetail_Fragment showDetail;
CustomAdapter adapter;
DatabaseHandler DB_ADAPTER;

public List_Fragment(DatabaseHandler DB_ADAPTER) {
this.DB_ADAPTER = DB_ADAPTER;
}

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

View root = inflater.inflate(R.layout.list_layout, container, false);


return root;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long idDB) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setMessage(R.string.remove);
dialog.setPositiveButton(R.string.remove, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((MainActivity)getActivity()).dbRemoveRow(idDB);
((MainActivity)getActivity()).buildList();
}
});

dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog.create();
dialog.show();

return true;
}
});
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

Cursor item = ((MainActivity)getActivity()).dbGetRow(id);

showDetail = new ShowDetail_Fragment(item, id);
FragmentTransaction FT = getFragmentManager().beginTransaction();
FT.replace(R.id.layout, showDetail);
FT.addToBackStack(null);
FT.commit();
}

Aucun commentaire:

Enregistrer un commentaire