How to get the id of a particular switch in a custom list view and check it

时间:2015-04-23 05:35:56

标签: android listview

This is my getView() code. I want to access a particular switch in this custom listview. What am i doing wrong ?

public View getView(final int position, View convertView, ViewGroup parent) {


            if(convertView==null)
            {
                //inflate the custom layout
                convertView = inflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
                viewHolder=new ViewHolder();

                //cache the views
                viewHolder.sw=(Switch) convertView.findViewById(R.id.switch1);
                viewHolder.name=(TextView) convertView.findViewById(R.id.name);
                viewHolder.rollno=(TextView) convertView.findViewById(R.id.roll);

                viewHolder.id=(TextView) convertView.findViewById(R.id.ID);

                //link the cached views to the convertview
                convertView.setTag(viewHolder);

            }
            else
                viewHolder=(ViewHolder) convertView.getTag();



            //set the data to be displayed

            viewHolder.name.setText(studentList.get(position).get("name").toString());
            viewHolder.rollno.setText(studentList.get(position).get("roll_no").toString());
            viewHolder.id.setText(studentList.get(position).get("id").toString());
            viewHolder.sw.setText("");

            for (int s : absentMarked) {

                Log.d("marked",Integer.toString(s));

                if(s==1){

                    Log.d("Entered","Yes");

                    viewHolder.sw=(Switch) convertView.findViewById(R.id.switch1);
                    viewHolder.sw.setChecked(true);
                }

            }

This is where problem exist. This checks all the switches to true. I want to access a particular Switch in my list View and check it. absentMarked is an array list which contains 0 and 1. I want to check the switch to true corresponding to the 1 value

for (int s : absentMarked) {

                Log.d("marked",Integer.toString(s));

                if(s==1){

                    Log.d("Entered","Yes");

                    viewHolder.sw=(Switch) convertView.findViewById(R.id.switch1);
                    viewHolder.sw.setChecked(true);
                }

            }

1 个答案:

答案 0 :(得分:2)

您应该在模型中为AbsentMarked添加字段。

    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
              setAbsentMarked(1);
            } else{
              setAbsentMarked(0);
        }
    });

并添加setOnCheckedChangeListner以切换absentMarked的切换值,如下所示:

import java.io.FileWriter; 
import java.io.IOException; 
import org.jdom2.Attribute; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter;

try {
    Element FICHADAS = new Element("FICHADAS");
    Document doc = new Document(FICHADAS);
    doc.setRootElement(FICHADAS);
    Element fichada = new Element("fichada");
    fichada.addContent(new Element("N_Terminal").setText("XX"));
    fichada.addContent(new Element("Tarjeta").setText("XX"));
    fichada.addContent(new Element("Fecha").setText("XX"));
    fichada.addContent(new Element("Hora").setText("XX"));
    fichada.addContent(new Element("Causa").setText("XX"));
    doc.getRootElement().addContent(fichada);
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileWriter("c:\file.xml"));
} catch(IOException e) {

}
相关问题