如果从列表视图中获取数据,如果选中3个复选框中的任何一个,如何从行中获取复选框和文本的值

时间:2015-03-24 09:30:01

标签: android listview checkbox

如何知道检查了哪个复选框以及如何检索每行的数据并将其保存到文件中

public class MainActivity extends Activity {

    ArrayList<Inforowdata> rowdata;
    ArrayList<Inforowdatab> rowdatab;
    ArrayList<Inforowdatac> rowdatac;
    ListView l;
    //String[] data={"Ram", "Shyam", "Deepak", "sabdhs", "dsbndj", "dshg", "dsnd", "dsavg", "dbash", "dbshd", "dshgd", "bsdhgs", "dsgfdsfdgsf", "sda", "sdchb", "AJdfg", "sdfjgh","data" , "sad", "dfax" };;
    String result;
    ArrayList<String> myArray;
    Button b;
    int s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        l= (ListView)findViewById(R.id.list);

        try {
            File file = new File("/sdcard/report.csv");
            if(!file.exists())
            {

                Toast.makeText(getApplicationContext(), "file Dont Exist", Toast.LENGTH_SHORT).show();
            }
            FileInputStream in = new FileInputStream(file);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(in));
            Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();



            l= (ListView)findViewById(R.id.list);
            myArray= new ArrayList<String>();


            try {
                String row;
                while((row= myReader.readLine())!=null)
                {

                    result= Arrays.toString(row.split(",")).replace("[", "").replace("]", "");

                    myArray.add(result);
                    ArrayAdapter<String> a= new ArrayAdapter<String>(getApplicationContext(), R.layout.row,R.id.text,myArray);
                    l.setAdapter(a);

                    //String result=Arrays.toString(row).replace("[", "").replace("]", "").split(",");
                    //resultList.add(data);
                    //resultList.add(result);
                    //myStringArrayList.add(result);
                    //ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_xml,R.id.name, data);
                    //l.setAdapter(adapter);
                }

                myReader.close();
                //Toast.makeText(getApplicationContext(), s.toString, Toast.LENGTH_SHORT).show();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        l.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }
        });






    rowdata= new ArrayList<MainActivity.Inforowdata>();
    for(int i=0;i<myArray.size();i++)
    {
        rowdata.add(new Inforowdata(false,i));
    }
    l.setAdapter(new MyAdapter());

    rowdatab= new ArrayList<MainActivity.Inforowdatab>();
    for(int i=0;i<myArray.size();i++)
    {
        rowdatab.add(new Inforowdatab(false,i));
    }
    l.setAdapter(new MyAdapter());

    rowdatac= new ArrayList<MainActivity.Inforowdatac>();
    for(int i=0;i<myArray.size();i++)
    {
        rowdatac.add(new Inforowdatac(false,i));
    }
    l.setAdapter(new MyAdapter())   


}

class Inforowdata{
    public boolean isclicked=false;
    public int index;
    /*public String fanId;
    public String strAmount;*/

    public Inforowdata(boolean isclicked,int index/*,String fanId,String strAmount*/)
    {
        this.index=index;
        this.isclicked=isclicked;
        /*this.fanId=fanId;
        this.strAmount=strAmount;*/
    }
}


class Inforowdatab{
    public boolean isclicked=false;
    public int index;

    public Inforowdatab(boolean isclicked,int index/*,String fanId,String strAmount*/) {
        // TODO Auto-generated constructor stub
        this.index=index;
        this.isclicked=isclicked;
    }
}



class Inforowdatac{
    public boolean isclicked=false;
    public int index;

    public Inforowdatac(boolean isclicked,int index/*,String fanId,String strAmount*/) {
        // TODO Auto-generated constructor stub
        this.index=index;
        this.isclicked=isclicked;
    }
}

公共类MyAdapter扩展了BaseAdapter {

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return myArray.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return myArray.get(arg0);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row= null;
    row= View.inflate(getApplicationContext(), R.layout.row,null);
    TextView  tv= (TextView)row.findViewById(R.id.text);
    tv.setText(myArray.get(position));

    //datab= myArray.get(position);


    CheckBox cba= (CheckBox)row.findViewById(R.id.a);
    CheckBox cbb= (CheckBox)row.findViewById(R.id.b);
    final CheckBox cbc= (CheckBox)row.findViewById(R.id.c);

    cba.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(rowdata.get(position).isclicked)
        {
            rowdata.get(position).isclicked=false;
        }
        else
            rowdata.get(position).isclicked=true;
    }}
    );

    if (rowdata.get(position).isclicked) {

        cba.setChecked(true);

    }
    else {
        cba.setChecked(false);
    }

cbb.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(rowdatab.get(position).isclicked)
        {
            rowdatab.get(position).isclicked=false;
        }
        else
            rowdatab.get(position).isclicked=true;
    }}
    );

    if (rowdatab.get(position).isclicked) {

        cbb.setChecked(true);
    }
    else {
        cbb.setChecked(false);
    }

cbc.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(rowdatac.get(position).isclicked)
        {
            rowdatac.get(position).isclicked=false;
        }
        else
            rowdatac.get(position).isclicked=true;
    }}
    );

    if (rowdatac.get(position).isclicked) {

        cbc.setChecked(true);
    }
    else {
        cbc.setChecked(false);
    }
    b= (Button)findViewById(R.id.button1);

    return row;
    }
}

1 个答案:

答案 0 :(得分:0)

要从列表中获取所有数据,

您需要创建全局适配器

MyAdapter mAdapter;
通过以下代码获取所有数据

onClick按钮事件

for(Inforowdata row : mAdapter){
              //All rows adata here....
           }

for(int pos=0;mAdapter.getItemCount()-1;pos++)
     mAdapter.getItem(pos)//All rows adata here....