gpt4 book ai didi

java - 列表中的 EditText 未按应有的方式工作

转载 作者:行者123 更新时间:2023-12-01 19:20:49 24 4
gpt4 key购买 nike

我在 ListActivity 中的 EditText 字段遇到问题。

代码完全符合要求,但功能很奇怪,在第一个字段中输入内容并隐藏键盘,此后文本出现在另一个编辑字段中。

帮我解决逻辑问题

package com.example.helloandroid;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class AddComp extends ListActivity {


static final int DATE_DIALOG_ID = 0;

private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] attitude_names;
private String[] attitude_values;

public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
attitude_values = new String[attitude_names.length];
}

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) {
convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);

holder = new ViewHolder();
holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.Attitude_Name.setText(attitude_names[position]);
holder.Attitude_Value.setHint(attitude_names[position]);
attitude_values[position] = holder.Attitude_Value.getText().toString();



return convertView;
}

class ViewHolder {
TextView Attitude_Name;
EditText Attitude_Value;
}

@Override
public int getCount() {
return attitude_names.length;
}
}


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListAdapter(new EfficientAdapter(this));

setContentView(R.layout.addcomp);
}
}

最佳答案

通过在 list 中添加条目并使用 TextWatcher 解决了问题(这是必需的,因为一个列表行的 View 会在内部调用多次,这意味着程序仅使用 500 个列表条目row.view-class 的一些实例会更加高效)因此需要使用文本观察器将更改的数据保存在额外的数据结构中以对数组进行采样..

        private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] attitude_names;
public String[] attitude_values;
private String name;

public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
attitude_values = new String[attitude_names.length];
}

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

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

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

if (convertView == null) {
convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);

holder = new ViewHolder();
holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);
holder.Attitude_Value.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edt)
{
attitude_values[holder.ref] = edt.toString();
}

public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//attitude_values[ref] = Attitude_Value.getText().toString();
}
});

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}


holder.ref=position;
holder.Attitude_Name.setText(attitude_names[position]);
holder.Attitude_Value.setHint(attitude_names[position]);
holder.Attitude_Value.setText(attitude_values[position]);




return convertView;
}

class ViewHolder {
TextView Attitude_Name;
EditText Attitude_Value;
int ref;



}

@Override
public int getCount() {
return attitude_names.length;
}
}

关于java - 列表中的 EditText 未按应有的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358342/

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