ListView控件没有得到更新调用notifyDataSetChanged() [英] ListView not getting updated on calling notifyDataSetChanged()

查看:123
本文介绍了ListView控件没有得到更新调用notifyDataSetChanged()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据添加到列表视图时pressing的按钮。

i want to add data into the listview when pressing the button.

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

but it is not getting updated pressing the button dont know how it is not getting worked. Please help me.

下面是低于

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

在此先感谢

推荐答案

不知道为什么没有更新。 code看起来不错给我。你可能会尝试调用之前清除阵列的addAll(),因为如果你不这样做你最终会在你的名单重复(这可能不是你想要的。应该是这样的:

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.

编辑:看到布局后:

没错,问题是在你的布局。你不要把的ListView 滚动型。 A 的ListView 已经知道如何滚动。摆脱周围的滚动型,你应该是不错的。

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.

这篇关于ListView控件没有得到更新调用notifyDataSetChanged()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆