0

I"m tring it refresh my listview when my sqllite database is change (when delete or update query). the querys itself works just fine, but it doesn't update the listview layout, only when i"m exiting the acitvity and renter it the liseview is changing.

I tried the methodes:

  1. notifyDataSetChanged()
  2. requery()

the code of the activiy is:

public class ShowListActivity extends ListActivity {


    private ItemsDataSource itemsDataSource;
    private String source[] = new String[] {MySQLiteHelper.KEY_NAME, MySQLiteHelper.KEY_QUANTITY, MySQLiteHelper.KEY_CHECKED};
    private int dest[] = new int[] {R.id.itemTitle, R.id.itemQuantity, R.id.itemCheck};


    public void goBackMethod(View view) {
        Intent intent = new Intent(this, MainScreen.class);
        startActivity(intent);  
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.activity_show_list);
        } catch (Exception e) {
            e.getMessage();
        }


        ApplicationController applicationController = (ApplicationController)getApplicationContext();
        itemsDataSource = applicationController.itemsDataSource;


        final Cursor mCursor = itemsDataSource.getAllItems();
        startManagingCursor(mCursor);


        CustomCursorAdapter adapter = new CustomCursorAdapter(this, mCursor);
        adapter.notifyDataSetChanged();
        setListAdapter(adapter);

      ListView listView = getListView();

      listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
                selectAction(id);
            }
        });
    }

    private void selectAction(final long position) {
        Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("בחר פעולה");
        builder
                .setMessage("בחר בפעולה שברצונך לבצע:");
        builder.setPositiveButton("עדכן פריט קניה",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                       //do update
                    }
                });

        builder.setNeutralButton("מחק פריט קניה",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         itemsDataSource.deleteItem(position);
                         Toast toast = Toast.makeText(getBaseContext(), "הפריט הנבחר נמחק בהצלחה", Toast.LENGTH_SHORT);
                         toast.show();               
                    }
                });

        builder.setNegativeButton("חזור לרשימת הקניות",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();

                    }
                });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();       
    }
}

the code of the customadapter is:

public class CustomCursorAdapter extends CursorAdapter implements Adapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;


public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c);
    mInflater=LayoutInflater.from(context);
    mContext=context;
}

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

    TextView itemTitle= (TextView)view.findViewById(R.id.itemTitle);
    itemTitle.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)));

    TextView itemQuantity = (TextView)view.findViewById(R.id.itemQuantity);
    itemQuantity.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY)));

    CheckBox itemCheck = (CheckBox) view.findViewById(R.id.itemCheck);
    itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED))==1);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view=mInflater.inflate(R.layout.listview_item_row, parent, false);

    TextView itemTitle= (TextView)view.findViewById(R.id.itemTitle);
    itemTitle.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)));

    TextView itemQuantity = (TextView)view.findViewById(R.id.itemQuantity);
    itemQuantity.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY)));

    CheckBox itemCheck = (CheckBox) view.findViewById(R.id.itemCheck);
    itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED))==1);

    return view;
}

}

Shimon Geld
  • 89
  • 11

1 Answers1

0

in your code you have not added your adapter class. I am sure that you will be adding the data in list view from an array list.

So in the time of updating the list view by adding or removing a data to the list view, first either add or remove the data from your array list and the call as follows

listView.invalidateViews();
then call your set Adapter method
Siva K
  • 4,968
  • 14
  • 82
  • 161
  • ok after updating the data either adding or removing try this, listView.invalidateViews(); CustomCursorAdapter adapter = new CustomCursorAdapter(this, mCursor); adapter.notifyDataSetChanged(); setListAdapter(adapter); – Siva K Oct 28 '12 at 09:40
  • doesn't work, there is an error on the line: CustomCursorAdapter adapter = new CustomCursorAdapter(this, mCursor) he want me to create a onclick lisnner inside the contsructor of the CustomCursorAdapter – Shimon Geld Oct 28 '12 at 10:01