Making gridview items clickable one by one - android

Level Selection GridView Image
I need to make Level 1 clickable and rest non clickable and when user clicks Level 1 it should make Level 2 clickable and rest non-clickable and so on so forth. Also if user is at Level 5 gridview should be clickable from level 1 to 4
myAdapter = new MyCustomAdapter(getActivity());
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//do something
}
private class MyCustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridObject object = my.get(position);
GridObject revers=reverseobj.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
holder = new ViewHolder();
holder.text = (ImageView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (check.get(position).equals("true"))
{
holder.text.setImageResource(revers.getName());
}
else {
holder.text.setImageResource(object.getName());
}
return convertView;
}
#Override
public int getCount() {
return my.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
public class GridObject extends ArrayList<GridObject> {
private int image;
private int state;
public GridObject(int name, int state) {
super();
this.image = name;
this.state = state;
}
public int getName() {
return image;
}
public void setName(int name) {
this.image = name;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}

If I read it correctly, you want a "current level" that allows you to check/uncheck any grid item up to that level and allow you also to click on the item for the next level to make that the new current level.
If so, then it is simple to modify your adapter by adding a currentLevel variable and adjusting getView() to enable clicking on items up to and including this level.
Here is an updated version of your adapter showing this:
private class MyCustomAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
private LayoutInflater mInflater;
// implement check as boolean array rather than what appears to be string array
private final SparseBooleanArray check = new SparseBooleanArray();
// keep track of the current level
private int currentLevel = 0;
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridObject object = my.get(position);
GridObject revers = reverseobj.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
holder = new ViewHolder();
holder.text = (ImageView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (check.get(position)) // used to be ...equals("true")
{
holder.text.setImageResource(revers.getName());
}
else {
holder.text.setImageResource(object.getName());
}
// make items up to current level clickable
convertView.setClickable(currentLevel < position);
// show unclickable items as disabled
convertView.setEnabled(currentLevel >= position);
return convertView;
}
#Override
public int getCount() {
return my.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// increase the current level if the item for the current level is clicked
currentLevel = Math.max(currentLevel, position + 1);
// toggle the checked state for the clicked item
check.put(position, !check.get(position, false));
// refresh the views
notifyDataSetChanged();
}
}
You can then enable this on your GridView in a similar way as before:
myAdapter = new MyCustomAdapter(getActivity());
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(myAdapter);
Note that I have implemented the missing check list as a SparseBooleanArray rather than what looks like an array of strings with "true" and "false" values in your version. You can change it back if you prefer.

I have Used a Simple Check list boolean = gridview items and It Works....By Default I have set the first value true and the rest value false.......
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id)
{
if (clickcheck.get(position).equals(true)) {
//complete some task then
position=position+1;
clickcheck.remove(position);
clickcheck.add(position,true);
}
else
{
Toast.makeText(getActivity(), "Click On Level "+position, Toast.LENGTH_SHORT).show();
}
`

Related

Listview showing previous selection [ANDROID]

In my app i am showing some items in list view, each row have one name and one tick image. Tick image only shows up when item is selected(checked), The selection is working fine but the problem is when a new item is selected tick of previously selected item doesn't goes invisible. How this can be fixed? Any help will be appreciated
here is my code
Adapter
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
Context context;
private ArrayList<String> responseList;
private LinearLayout parent_choice_list_container;
private TextView item_name;
private ImageView iv_choice_list_item_selected;
public ListAdapter (Context context, ArrayList<String> responseList) {
this.context = context;
this.responseList = responseList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedIndex(int ind) {
selectedIndex = ind;
notifyDataSetChanged();
}
#Override
public int getCount() {
return responseList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
}
parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container);
item_name = (TextView) view.findViewById(R.id.item_name);
iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected);
iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo);
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
} list_item_name.setText(responseList.get(position).getName());
return view;
}
}
FragmentPart (Item Click Listener)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
response.get(position).setSelected(true);
ListAdapter.notifyDataSetChanged();
}
It sounds like you're using a CheckBox.
Have you tried conditionally updating the Views visibility depending on whether the item isChecked (and not isSelected)?
you should set tag for each image to keep track of its state (0 = not selected/invisible , 1 = selected/visible) , e.g:
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setTag(1);
}
and you can add this :
if (iv_choice_list_item_selected.getTag() == 1) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
}

Android : Listview Change Button Text On clilck

When I click on the first button only clicked on one button it sets the button background ok . but problem when I scroll down i found more buttons was changed randomly look like picture.
See picture here.
part of code :- ContactSug_Adapter
public class ContactSug_Adapter extends ArrayAdapter {
List list = new ArrayList();
ImageLoader imgLoader = new ImageLoader(getContext());
private Context context;
public ContactSug_Adapter(Context context, int resource) {
super(context, resource);
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View row;
row = convertView;
final ContactHolder contactHolder;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row, parent, false);
contactHolder = new ContactHolder();
contactHolder.tx_id = (TextView) row.findViewById(R.id.usersName2);
contactHolder.tx_name = (TextView) row.findViewById(R.id.usersName);
contactHolder.sug_add = (Button) row.findViewById(R.id.sug_id);
row.setTag(contactHolder);
} else {
contactHolder = (ContactHolder) row.getTag();
}
final Contacts_Sug contacts = (Contacts_Sug) this.getItem(position);
contactHolder.image_tx.setImageResource(R.mipmap.ic_launcher);
contactHolder.tx_id.setText(contacts.getId());
contactHolder.tx_name.setText(contacts.getName());
contactHolder.sug_add.setTag(position);
contactHolder.sug_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
contactHolder.sug_add.setText("Selected");
}
});
return row;
}
public class ContactHolder {
TextView tx_id, tx_name,loadId;
ImageView image_tx;
public Button sug_add;
}/********* act
This is due to recycling of views.
You need to set the text "Selected" for the items you want to be and set the default text for the other items. You can do that using if-else statement.
For that you need to have a member variable in Contacts_Sug to hold the selectionPos like this -
private int selectionPos;
public void setSelectedPosition(int position){
selectionPos = position;
}
public int getSelectedPosition(){
return selectionPos;
}
And set it in button onClick() -
contactHolder.sug_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
contacts.setSelectedPosition(position); //Set position here
contactHolder.sug_add.setText("Selected");
}
});
And outside this onClick() where you are setting the values for your views.
Add this -
if(contacts.getSelectedPosition() == position){
//Set your button state to "selected" here
contactHolder.sug_add.setText("Selected");
} else{
//Set your button state to default here
contactHolder.sug_add.setText("Follow");
}

android - How to remove the view at some position in Listview

I want to know about how to delete child view at some position in list view and added the same at the top of the list. I tried using the following methods butit will generate the unsupported exception. How do that please can anybody help me.
lvAddedContacts.removeViewAt(AddUserView,nAddUserPosition );//Here i want to remove this view from the list
lvAddedContacts.addView(AddUserView, 0); //Add the same at the top
lvAddedContacts.invalidate();//list is refreshed
contactsAdapter.notifyDataSetChanged();
private class ContactsListViewAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public ContactsListViewAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
int nListSize = DH_Constant.AddedContactsList_obj.response.size();
if(nListSize > 0)
{
return nListSize;
}
else
{
return 0;
}
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, final ViewGroup parent)
{
ViewHolder holder;
convertView = mInflater.inflate(R.layout.added_contacts_list, null);
holder = new ViewHolder();
//getting the id's
holder.tvName = (TextView) convertView.findViewById(R.id.xrays_Name_tv);
holder.btnRemove = (Button) convertView.findViewById(R.id.xrays_removebtn);
//Name
String strName = DH_Constant.AddedContactsList_obj.response.get(position).Name;
holder.tvName.setText(strName);
//Change the color for differentiate the dicom and non dicom users
if(DH_Constant.AddedContactsList_obj.response.get(position).IsDicomUser)
{
holder.tvName.setTextColor(Color.rgb(0, 135, 137));
}
else
{
holder.tvName.setTextColor(Color.BLUE);
}
//Remove button Listener
holder.btnRemove.setBackgroundResource(R.layout.xrays_contact_removebtn_widget);
holder.btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
lnIcontactId = DH_Constant.AddedContactsList_obj.response.get(position).ImportedContactsID;
nDicomUser = DH_Constant.AddedContactsList_obj.response.get(position).IsDicomUser?1:0;
//Alert for remove the contact
showDialog(DIALOG_removebtnalert);
}
});
//Copy the view and position if the user is added
if(DH_Constant.blnAddUserStatus)
{
System.out.println("IContactID(xrays):"+DH_Constant.lnAddUserID);
if(DH_Constant.AddedContactsList_obj.response.get(position).ImportedContactsID == DH_Constant.lnAddUserID)
{
nAddUserPosition = position;
AddUserView = convertView;
}
}
return convertView;
}
class ViewHolder
{
TextView tvName;
Button btnRemove;
}
}
Don't remove Views backed by an Adapter yourself, as it may result in weired behavior!! Your Implementation of BaseAdapter looks strange to me, too. Ie:
public Object getItem(int position)
{
return position;
}
public long getItemId(int position) {
return position;
}
doesn't seem to make any Sense!
You should use an ArrayAdapter passing a Model to it and just implement getView(int, View, ViewGroup) accordingly. If you then want to move an Item inside on Top, all you have to do is just:
ArrayAdapter adapter = //initialize with your Model Objects and set it as ListAdapter
Object someItemInsideList = //some Item
adapter.remove(someItemInsideList);
adapter.insert(someItemInsideList, 0);
adapter.notifyDataSetChanged();

How to get the selected items from listview when clicking on button

I have a custom listview and i am selecting multiple items in the list, now i need to get those item names when clicking on a button the button will calls listfunction() method, you can see this in the below code.
public class Places extends Activity {
private ListView listView;
private static int selectedListItem = -1;
private Handler mHandler = new Handler();
private static Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
data.add("six");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//System.out.println(EfficientAdapter.saveState.get(position));
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
if(EfficientAdapter.saveState.get(position)=="selected"){
EfficientAdapter.saveState.put(position,"unselected");
}else
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
for(int i=0;i<data.size();i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
public void listfunction(View button) {
System.out.println("listfunction items are::::"+selectedListItem);
}
}
Custom listview should have custom adapter. In custom adapter you can find method named getView() along with view, position and parentgroup. When you are attempting click event, position will represent the index of the currently selected item.
public class MyArrayAdapter extends ArrayAdapter<String> {
#Override
public View getView(final int position, View convertView,ViewGroup parent) {
}
}
Use a global array to store the index of all the items clicked or touched in the list view by using the following:
listview.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> p, View view, int position,long id) {}
}
);
where position will be the index of the item starting from 0.then after clicking on button use the array to retrieve the positions of the items clicked

Listview items background

How to change the background image for list items, i am able to change only 1 item background at a time.
If there are 6 items on the list and if click on 3 items those 3 items background images should be changed, how it is possible
Below is my code
public class Places extends Activity {
private ListView listView;
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
} else {
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
}
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Try this,it should work logically.(I didn't try it,btw! :P)
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
...
private class EfficientAdapter extends BaseAdapter {
public static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
for(int i=0;i<5;i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Every time you click on listview item whole listview is getting refreshed.
So if you want to show previously selected items also then need to keep record of all the item selected. and when listview refreshed you need to check that this positions are previously selected or not according to that set your color.
Hope this help you
try this
android:background="#drawable/img_list_background_repeater"
if(clickWord.size()!=0)
{
for(int i = 0;i<clickWord.size();i++){
if(clickWord.get(i).equalsIgnoreCase(adListText[position])&&clickIndex.get(i)==position){
wordChk.setBackgroundResource(R.drawable.star_1);
}
}
}
Here clickWord is an arraylist of items selected. so when items get selected it will be added in this arraylist and when arraylist is refreshed i check all this using this loop.

Categories

Resources