Set ImageView visible/invisible from activity's adapter - android

I have an activity with a listview over an imageview (sort of a background for the list view). When the arraylist of the activity's adapter is empty, I want to set the imageview visible, and when there is one or more object(s) on my arraylist I want to set the imageview invisible.
Whenever I change the arraylist, I am calling this from the adapter:
((MainActivity)context).checkForLogo();
and the checkForLogo method on MainActivity:
public void checkForLogo()
{
ImageView logoView = (ImageView)findViewById(R.id.imageViewLogo);
if (adapter.getCount() == 0)
logoView.setVisibility(View.VISIBLE);
else
logoView.setVisibility(View.INVISIBLE);
}
I am getting NullPointerException. When I set visibility directly from the activity then it works (example from onCreate). But that won't work for my problem since I need to do it every time the adapter's arraylist is modified.

Try to declare your logoView global and initialize it inside onCreate

Related

How to access the adapter controls from activity?

In Base adapter, I have used some imageview, can I change the image src in activity?
You should not access the adapter views directly in activity. Write a method in adapter instead. Call adapter.change image from your activity.
public void changeImage(int imgResId) {
likeButton.setImageResource(imgResId);
}
If Image src is provided from activity then you can call notifyDataSetChanged() after changing data.
Eg.
adapter = new MYAdapter(data);
list.setAdapter(adapter);
//change data here
adapter.notifyDataSetChanged();
You need to update the model data which is associated with ViewHolder after that just notify the adapter and adapter will update your item with new model data.
Take example if you have Array of 10 objects and you want to update ImageView on 5th position then update your 5th model in ArrayList and call adapter.notifyDataSetChange() it will update desired image view.

GridView Adapter single selection mode

i have one Fragment in that i defined one GridView,so iam attaching listener for the LinearLayout from GridView Adapter so i click the LinearLayout of the Adapter it should change the background color of selected layout..
First in your girdview adapter class take on variable exp.
int selected;
then make on method in grid view like that
public void selectedPosition(int postion)
{
selected = postion;
}
then you can also put a code in getview method of gridview. Like this
if(position==selected)
{
imageView.setBackgroundColor(Color.WHITE);
}
else
{
imageView.setBackgroundColor(Color.parseColor("#578FFF"));
}
now in onitemclick of grid view you can post like that
adapter.selectcrop(position);
adapter.notifyDataSetChanged();
here is a solution example I will give you step by step i think you can solve your problem using that.

ListView item have a linearlayout , linearlayout addView then getview be called,and ImageView flashed.what can i do for image not to flash

user is a bean,head is an imageView,ll is a layout param. when click add button the follow code be called. then adapter call getview and every thing in this item will be refresh but data have no changed. so imageView inside of item,must be flashed.how wo fix it?
if (mUsers==null) {
mUsers = new ArrayList<ThreadUser>();
}
mUsers.add(0, user);
ll_Collect.addView(head,0,ll);
if (mUsers.size()>7) {
mUsers.remove(mUsers.size()-1);
ll_Collect.removeViewAt(ll_Collect.getChildCount()-1);
}
}
you should call this function
yourAdapter.notifyDataSetChanged();

Swap two elements in ArrayList inside custom adapter to show in Listview

I have a Listview and a custom adapter, I am trying to change swap two arraylist items inside the apdapter in onclick of button as listview item.
My code is
viewHolder.btnUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer pos = (Integer) v.getTag();
if (!pojos.get(pos).equals(pojos.get(0))) {
Collections.swap(pojos, pos, pos - 1);
notifyDataSetChanged();
}
}
});
But I can not see the changes in listview, though arraylist has modified, but UI changes has not reflected.
Thats because your adapter has already finished its work. The adapter will turn the data into views and pass those views to the list view. Notice that changing the order in the original collection wont change the views inside the list view. What you could do is remove the views and add them at the correct positions. Get access to the list view by doing viewHolder.getParent()
If pojos is a local final variable, make sure the one the adaptor uses still points to the same collection otherwise the anonymous class will swap 2 elements in an collection that's not being used.
I would recommend to pass the arrayList to the adapter again and setting adapter to the list view, all this before the notifyDataSetChanged(); method.

ListView getChildAt() stays null BUT if in onclick it works

I have a ListView that is filled with items of an ArrayAdapter.
Now I want to strike them out when click and also check in the beginning if they are already struck through(calling an external api etc).
But it only works in the onItemClickListener the method above it doesn't.
to understand it better, here is some code:
public void machListe() {
listViewArrayAdapter = new ArrayAdapter(getApplicationContext(),R.layout.task_item, ti);
taskListe.setAdapter(listViewArrayAdapter);
TextView ab=(TextView) taskListe.getChildAt(0);
So if I Debugg now I see that ab is null.
taskListe.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
TextView ab=(TextView) taskListe.getChildAt(0);
If I debug here, ab is NOT null.
You are trying to saft the state of the tasks inside your Views.
You should try to separate Model and View at this point.
Create a Model Object that contains a single task then extend ArrayAdapter and overwrite getView. In the getView you retrieve the correct task from the task list. Create the textview for the task and if the task is marked as done in your model you cancel out the text.
If you try to change the views in a list after they are created you would have to do this every time the list stops scrolling because the list will only hold childviews for the items that are shown on the screen, the other views are created during scrolling to save memory.
The only correct place to change a listview Item is in the getView method of the corresponding Adapter.
Have a look at the ListsView Tutorial on vogella.de for more information.
Setting the adapter will trigger a requestLayout but the actual layout is not done yet.
So taskListe won't have child-views yet when you call getChildAt(0) immediately after calling setAdapter
Here is the code and 2 is the index value
View view=adapter.getView(2, null, null);
TextView textView = (TextView) view.findViewById(R.id.textView1);
String value = textView.getText().toString();

Categories

Resources