2013-10-05 155 views
1

我是新來的Android Sqlite DB Dev't,所以我需要你的幫助球員。我能夠完成基本的CRUD操作。現在我想知道如何刷新列表視圖來顯示新添加的數據。我查看了adapter.notifyDataSetChanged,但我想知道爲什麼它不適合我。這是我迄今爲止所嘗試的刷新列表視圖/顯示新增數據到列表視圖android

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

什麼這個循環爲:對(INT I = 0; I hasan83

回答

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

更多的澄清,請參閱本SO鏈接:notifyDataSetChanged example http://androidadapternotifiydatasetchanged.blogspot.in

+0

感謝您的鏈接和您的幫助。 – Dunkey

+0

@Dunkey不客氣。快樂的編碼。 –