1

我是 Android Sqlite DB Dev 的新手,所以我需要你们的帮助。我能够进行基本的 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;



}

有任何想法吗?我真的需要让这个工作。谢谢

4

1 回答 1

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

List<Item>需要传递ArrayAdapter 构造函数的最后一个参数。然后只需调用setAdapter(adapter).ListView

有关更多说明,请参阅此 SO 链接:notifyDataSetChanged 示例 http://androidadapternotifiydatasetchanged.blogspot.in

于 2013-10-05T10:23:37.510 回答