Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView中的重复项_Android_Listview - Fatal编程技术网

Android ListView中的重复项

Android ListView中的重复项,android,listview,Android,Listview,我已经用数组适配器创建了一个自定义listview。它包含编辑文本和一些其他单选按钮等。 我的listview在每4个视图之后显示重复视图。也就是说,当我输入第1版文本时,它也被输入第5版文本。 我读过,为了节省内存,android只创建有限的视图并重复它。 那么,如何克服这个问题呢?如何使用bindView()方法?在哪里?怎么做 公共类问题适配器扩展了ArrayAdapter{ Context context; int layoutResourceId; Question dat

我已经用数组适配器创建了一个自定义listview。它包含编辑文本和一些其他单选按钮等。 我的listview在每4个视图之后显示重复视图。也就是说,当我输入第1版文本时,它也被输入第5版文本。 我读过,为了节省内存,android只创建有限的视图并重复它。 那么,如何克服这个问题呢?如何使用bindView()方法?在哪里?怎么做

公共类问题适配器扩展了ArrayAdapter{

Context context; 

int layoutResourceId;    
Question data[] = null;

public QuestionAdapter(Context context, int layoutResourceId, Question[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final WeatherHolder holder;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();          
        row = inflater.inflate(layoutResourceId, parent, false);
        //row = inflater.inflate(R.layout.listview_item_row, null);
        holder = new WeatherHolder();
        holder.txtquestion = (TextView)row.findViewById(R.id.txtquestion);
        holder.radioYes = (RadioButton)row.findViewById(R.id.radioYes);
        holder.radioNo= (RadioButton)row.findViewById(R.id.radioNo);
        holder.editResponse=(EditText)row.findViewById(R.id.editResponse);
        holder.radio_group=(RadioGroup)row.findViewById(R.id.radio_group);

        row.setTag(holder);


    }
    else

    {
        holder = (WeatherHolder)row.getTag();
    }

    Question question = data[position];
    holder.txtquestion.setText(question.question);
    holder.radioYes.setChecked(true);
    holder.radioYes.setChecked(false);

    //holder.editResponse.setText("Edit Response");
    return row;

}



static class WeatherHolder
{
    TextView txtquestion;
    RadioButton radioYes;
    RadioButton radioNo;
    EditText editResponse;
    RadioGroup radio_group;
}
}

在getView()中,您必须手动为每个视图指定值,在您的情况下,为每个EditText指定值。您可以在getView中执行edtitext.setText(“”),当您在一个edittext中写入时,listview将不会重复显示

编辑:

您可以尝试在每个EditText中保存文本,并在getView()中将这些文本指定给其EditText

String [] text ;

Question data[] = null;

public QuestionAdapter(Context context, int layoutResourceId, Question[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;

    text = new String[data.length];

    for(String s: text)
        s="";
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final WeatherHolder holder;
    final pos = position;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();          
        row = inflater.inflate(layoutResourceId, parent, false);
        //row = inflater.inflate(R.layout.listview_item_row, null);
        holder = new WeatherHolder();
        holder.txtquestion = (TextView)row.findViewById(R.id.txtquestion);
        holder.radioYes = (RadioButton)row.findViewById(R.id.radioYes);
        holder.radioNo= (RadioButton)row.findViewById(R.id.radioNo);
        holder.editResponse=(EditText)row.findViewById(R.id.editResponse);
        //Add a listener and you'll know the text inside EditText
        holder.editResponse.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            //Save text after typed it
            text[pos] = s.toString();

        }
    });

    holder.radio_group=(RadioGroup)row.findViewById(R.id.radio_group);

    row.setTag(holder);


}
else

{
    holder = (WeatherHolder)row.getTag();
}

    Question question = data[position];
    holder.txtquestion.setText(question.question);
    holder.radioYes.setChecked(true);
    holder.radioYes.setChecked(false);
    //Assign text
    holder.editResponse.setText(text[position]);
    return row;
}
您的getView(…)实现是什么样子的

应该是这样的:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View view = convertView;

    if(view == null)
        view = inflate(view, parent);

    MyObject obj = objects.get(position);
    if(obj != null)
        view = setUIControls(position, view, obj);

    return view;
}

您可以膨胀新的
视图
或重新使用“旧的”
视图
首先使用ConvertView而不是您的视图 这是没用的,你对他们做了什么

if(convertView == null){
convertView=inflater.inflate....



return convertView;
然后从viewHolderObject中删除
final
修改器

Question data[] = null;// what the hell? if you want an array....
Question[] data=null;
并使您的视图持有者共享

class YourClass..{
private WeatherHolder holder;
converView==null时

if(convertView==null){
....
holder=new WeatherHolder();

你能发布你的适配器实现吗?把你的代码snippet@HemantVc请看问题/有一个新问题。当我向上或向下滚动listview时,EditText中没有文本。我认为将文本保存到EditText不起作用。您在ContextChanged中编写的代码应该在OnFocusChanged中。那它就可以正常工作了。非常感谢。是的,OnfocusChangedLister不应该出现在if或else语句中。它应该在else{}块之后..因为您只是在if(row=null){}块中膨胀一行。