CustomAdapter覆盖ListView中的项目 - Android

时间:2014-07-07 01:29:49

标签: java android list listview custom-adapter

此代码有效,但每次单击该按钮时,它都会覆盖列表视图中的项目,而不是将项目添加到列表底部。尝试了很多,但没有解决这个问题。

  public static final class ChatRoom2 extends Activity {

    Button addButton;
    ListView listView;
    EditText editText;
    TextView values;
    ImageView userimage;
    ChatRow adapter;
    ChatRow adapter2;
    ChatRow listItems;
    String message;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String sText = intent.getExtras().getString("textFromList");
        getActionBar().setTitle(sText);
        setContentView(R.layout.chatlayout2);


        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        final int[] images = {R.drawable.image01, R.drawable.image02, R.drawable.image03};



        final int imageId = (int)(Math.random() * images.length);
        userimage = (ImageView) findViewById(R.id.img);
        addButton = (Button) findViewById(R.id.button2);

        editText = (EditText) findViewById(R.id.editText);
        values = (TextView) findViewById(R.id.txt);


        final RowItems weather_data[] = new RowItems[]
                {
                        new RowItems(images[imageId],message)
                };

        adapter = new ChatRow(this,
                R.layout.chatmessage, weather_data);


        listView = (ListView) findViewById(R.id.listView);



         listView.setAdapter(adapter);


        addButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {


                message = editText.getText().toString();
                RowItems item = new RowItems(images[imageId], message);
                adapter.add(item);
                adapter.notifyDataSetChanged();

                editText.setText("");

                //minimize keyboard 
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.
                        INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                                    long id) {


                Toast.makeText(ChatRoom2.this, "clicked", Toast.LENGTH_LONG)
                        .show();




            }
        });

    }


}

这已更新,现在崩溃了。这是logcat

07-06 18:56:40.953  17949-17949/com.example.jason.testlist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
        at com.example.jason.testlist.ListViewAndroidExample$ChatRoom2$1.onClick(ListViewAndroidExample.java:213)
        at android.view.View.performClick(View.java:4475)
        at android.view.View$PerformClick.run(View.java:18784)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5450)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您正在设置新的(单项)适配器,而不是更新当前的适配器。这就是ListView不保留旧行的原因。

首先,在创建ListView后立即设置适配器(您不需要第二个,这可能会引起混淆,所以只需删除adapter2):

adapter = new ChatRow(this, R.layout.chatmessage, new ArrayList<RowItems>());
adapter.add(new RowItems(images[imageId],message));
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

然后,更改此代码:

message = editText.getText().toString();
RowItems messagedata[] = new RowItems[]{ new RowItems(images[imageId], message) };
adapter2 = new ChatRow(ChatRoom2.this, R.layout.chatmessage, messagedata);
listView.setAdapter(adapter2);
adapter2.notifyDataSetChanged();

或多或少像:

message = editText.getText().toString();
RowItems item = new RowItems(images[imageId], message);
adapter.addItem(item);
adapter.notifyDataSetChanged();

其中addItem()添加到适配器的数据收集。

如果适配器是ArrayAdapter 并且您使用List<T>初始化它,而不是数组,那么您不必实现addItem(),只需使用内置的add()