mercredi 25 mars 2015

Get All Checked Items From a ListView and Retrieve it's To Another Activity

I want to achieve :


1- A ListView of all pdf in device the user can select multiple list items (each item )Using Checkbox (I d'ont want to use multiple selection mode).


2- in action bar button click event with counter (to indicate the number of elements Checked). On button click event , we retrieve the selected list view items using Intent and start another activity (ResultActivity) and displays the result in ListView.


3- save state of ResultActivity when user exits the application so that I can reload this state when the application restarts


I have finished the first part using code bellow


Custom List adapter :



public class CustomList
extends ArrayAdapter<File> implements CompoundButton.OnCheckedChangeListener
{
SparseBooleanArray mCheckStates;
Context context;
List<File> objects;

public CustomList(Context paramContext, int paramInt, List<File> paramList)
{
super(paramContext, 0, paramList);
this.context = paramContext;
this.objects = paramList;
mCheckStates = new SparseBooleanArray();

}

public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
{
View localView = ((LayoutInflater)this.context.getSystemService("layout_inflater"))
.inflate(R.layout.itemcheck, null, true);
TextView localTextView1 = (TextView)localView.findViewById(R.id.textView1);
CheckBox chkSelect = (CheckBox) localView.findViewById(R.id.checkbox_android);

localTextView1.setText(((File)this.objects.get(paramInt)).getName());

chkSelect.setTag(paramInt);
chkSelect.setChecked(mCheckStates.get(paramInt, false));
chkSelect.setOnCheckedChangeListener(this);
return localView;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);

}

public void toggle(int position) {
setChecked(position, !isChecked(position));

}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

mCheckStates.put((Integer) buttonView.getTag(), isChecked);

}
}


MainActivity :



public void search(File paramFile) {
String pdfPattern = ".pdf";

File arrayOfFile[] = paramFile.listFiles();

if (arrayOfFile != null) {
for (int i = 0; i < arrayOfFile.length; i++) {

if (arrayOfFile[i].isDirectory()) {
search(arrayOfFile[i]);
} else {
if (arrayOfFile[i].getName().endsWith(pdfPattern)){

File localFile = arrayOfFile[i];
this.result.add(localFile.getAbsoluteFile());


}
}
}
}
}



public void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.listcheck);


//
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

StringBuilder result = new StringBuilder();
for(int i=0;i<customList.mCheckStates.size();i++)
{
if(customList.mCheckStates.get(i)==true)
{
// here where i want Add the bundle to the intent and start the ResultActivity

}

}
Toast.makeText(MainActivity.this, result, 1000).show();
}

});

this.progressDialog = ProgressDialog.show(this, "Please Wait ...", "Collecting Pdf files ...");
this.progressDialog.setCancelable(true);
new Thread(new Runnable()
{
public void run()
{
try
{
Thread.sleep(5000L);

MainActivity.this.progressDialog.dismiss();
return;
}
catch (Exception localException)
{
MainActivity.this.progressDialog.dismiss();
}
}
}).start();
File localFile = Environment.getExternalStorageDirectory();
// Log.v("externalpath", localFile);
search(localFile);
for (int i = 0;; i++)
{
if (i >= this.result.size())
{

Collections.sort(this.result, new FilerByName());


localCustomList = new CustomList(this, 0, this.result);
this.list = ((ListView)findViewById(R.id.list2));
this.list.setAdapter(localCustomList);
return;
}
Log.v("result", ((File)this.result.get(i)).toString());
}
}

protected void onSaveInstanceState(Bundle paramBundle)
{
super.onSaveInstanceState(paramBundle);
}
class FilerByName
implements Comparator<File>
{

FilerByName() {}

public int compare(File paramFile1, File paramFile2)
{
return String.CASE_INSENSITIVE_ORDER.compare(paramFile1.getName(), paramFile2.getName());
}
}

}




Aucun commentaire:

Enregistrer un commentaire