2013-04-26 52 views
1

我想添加數據到列表視圖時按下按鈕。ListView沒有得到更新的調用notifyDataSetChanged()

但它沒有得到更新按下按鈕不知道它是如何工作。 請幫幫我。

這裏是低於

@SuppressLint("NewApi") 
public class Messanger extends Activity { 
ArrayAdapter<String> adapter; 
ListView output; 
EditText box; 
String msg[]; 
Button btn; 
int counter=0; 
ArrayList<String> listItems; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_messanger); 
    output=(ListView)findViewById(R.id.textOutput); 
    box=(EditText)findViewById(R.id.textInput); 
    listItems = new ArrayList<String>(); 
     adapter = new ArrayAdapter<String> (Messanger.this,android.R.layout.simple_list_item_1, android.R.id.text1, listItems); 
    output.setAdapter(adapter); 
    btn=(Button)findViewById(R.id.btnSend); 
    btn.setOnClickListener(new OnClickListener() 
    { 

     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Toast.makeText(Messanger.this, "in event", Toast.LENGTH_LONG).show(); 

      sendMessage(box.getText().toString()); 
      //stItems.notifyAll(); 


     } 

    }); 
} 
public void sendMessage(String message) 
{ 
    listItems.add("me: "+message); 
    adapter.addAll(listItems); 
    adapter.notifyDataSetChanged(); 
} 
} 

的代碼和我的佈局看起來這

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="10" > 
    <ListView 

     android:id="@+id/textOutput" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingTop="5dp" /> 
</ScrollView> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="100" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:paddingBottom="5dp" 
    android:baselineAligned="true"> 
    <EditText android:layout_weight="1" android:id="@+id/textInput" 
     android:layout_height="45dp" android:layout_width="fill_parent"> 
     <requestFocus></requestFocus> 
    </EditText> 
    <Button android:layout_weight="1" android:text="Send" 
     android:layout_height="45dp" android:layout_width="125dp" 
     android:id="@+id/btnSend"></Button> 
</LinearLayout> 
</LinearLayout> 

感謝像事先

回答

1

不知道爲什麼沒有更新。代碼對我來說很合適。你可能會嘗試調用addAll()之前清除陣列,因爲如果你沒有,你最終會在你的名單副本(這可能是不是你想要的應該是這樣的:

listItems.add("me: "+message); 
adapter.clear(); 
adapter.addAll(listItems); 
adapter.notifyDataSetChanged(); 

它不是原因。「T更新可能是別的地方雖然發佈你的佈局,也許問題就在那裏

編輯:看到佈局後:

沒錯,問題是出在你的佈局你不把ListView放在ScrollView之內A ListView已經知道如何滾動。擺脫周圍的ScrollView,你應該很好。

+0

我已經粘貼了UI代碼,你可以建議一些它 – 2013-04-26 14:25:48

+0

是的,現在我可以看到問題了。我編輯了我的答案。 – 2013-04-26 14:29:46

+0

它的工作真棒感謝外掛.... – 2013-04-27 08:05:56

0

注意,你總是通過調用

adapter.addAll(listItems); 

不管添加相同的項目,

嘗試在適配器調試你getView()函數。

+0

OP是不是使用自定義適配器,他使用標準的Android'ArrayAdapter'。 – 2013-04-26 14:27:29

相關問題