在android中刷新Listview

时间:2012-10-28 09:06:19

标签: android-listview

当我的sqllite数据库发生变化时(删除或更新查询时)我刷新了我的列表视图。 查询本身工作正常,但它不会更新列表视图布局,只有当我退出活动并租用它时,liseview正在改变。

我尝试了这些方法:

  1. notifyDataSetChanged()
  2. 重新查询()
  3. 活动的代码是:

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

    customadapter的代码是:

    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 个答案:

答案 0 :(得分:0)

在您的代码中

您没有添加适配器类。我确信您将从列表视图中添加数据。

因此,在通过向列表视图添加或删除数据来更新列表视图时,首先要添加或删除数组列表中的数据以及调用,如下所示

listView.invalidateViews();
then call your set Adapter method