0

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

我尝试了以下方法:

  1. notifyDataSetChanged()
  2. 重新查询()

活动的代码是:

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

}

4

1 回答 1

0

在您的代码中,您尚未添加适配器类。我确信您将从数组列表中添加列表视图中的数据。

因此,在通过向列表视图添加或删除数据来更新列表视图时,首先从数组列表中添加或删除数据,然后调用如下

listView.invalidateViews();
then call your set Adapter method
于 2012-10-28T09:24:51.720 回答