mardi 24 mars 2015

how to get data from listview if check any of the 3 checkboxes how to get the value of checked box and text from row

how to know which checkbox is checked and how retrieve data of each row and save it into a file


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


public class MyAdapter extends 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;
}


}





Aucun commentaire:

Enregistrer un commentaire