刷新listview /显示新添加的数据到listview android

时间:2013-10-05 10:16:22

标签: android sqlite listview

我是Android Sqlite DB Dev't的新手,所以我需要你的帮助。我能够完成基本的CRUD操作。现在我想知道如何刷新列表视图以显示新添加的数据。我查看了adapter.notifyDataSetChanged,但我想知道为什么它不适合我。这是我到目前为止所尝试的内容

   private void initControls() {

    SearchAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    String[] SearchItems = getResources().getStringArray(R.array.DogSearch);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.searchitems,SearchItems);
    SearchAuto.setAdapter(adapter);
    SearchAuto.setThreshold(0);

    search = (Button) findViewById (R.id.handle);
    search.setOnClickListener(this);
    lv = (ListView) findViewById (R.id.lv);

    values = dbHelper.getAllAnimals(ANIMALTYPE);
    for (int i = 0; i < values.length; i++) {
        lvAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        lv.setAdapter(lvAdapter);
    }

    // TODO onItemClick
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
         {
            String strDog = parent.getItemAtPosition(position).toString();

            Intent i = new Intent(DogClass.this, Dogs.class);
            i.putExtra("dog_name", strDog);
            startActivity(i);    
            finish();
         }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.addanimal, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // get prompt_addprayer.xml view
    LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.prompt_addanimal, null);

    AlertDialog.Builder alertDialogBuilder = 
            new AlertDialog.Builder(this);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final EditText etBreed = (EditText) promptsView
            .findViewById(R.id.etBreed);

    final EditText etDescription = (EditText) promptsView
            .findViewById(R.id.etDescription);

    final EditText etDiet = (EditText) promptsView
            .findViewById(R.id.etDiet);

    final EditText etShelter = (EditText) promptsView
            .findViewById(R.id.etShelter);

    final EditText etHygiene = (EditText) promptsView
            .findViewById(R.id.etHygiene);

    final EditText etMedication = (EditText) promptsView
            .findViewById(R.id.etMedication);

    // set dialog message
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(
                                DialogInterface dialog,
                                int id) {

                            String strAnimalType = ANIMALTYPE;
                            String strBreed = etBreed.getText().toString();
                            String strDesc = etDescription.getText().toString();
                            String strDiet = etDiet.getText().toString();
                            String strShelter = etShelter.getText().toString();
                            String strHygiene = etHygiene.getText().toString();
                            String strMedication = etMedication.getText().toString();

                                dbHelper.addAnimalInfo(strAnimalType, strDesc, 
                                        strDiet, strShelter, strHygiene, strMedication, strBreed);
                                Toast.makeText(DogClass.this, "Data has been added successfully!",
                                        Toast.LENGTH_SHORT).show();

                                lvAdapter.notifyDataSetChanged();
                        }

                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(
                                DialogInterface dialog,
                                int id) {
                            dialog.cancel();
                        }
                    });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    // show it
    alertDialog.show();
    return true;



}

有什么想法吗?我真的需要让这个工作。感谢

1 个答案:

答案 0 :(得分:1)

dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
                                        Toast.LENGTH_SHORT).show();

lvAdapter.notifyDataSetChanged();

这是你缺乏的地方。你所做的是,你只是将数据插入到数据库中,你仍然需要从数据库中获取数据并将其与适配器绑定,然后你可以调用notifyDataSetChanged();,你就可以实现你想要的......

试试这个:

dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
    Toast.makeText(DogClass.this, "Data has been added successfully!",
                                            Toast.LENGTH_SHORT).show();
values = dbHelper.getAllAnimals(ANIMALTYPE);
lvAdapter.clear();
lvAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);

    lvAdapter.notifyDataSetChanged();

注意:你不应该这样写。你的代码:

for (int i = 0; i < values.length; i++) {
        lvAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        lv.setAdapter(lvAdapter);
    }

如果您的getAllAnimals()方法返回一个arrayList,那么您只需要:

lvAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        lv.setAdapter(lvAdapter);

ArrayAdapter构造函数的最后一个参数需要传递List<Item>。然后只需致电setAdapter(adapter)的{​​{1}}。

有关详细说明,请参阅此SO链接:notifyDataSetChanged example http://androidadapternotifiydatasetchanged.blogspot.in