问题
i want to add data into the listview when pressing the button.
but it is not getting updated pressing the button dont know how it is not getting worked. Please help me.
here is the code below
@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();
}
}
And my Layout looks like this
<?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>
thanks in advance
回答1:
Not sure why is isn't updating. Code looks OK to me. You might try clearing the array before calling addAll()
because if you don't you will end up with duplicates in your list (which probably isn't what you want. Should look like this:
listItems.add("me: "+message);
adapter.clear();
adapter.addAll(listItems);
adapter.notifyDataSetChanged();
The reason it isn't updating is probably somewhere else though. Post your layout, maybe the problem is in there.
EDIT: After seeing the layout:
Yep, the problem is in your layout. You don't put a ListView
inside a ScrollView
. A ListView
already knows how to scroll. Get rid of the surrounding ScrollView
and you should be good.
回答2:
Notice that you always add the same items by calling
adapter.addAll(listItems);
Regardless,
Try debugging you getView() function in the adapter.
来源:https://stackoverflow.com/questions/16238153/listview-not-getting-updated-on-calling-notifydatasetchanged