具有edittext,checkbox和textview的自定义列表视图 [英] custom listview with edittext,checkbox and textview

查看:137
本文介绍了具有edittext,checkbox和textview的自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom listview that contains an edittext,checkbox and two text views.Now i am hving a problem with the edittext values in my code.I am not able to get the correct value from the edittext.And also if i set the value of an edit text at one position in the list,that value is also set to edittexts at random positions in the listview.And i have the common problem of the values changing on scroll.Following is the code:

活动:

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

 }

推荐答案

问题全都与焦点有关.看到这里,可能会对您有所帮助.

the problem is all about relate to focus . see here , it may help you.

ListView内的焦点EditText

这篇关于具有edittext,checkbox和textview的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆