簡體   English   中英

notifyDataSetChanged(); 不刷新

[英]notifyDataSetChanged(); not refreshing

我想在每次按下帶有編輯文本中字符串的搜索按鈕時刷新列表視圖,但是列表沒有刷新。

這是我的MainActivity:

public class MainActivity extends ListActivity implements OnClickListener {
    private DataSource datasource;

    Button search;
    EditText searchEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        search = (Button) findViewById(R.id.bSearch);

        searchEt = (EditText) findViewById(R.id.etTextSearch);

        search.setOnClickListener(this);

        datasource = new DataSource(this);
        datasource.open();

        List<Recipe> values = datasource.getAllRecipe(Recipe.l);
        // List<Recipe> values = datasource.getAllRecipe();

        // use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter<Recipe> adapter = new ArrayAdapter<Recipe>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ArrayAdapter<Recipe> adapter = (ArrayAdapter<Recipe>) getListAdapter();
        Recipe title = null;
        switch (v.getId()) {

        case R.id.bSearch:
            try {
                String s = searchEt.getText().toString();
                Recipe.l = s;
                Log.i("1", Recipe.l + "");

            } catch (Exception e) {
                String error = e.toString();
                Dialog d = new Dialog(this);
                d.setTitle("Row Empty or ID not found");
                TextView tv = new TextView(this);
                tv.setText(error);
                d.setContentView(tv);
                d.show();
            }
            break;

        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        datasource.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        datasource.close();
        super.onPause();
    }
}

這是我的DAO中的方法:

public List<Recipe> getAllRecipe(String l) throws SQLException {
        List<Recipe> titles = new ArrayList<Recipe>();
        Cursor cursor = database.query(Helper.TABLE_RECIPES, allColumns,
                Helper.COLUMN_TAG + "='" + Recipe.l + "'", null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Recipe title = cursorToRecipe(cursor);
            titles.add(title);
            cursor.moveToNext();
        }
        // make sure to close the cursor
        cursor.close();

        return titles;
    }

    private Recipe cursorToRecipe(Cursor cursor) {
        Recipe title = new Recipe();
        title.setId(cursor.getLong(0));
        title.setTitle(cursor.getString(1));
        return title;
    }

這是從我的模型課:

public static String l = "chicken";

我的onclicklisteners在工作,按鈕在工作。。我檢查了DDMS中的日志,變量l正在更改。。唯一的問題是列表視圖沒有刷新。

由於將getAllRecipe()(將返回Recipe對象的列表)類型轉換為Recipe對象(而不是Recipe對象的列表),因此您將獲得類強制轉換異常。 嘗試將其更改為

title = (List<Recipe>) datasource.getAllRecipe(Recipe.l);

有一種更好的方法可以解決您要達到的目標,但這可以使您繼續前進。 希望這可以幫助。

ListView沒有刷新,因為您沒有在onClick方法中的代碼中的任何地方修改ArrayAdapter的內容數組。 因此,在notifyDataSetChanged時,它使用相同的舊數組並顯示相同的先前內容。 因此沒有變化。

嘗試在onClick為適配器設置一個新數組,然后再次調用notifyDataSetChanged 那應該解決您的問題。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM