我对EditText
中的ListActivity
字段存在问题。
代码完全符合,但功能很奇怪,输入第一个字段并隐藏键盘,此后文本出现在另一个编辑域中。
帮助解决我的逻辑问题
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);
}
}
答案 0 :(得分:3)
通过在清单中添加条目和使用TextWatcher
来解决问题(这是必需的,因为一个列表行的视图被内部调用了几次,这意味着对于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;
}
}
答案 1 :(得分:1)
这会对你有所帮助
``
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] attitude_names;
public String[] attitude_values;
private String name;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
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];
}
// initialize myList
for(int i=0;i<attitude_names.length;i++)
{
myList.put(i,"");
}
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)
{
myList.put(pos,s.toString.trim());
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(myList.get(position));
return convertView;
}
class ViewHolder {
TextView Attitude_Name;
EditText Attitude_Value;
int ref;
}
@Override
public int getCount() {
return attitude_names.length;
}
}
这里我已经包含了一个HashMap对象,它将继续关注EditText包含的值。当你滚动listview时,它将通过调用它的getView方法再次呈现。
在这段代码中,当你第一次加载listview时,你所有的edittext都没有text.once你输入一些文本,它会在myList中注明。所以当你再次渲染列表时,你的文本就会被阻止。< / p>
答案 2 :(得分:0)
android:windowSoftInputMode="adjustPan"
作为值添加到Projects Manifest中的活动
但另一方面,通常不止一个edidfield值发生变化的问题现在还没有解决