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

标签 android android-listview android-arrayadapter

我想在按下按钮时将数据添加到 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/

相关文章:

android - 如何在单个 TextView 中显示数组列表中的多行?

Android-Priority-Jobqueue Survive Orientation Change

java - 具有自定义行布局的 ListView - Android

java - 当我到达 recyclerView.setAdapter(adapter); 部分时,我在 android studio 中的应用程序崩溃了。

android - 如何检查 json 数组是否可用并存在?

java - 安卓 UIAutomator : setAsHorizontalList() throws noSuchMethodError

android - ListView 项目排序/重新排序并保存更改?

java - 从适配器访问时 EditText 返回 null

java - 选择状态微调器后填充城市微调器

android - 获取 ListView 中的 ImageView 位置