【问题标题】:custom listview with edittext,checkbox and textview带有edittext、checkbox和textview的自定义列表视图
【发布时间】:2014-02-04 09:39:35
【问题描述】:

我有一个自定义列表视图,其中包含一个编辑文本、复选框和两个文本视图。现在我的代码中的编辑文本值有问题。我无法从编辑文本中获取正确的值。而且如果我在列表中的一个位置设置编辑文本的值,该值也设置为列表视图中随机位置的编辑文本。我有一个常见问题是滚动时值发生变化。以下是代码:

活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_assess_list);
    prev = getIntent();
 final ListView listview = (ListView) findViewById(R.id.studentsListView);
 listview.setAdapter(new AssessmentAdapter(this, R.layout.assessment,
            StudentNames.student_name, StudentNames.studentRollNo));

}

适配器:

public class AssessmentAdapter extends ArrayAdapter<String> {

Context context;
ViewHolder holder;
CheckBox present;
EditText marks;
int pos;
public static ArrayList<String> studentNames,studentRollNo,studentsPresent,marksObtainedList;
public AssessmentAdapter(Context context, int textViewResourceId,ArrayList<String> studentNames,ArrayList<String> studentRollNo) {
    super(context, textViewResourceId,studentNames);
    // TODO Auto-generated constructor stub
    this.context=context;
    AssessmentAdapter.studentNames=studentNames;
    AssessmentAdapter.studentRollNo=studentRollNo;
    studentsPresent=new ArrayList<String>();
    marksObtainedList=new ArrayList<String>();
    Log.d("No. of students",""+studentRollNo.size());
    for(int i=0;i<studentNames.size();i++)
    {
        studentsPresent.add("1");
        marksObtainedList.add("0");
    }

}
 static class ViewHolder { 

    CheckBox presentCB;
    TextView name,Rno;
    EditText marksObtained;
    Button save;
} 

public View getView(final int pos,View convertview,ViewGroup parent)
{
try
{
holder=null;
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(convertview==null)
{

    //rowView=inflater.inflate(org.example.attendance.R.layout.list_radio,parent,false);
    convertview=inflater.inflate(R.layout.assessment,null);
    holder=new ViewHolder();
    holder.presentCB=(CheckBox)convertview.findViewById(org.example.attendance2.R.id.presentCB);
    holder.name=(TextView)convertview.findViewById(org.example.attendance2.R.id.studNameTV);
    holder.Rno=(TextView)convertview.findViewById(org.example.attendance2.R.id.studRollNoTV);
    holder.marksObtained=(EditText)convertview.findViewById(org.example.attendance2.R.id.marksET);
    holder.save=(Button)convertview.findViewById(org.example.attendance2.R.id.saveButton);

    convertview.setTag(holder);
}

 else
{
    holder=(ViewHolder) convertview.getTag();
}
holder.name.setText(studentNames.get(pos));
holder.Rno.setText(studentRollNo.get(pos));

holder.save.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
});

    holder.marksObtained.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        marksObtainedList.set(pos, arg0.toString());
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

});
holder.presentCB.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(studentsPresent.get(pos).equals("1"))
            {
                studentsPresent.set(pos, "0");
            }
            else 
            {
                studentsPresent.set(pos, "1");
            }       
    }
});
    //if(RadioChecked[pos])
    if(studentsPresent.get(pos).equals("1"))
    {
        holder.presentCB.setChecked(true);      
    }
//  else if(!RadioChecked[pos])
    else if(studentsPresent.get(pos).equals("0"))
    {
        holder.presentCB.setChecked(false);
    }   
    if(holder.marksObtained.getText().toString().equals(""))
    {
        marksObtainedList.set(pos,"0");
    }
    else
    {
        String marks=holder.marksObtained.getText().toString();
        marksObtainedList.set(pos,marks);
        Log.d(""+pos,marksObtainedList.get(pos));
    }
    holder.marksObtained.setText(marksObtainedList.get(pos));
}catch(Exception e)
{
}
return convertview;
}
 public boolean areAllItemsEnabled() 
 {
return true;
  }

  @Override
 public boolean isEnabled(int arg0) 
 {
return true;
  }

 }

【问题讨论】:

  • editText 的值保存到对象的位置?
  • 视图回收的经典案例,你需要创建一个数组来存储你的值。
  • 如果您对编辑文本的方式进行了任何更改。您还必须更新数组上的值。
  • @Shayanpourvatan 我将值存储在一个数组列表中......最后一个 if else 在适配器中
  • 用这个你添加 null 到你的所有对象,你需要 textWatcherfocus listener (这取决于你的需要)并保存价值

标签: android listview android-listview


【解决方案1】:

问题在于与 focus 相关。看这里,可能对你有帮助。

Focusable EditText inside ListView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多