Refresh Listview in android

与世无争的帅哥 提交于 2019-12-24 21:17:53

问题


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;
}

}


回答1:


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


来源:https://stackoverflow.com/questions/13107569/refresh-listview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!