gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:48 25 4
gpt4 key购买 nike

我有一个 listView,用户可以在其中输入内容来更改每个 listView 行上的 textView。当您将该行滚动出屏幕时,编辑的文本将消失,默认文本将重新出现。我知道这是通过 viewHolder 修复的,但我无法让它在我的自定义适配器类中工作。

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();

}

});




}
}

自定义适配器.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

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

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

最佳答案

当文本输入到编辑文本中时,您需要将其保存到与该位置关联的变量中,并且在 getView 中您需要将 editText 的文本设置为该位置的值。否则你只会得到最后的随机值。

基本上,任何可以在行 UI 中更改的值都必须保存并写入 getView 中。

关于java - 如何使用viewHolder使listView用户输入在滚动离开屏幕时不返回默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41411351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com