滚动时,ListView的顺序混乱了

时间:2019-04-05 09:47:03

标签: java android listview scroll mixed

我正在开发一个Android应用程序,并且遇到了一个看似众所周知的问题(我发现了很多类似的问题,但没有答案适合我)。 我使用ListView来显示EditText和2 TextView,方法是在用户每次按Enter键时在ArrayList中添加一个项目。但是,当我滚动ListView时,元素的顺序更改了,因此无法解决此问题。我知道问题来自每次都要重新充电的GetView,因此,在视图上每个项目都会更改位置,但是我不知道如何调整它。 感谢您的帮助。

我已经尝试过重载getViewTypeCount()和getItemViewType函数,我还尝试删除了最大的操作(如果convertView == null),但是没有任何东西可以解决我的问题。

main_game.xml

<?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" android:background="@android:color/black">

        <ListView android:id="@+id/MyList" android:layout_height="fill_parent"
                android:layout_width="fill_parent" android:descendantFocusability="beforeDescendants">
        </ListView>
</LinearLayout>

activity_game.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.benjamin.realhacksimulatorgame.Game"
    android:background="@android:color/black">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/ll_game_vertical"
        android:paddingTop="0sp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll_game"
        android:paddingTop="0sp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/user_name"
        android:text="@string/name_user"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="15sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/editText_game"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_green_dark"
        android:backgroundTint="@android:color/holo_green_dark"
        android:background="@android:color/transparent"
        android:textSize="15sp"
        android:layout_marginLeft="10dp"
        android:textCursorDrawable="@drawable/color_cursor"
        android:inputType="text" />

</LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/reponse"
        android:layout_below="@+id/editText_game"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/editText_game"
        android:layout_alignEnd="@+id/editText_game"
        android:textColor="@android:color/holo_green_dark"/>

    </LinearLayout>
</RelativeLayout>

最后是Java类Game.java

package com.benjamin.realhacksimulatorgame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class Game extends Activity {

    //DECLARATION
    ListView myList;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_game);
        Intent i = getIntent();
        myList = (ListView) findViewById(R.id.MyList);
        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();
        myList.setAdapter(myAdapter);
    }

    protected void FonctionTest(String test, ViewHolder holder) {
        if (test.equalsIgnoreCase("ls") || test.equalsIgnoreCase("ls "))
            holder.caption_text.setText("aucun fichier a affiché");
        else if (test.equalsIgnoreCase("mkdir") || test.equalsIgnoreCase("mkdir "))
            holder.caption_text.setText("Impossible de creer un fichie pour le moment");
        else if (test.equalsIgnoreCase(""))
            holder.caption_text.setText("Vous n'avez rien entré");
        else if (test.equalsIgnoreCase("a "))
            holder.caption_text.setText("lettre a");
        else
            holder.caption_text.setText("Commande inconnu\n\n\ndesole");
    }

    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        public ArrayList myItems = new ArrayList();

        public MyAdapter() {
           // mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ListItem listItem = new ListItem();
                myItems.add(listItem);
                notifyDataSetChanged();
        }

        //GET SIZE LIST
        public int getCount() {
            return myItems.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

           // if (convertView == null || ((ViewHolder)convertView.getTag()).id != holder.caption_edit.getId()) {
            if (convertView == null ) {
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.activity_game, null);
                holder = new ViewHolder();
                holder.caption_edit = (EditText) convertView.findViewById(R.id.editText_game);
                holder.caption_text = (TextView) convertView.findViewById(R.id.reponse);
                holder.caption_name = (TextView) convertView.findViewById(R.id.user_name);
                //holder.id = holder.caption_edit.getId();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.caption_edit.setId(position);


            //we need to update adapter once we finish with editing
            final ViewHolder finalHolder = holder;
            holder.caption_edit.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                         //FILTER
                         if (event.getAction()!=KeyEvent.ACTION_DOWN) {
                             Toast.makeText(Game.this, "le retrun true prend effet" , Toast.LENGTH_LONG).show();
                             return true;
                         }
                    //IF USER CLICK ON ENTER BUTTON
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                            (keyCode == KeyEvent.KEYCODE_ENTER)) {

                        //DISABLED OLD EDITTEXT
                        finalHolder.caption_edit.setEnabled(false);
                        FonctionTest(finalHolder.caption_edit.getText().toString(), finalHolder);
                        //NEXT ITEM
                        final int position = v.getId();
                        final EditText Caption = (EditText) v;
                        final TextView Caption_text = (TextView) v;
                        final TextView Caption_name = (TextView) v;
                        ((ListItem) myItems.get(position)).caption_edit = Caption.getText().toString();
                        ((ListItem) myItems.get(position)).caption_text = Caption.getText().toString();
                        ((ListItem) myItems.get(position)).caption_name = Caption.getText().toString();
                        //Toast.makeText(Game.this, "Ceci est l'editeur numero " + position, Toast.LENGTH_LONG).show();

                        //CREATE NEW ITEM
                        ListItem listItem = new ListItem();
                        myItems.add(listItem);
                    }
                    return true;
                }
            });

            return convertView;
        }

        @Override
        public int getViewTypeCount() {

            return getCount();
        }

        @Override
        public int getItemViewType(int position) {

            return position;
        }
    }

    class ViewHolder {
        int id;
        EditText caption_edit;
        TextView caption_text;
        TextView caption_name;
    }

    class ListItem {
        int id;
        String caption_edit;
        String caption_text;
        String caption_name;
    }

}



0 个答案:

没有答案