使用edittext,checkbox和textview的自定义列表视图

时间:2014-02-04 09:39:36

标签: android listview android-listview

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

活动:

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;
  }

 }

1 个答案:

答案 0 :(得分:0)

问题在于与焦点有关。看到这里,它可能对你有所帮助。

Focusable EditText inside ListView