简体   繁体   English

如何使用viewHolder使listView用户输入在屏幕滚动时不返回默认值

[英]How to use viewHolder to make listView user input not go back to default when scrolling off screen

I have a listView where a user can enter input to change the textView on each listView row. 我有一个listView,用户可以输入输入来更改每个listView行上的textView。 When you scroll the row off the screen the edited text goes away and the default text reappears. 当您从屏幕上滚动该行时,编辑的文本将消失,并重新显示默认文本。 I know this is fixed with the viewHolder but I can't get it to work in my custom adapter class. 我知道这是用viewHolder修复的,但我不能让它在我的自定义适配器类中工作。

MainActivity.java MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ArrayList<String> Chores = new ArrayList<>();
        //Chores.add("");

        final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
        ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
        listViewObject.setAdapter(MyAdapter);

        listViewObject.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                    String ChoreString = String.valueOf(parent.getItemAtPosition(position));




                }
            }

        );
        final Button button = (Button) findViewById(R.id.button_ID);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Chores.add("");
                ((ArrayAdapter)MyAdapter).notifyDataSetChanged();

            }

        });




    }
}

CustomAdapter.java CustomAdapter.java

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import static com.example.emilythacker.chorelist.R.id.textView_ID;



class CustomAdapter extends ArrayAdapter{

    ArrayList<String> choreText;

    public CustomAdapter(Context context, ArrayList choreText) {
        super(context, R.layout.custon_listview_row, choreText);
        this.choreText = choreText;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        //final TextView textView = (TextView) customView.findViewById(textView_ID);

        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            convertView = layoutInflater.inflate(R.layout.custon_listview_row, null);
            holder = new ViewHolder();
            holder.imageButton = (ImageButton) convertView.findViewById(R.id.imageButton_ID);
            holder.textView = (TextView) convertView.findViewById(textView_ID);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(choreText.get(position));
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //what happens when textView is clicked
                AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
                final EditText input = new EditText(getContext());
                input.setHint("hint");
                alertDialog.setView(input);
                alertDialog.setTitle("Set Chore");
                alertDialog.setMessage("Alert message to be shown");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {




                        holder.textView.setText(input.getText().toString());





                        dialog.dismiss();
                    }
                });
                alertDialog.show();

            }
        });



        holder.imageButton.setImageResource(R.drawable.clock);


        return convertView;
    }

    static class ViewHolder {
        ImageButton imageButton;
        TextView textView;
    }
}

activity_main.xml activity_main.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:id="@+id/activity_main"
    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.example.emilythacker.chorelist.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/customListView_ID"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp" />

    <Button
        android:text="Add Chore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button_ID" />
</RelativeLayout>

custon_listview_row.xml custon_listview_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="60dp">

    <ImageButton
        android:layout_width="60dp"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/clock"
        android:id="@+id/imageButton_ID"
        android:layout_height="60dp"
        android:background="@null"
        android:layout_alignParentRight="true"
        android:padding="5dp"
        android:layout_weight="1" />


    <TextView
        android:text="Click here to add chore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/textView_ID"
        android:textSize="25dp"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/imageButton_ID"
        android:layout_marginEnd="11dp"
        />


</RelativeLayout>

When text is entered into the edit text, you need to save it to a variable associated with that position, and in getView you need to set the text of the editText to that position's value. 当文本输入编辑文本时,您需要将其保存到与该位置关联的变量,而在getView中,您需要将editText的文本设置为该位置的值。 Otherwise you'll just have whatever random value was in there last. 否则你最后会得到任何随机值。

Basically, any value that can change in a row's UI must be saved and written to it in getView. 基本上,任何可以在行的UI中更改的值都必须保存并在getView中写入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让动图 go 掉屏并让它重新出现 - How to make a moving picture go off the screen and make it come back again 当用户从任何屏幕注销应用程序时,如何返回主屏幕? - How to go back main screen when user logout app from any screen? 无论用户如何输入,如何使Searchview Widget输入进入特定屏幕 - How to make Searchview Widget input go to specific screen regardless of user input 向上滚动时,我的自定义列表视图会立即滚动回到第一个元素。 如何防止这种情况并使滚动平滑 - My custom listview scrolls back to first element instantly when scrolling up. How to prevent that and make a smooth scrolling 屏幕关闭时如何使用输入点击命令 - How to use input tap command with the screen off 如何使用viewHolder模式使UI元素在listView中滚动? - How to use viewHolder pattern to keep UI elements on scroll in listView? 多个viewholder recyclerview无法从屏幕上加载视图 - multple viewholder recyclerview not loading off screen views 如何使该程序返回提示,直到用户退出 - How to make this program go back to prompt until the user exits 如何循环返回并与用户输入一起使用 - How to loop back through and use with user input 用户关闭屏幕时如何暂停游戏 - How to pause game when user turn off screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM