How to get listview checked items to Array in android? - android

I have a list view with check boxes.I want to get all the checked items ids or data in the particular position.
Please any one help me with sample code
Thanks in advance

I found this answer from Internet
Working properly what i need
my_sel_items=new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for(int i = 0; i < lv_items.length ; i++)
{
if (a.valueAt(i))
{
/*
Long val = lView.getAdapter().getItemId(a.keyAt(i));
Log.v("MyData", "index=" + val.toString()
+ "item value="+lView.getAdapter().getItem(i));
list.add(lView.getAdapter().getItemId((a.keyAt(i))));
*/
my_sel_items = my_sel_items + ","
+ (String) lView.getAdapter().getItem(i);
}
}
Log.v("values",my_sel_items);

Related

How to properly notifyItemMoved, insert and removed in RecyclerView old data list when i get new fresh list to assign to it

I am populating new data in my RecyclerView adapter all at once, so there are no insert or remove one item actions.
So simply, i have an old list and when some Event occurs i get the new list and i can assign the new list to the old.
Problems are i cannot make properly the animation for each item in the old list
when item has new position in the new list (should notifyItemMoved from old position to new)
when there is a new item in the new list (should notifyItemInserted with that position in the new list)
when the old item is not present in the new list (should notifyItemRemoved with that position)
Here is something i have now, which i thought will work for first case - item move to new position:
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyItemMoved(i, j);
}
}
}
}
}
currentAdapterData = newData;
However it does not work as expected, and there is difference between logs(which are correct) and the list appearing on the phone(with wrong items positions, some duplicates, buggy etc.)
So how can i make it work? With notifyItemMoved, notifyItemInserted and notifyItemRemoved?
I don't want to just use NofifyDataSetChanged, because it refresh the entire list instead of just updating the items with animations that have changed.
It looks like that your new data is also a form of list, not a single item. I think this could be a good candidate for using DiffUtil in the support library.
Here is also a nice tutorial for it.
It will allow you to calculate the difference in the new data and only update needed fields. It will also offload the work asynchronously.
You just need to implement a DiffUtil.Callback to indicate if your items are the same or the contents are the same.
You update your recyclerView like that:
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
diffResult.dispatchUpdatesTo(yourAdapter);
Simply use DiffUtil like
final MyDiffCallback diffCallback = new MyDiffCallback(prevList, newList);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
Create a Callback by extending MyDiffCallback and override methods and do as needed.
public class MyDiffCallback extends DiffUtil.Callback
// override methods
Well for this ,I feel this would be the easiest.Just follow it ->
Replace this
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyItemMoved(i, j);
}
}
}
}
}
currentAdapterData = newData;
with
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyDataSetChanged();
new CountDownTimer(250, 250) {
#Override
public void onTick(long millisUntilFinished) {
Log.d("millisUntilFinished", "" + millisUntilFinished);
}
#Override
public void onFinish() {
notifyItemMoved(i, j);
}
}.start();
}
}
}
}
}
this will update the values and after 250 millisecond(1/4th a second),the value with be moved with animation.

Which Json structure suitable for this work?

This function generate dummy data for this Structure .
private void createDummyData() {
for (int i = 1; i <= 10; i++) {
VerticalDataModel dm = new VerticalDataModel();
dm.setSectionTitle("Section " + i);
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 10; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setSingleItemModelha(singleItem);
allSampleData.add(dm);
}
Know for generating this data from internet , i need json structor for this work .(I use volley library)
Thanks <3
{
data : [
{
"title":"section1",
"items":[{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]
},
{
"title":"section2",
"items":[{"name":"item3","url":"url3"},{"name":"item4","url":"url4"}]
}
]
}
so basically you have array of objects which contains title and items as its property. Again items is another array of objects which contains name and url as its property.

print ftp files in list view

I m developing a app that downloads list of files from ftp server. the file names are downloaded using array. how can i pass this array to main activity and display it in a list view.i have a very little knowledge in this domain so kind of help is appreciated.thanking you in advance
FTPFile[] ftpFiles = mFTPClient.listFiles("/public_html/");
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
You need to use ListView widget. The simplest way of displaying a list of items in a list view would be to use predefined ArrayAdapter class and an existing Android layout for the rows. Please check this tutorial for detailed explanation how to use ListView.
http://www.vogella.com/articles/AndroidListView/article.html

Show Multiple Item Details in TextView not only Sinlge

Getting value for only single Item in TextView from CartActivity, but want to fetch detail for all Item(s) placed in Cart
Like: I have 4 items in Cart, but once i am trying to show these item details in another activity, so here i am only getting single item detail (only for 4th Item, not for all fours) why?
CODE:
for (int i = 0; i < Session.sItem_Detail.size(); i++) {
String title=Constants.sItem_Detail.get(i).get(
ProductInformationActivity.KEY_TITLE);
String qty=Constants.sItem_Detail.get(i).get(
ProductInformationActivity.KEY_QTY);
String cost=Constants.sItem_Detail.get(i).get(
ProductInformationActivity.KEY_COST);
textDetails =(TextView)findViewById(R.id.txtDetails);
textDetails.setText("(Title:" +title + "(Qty:" + qty + ")" + "(Cost:" + "Rs." + cost + ")");
public class Session {
public static ArrayList<HashMap<String, String>> sItem_Detail = new ArrayList<HashMap<String, String>>();
}
So here is my question, how can i get item details for all items stored in Cart, not just for one..
Try this
use append(charctersequence) instead of setText(charctersequence)
textDetails.append("(Title:" +title + "(Qty:" + qty + ")" + "(Cost:" + "Rs." + cost + ")"+"\n");

Checked items in Multiple Choice Dialog

I'm displaying a List of Objects in a MultipleChoiceDialog. Another List contains all Objects who are already checked.
My Lists:
List<Participant> participants = datasourceParticipant.getAllParticipants();
List<Participant> participantsConference = datasourceParticipant.getAllParticipants(conference.getId());
In order to display them in the MultipleChoiceDialog, I build my List like this:
participantsNames = new ArrayList<String>();
for(int i = 0; i < this.participants.size(); i++) {
participantsNames.add(i, participants.get(i).getFirstname() + " " + participants.get(i).getLastname());
}
participantConferenceNames = new ArrayList<String>();
for(int i = 0; i < this.participantsConference.size(); i++) {
participantConferenceNames.add(i, participantsConference.get(i).getFirstname() + " " + participantsConference.get(i).getLastname());
}
Afterwards, I create the necessary String array ...
final CharSequence[] items = participantsNames.toArray(new CharSequence[participantsNames.size()]);
to display it in the MultipleChoiceDialog
builder.setMultiChoiceItems(items, null, null);
How do I add the checkedItems to the MultipleChoiceDialog. Or is there a much easier way to do it?
You have to pass in a boolean[] instead of null with the values that you want checked. The most straightforward way to accomplish this is to use a set:
Set<Participant> set = new HashSet();
set.addAll(datasourceParticipant.getAllParticipants(conference.getId()));
boolean[] checked = new boolean[participants.size()];
for (int i =0; i < participants.size(); i ++) {
checked[i] = set.contains(participants.get(i));
}
....
builder.setMultiChoiceItems(items, checked, null);
For that to work your Participant class must implement hashCode();

Categories

Resources