anurag shrivastava anurag shrivastava - 1 month ago 14
Android Question

Enable button in activity after the last item clicked

I am creating an app which has a

button
and a
listview
. My problem is to enable this
button
after all the items in this
listview
has been clicked. Elseway the
button
will be disabled.

How can I checked if all items has been clicked or not?

here is my adapter please take a look

public class InstallAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
public List<InstallModel> galleryItems;
ProgressDialog dia;




public InstallAdapter(Activity activity,
ArrayList<InstallModel> galleryItems) {
this.activity = activity;
this.galleryItems = galleryItems;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
Log.e("getCount :", galleryItems.size() + "");
return galleryItems.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
Log.e("getItem :", galleryItems.get(position) + "");
return galleryItems.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
Log.e("getItemId :", position + "");
return galleryItems.indexOf(getItem(position));
}

class ViewHolder {

Button Title;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

final ViewHolder holder;
holder = new ViewHolder();
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(R.layout.custom_list_result, null);

Log.e("AgetView", galleryItems.size() + "");

holder.Title = (Button) convertView.findViewById(R.id.buttonInstall1);

Log.e("gallery postion", galleryItems.get(position) + "");
holder.Title.setText(galleryItems.get(position).getHeading());

holder.Title.setOnClickListener(new OnClickListener() {

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

String url = galleryItems.get(position).getApp_url();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
activity.startActivity(i);


}
});

/*if (position == getCount() - 1) {
btGoCongrts.setEnabled(false);
} else {
btGoCongrts.setEnabled(true);
}*/

return convertView;
}


}

Answer

An easy way could be adding a list of positions.

You can create an ArrayList of int. Everytime an user clicks on an item, you take this item position and you check if the ArrayList contains this index.

If it doesn't contain it, you add this int to the ArrayList.

After this you check the count of items in this ArrayList and if it's equals to the total count of items in your list, you are done.

Little code:

Add it inside list.setOnItemClickListener

ArrayList<int> myIndexes = new ArrayList<>()

//add onitemclick method with this body
if(!myIndexes.contains(position)){
  myIndexes.add(position);
}
if(myIndexes.length == myList.length){
  //enable button
}

Obiouvsly you have to replace names and implement methods, but this will work