gpt4 book ai didi

android - ListView 在调用 notifyDataSetChanged() 时没有得到更新

转载 作者:行者123 更新时间:2023-11-30 03:38:29 28 4
gpt4 key购买 nike

我想在按下按钮时将数据添加到 ListView 中。

但它没有得到更新按下按钮不知道它是如何不工作的。请帮助我。

下面是代码

@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>

提前致谢

最佳答案

不确定为什么不更新。代码对我来说看起来不错。您可以尝试在调用 addAll() 之前清除数组,因为如果不这样做,您最终会在列表中出现重复项(这可能不是您想要的。应该如下所示:

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

它没有更新的原因可能是在其他地方。发布你的布局,也许问题在那里。

编辑:看到布局后:

是的,问题出在您的布局上。您不会将 ListView 放在 ScrollView 中。 ListView 已经知道如何滚动。去掉周围的 ScrollView 就可以了。

关于android - ListView 在调用 notifyDataSetChanged() 时没有得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16238153/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com