2016-11-07 108 views
0

我嘗試在Android中更新我的listView時遇到適配器問題。我在堆棧中經歷了類似的問題,但仍然沒有弄明白。在ListView中更新適配器android

我正在研究一種垃圾桶列表應用程序,並且我開始一項新活動,以便將願望添加到希望列表中。它只是一個用於學習目的的原型,我試圖通過使用adapter.add(「用戶插入的願望」)來實現它,而不使用異步解決方案,但應用程序崩潰或者甚至無法啓動。我提到我已經檢查了發送消息的活動的功能(我只是簡單地顯示了它,因爲您可以在onActivityResult上看到)。此外,下面提供的示例代碼也適用,因爲我已經評論了問題代碼,但不會向listView添加任何內容。

那麼,爲什麼我認爲它不工作(如果我使用註釋代碼):如果我把我的適配器聲明中的onCreate下來 onActivityResult,我不能夠使用它不可見該方法添加在那裏,也適配器,如果我聲明我的適配器外面它只是崩潰/不啓動應用程序。

package com.example.packagename.reforgedbucketlist; 

import android.content.Intent; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import static com.example.beatriceghetel.reforgedbucketlist.R.styleable.View; 

public class MainActivity extends AppCompatActivity { 
    public String logMsg = "State"; 

    String[] wishesArray = {"Preset wish: use this app!","Preset wish: enjoy this app!","Preset wish: rate this app!","Preset wish: recommend this app!"}; 

    /*ArrayAdapter adapter = new ArrayAdapter<String>(this, 
      R.layout.activity_listview, wishesArray); 
    ListView listView = (ListView) findViewById(R.id.wishes_list);*/ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ArrayAdapter adapter = new ArrayAdapter<String>(this, 
       R.layout.activity_listview, wishesArray); 
     ListView listView = (ListView) findViewById(R.id.wishes_list); 
     listView.setAdapter(adapter); 
     Log.d(logMsg, "The onCreate() event"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_items, menu); 

     MenuItem item1 = (MenuItem) findViewById(R.id.about_id); 
     MenuItem item2 = (MenuItem) findViewById(R.id.settings_id); 
     MenuItem item3 = (MenuItem) findViewById(R.id.add_wish_id); 



     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.d(logMsg, "The onStart() event"); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d(logMsg, "The onResume() event"); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Log.d(logMsg, "The onPause() event"); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     Log.d(logMsg, "The onStop() event"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d(logMsg, "The onDestroy() event"); 
    } 

    public void onAddWishClick(MenuItem item) { 
     Intent getNewWishIntent = new Intent(this, AddWishActivity.class); 
     final int result = 1; 
     getNewWishIntent.putExtra("CallingActivity", "Wish"); 
     startActivityForResult(getNewWishIntent, result); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     TextView userWishView = (TextView) findViewById(R.id.message_sent_back); 
     String wishSentBack = data.getStringExtra("UserWish"); 
     userWishView.append(" " + wishSentBack); 

     //adapter.add(wishSentBack); 
     //adapter.notifyDataSetChanged(); 

    } 
} 

回答

1

問題是您正在使用具有固定大小的數組。

嘗試聲明改成這樣:

public class MainActivity extends AppCompatActivity { 
    public String logMsg = "State"; 
    List<String> list = new ArrayList<>(); 

    String[] wishesArray = {"Preset wish: use this app!","Preset wish: enjoy this app!","Preset wish: rate this app!","Preset wish: recommend this app!"}; 


// On create ....{ 
list.addAll(Arrays.asList(wishesArray)); 

ArrayAdapter adapter = new ArrayAdapter<String>(this, 
       R.layout.activity_listview, list); 
//.... do the rest normally 
} 

而當你需要更新你的數據:

list.add(wishSentBack); 
    adapter.notifyDataSetChanged(); 
+0

修改,但仍無法使用'adapter.notifyDataChanged() ;'在_onActivityResult_中,所以我留下了那條評論... result:**等待設備時出錯:等待模擬器上線300秒後超時** Android Studio死了...我重新打開後立即重試 – student0495

+0

我不得不重啓電腦,問題出在我的模擬器上。解決與適配器也問題,我已經宣佈的列表和適配器_onCreate_方法列表# list = new ArrayList <>(); ArrayAdapter adapter;'並且像你所說的那樣在該方法內使用它們。現在它完美地工作。謝謝! – student0495