0

I am developing an Android application and I am facing a seemingly well-known problem (I found a lot of similar questions but no answer works for me). I use a ListView that displays an EditText and 2 TextView by adding an item in an ArrayList each time a user presses the button enter. However when I scroll the ListView the order of the elements changes and I can not fix this problem. I understand that the problem comes from the GetView which is recharged each time and therefore, on the view each item changes position but I do not know how to adjust it. Thanks for your help.

I have already tried to overload the getViewTypeCount () and getItemViewType functions, I also tried to remove the maximum action in the (if convertView == null) but nothing has fixed my problem.

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>

And finally the java class 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;
    }

}



Chemla
  • 1
  • 2
  • 1
    check this link ? – Partha Apr 05 '19 at 09:52
  • Thank you for your answer, I thought I visited all the links on this topic but obviously not, I did not actually add the "android: windowSoftInputMode =" adjustPan "", but even after adding this only delays my problem, it works at the beginning and after 7 or 8 add in the list it goes back to putting the first element at the end of the list (which is disabled) and I can not access the last EditText anymore – Chemla Apr 05 '19 at 10:36

0 Answers0