How to change elements of an Adapter from outside? - android

I have a gallery with a custom adapter. I'm showing an image along with some texts and buttons.
I want to change the texts that are associated with the images when the corresponding buttons are clicked.
If I put the button handler inside the adapter, it doesn't catch the call.
I found out that I should moved the handler to the activity.
But the problem is that I don't have access to the Adapter anymore. Any solutions?
public class MyAdapter extends BaseAdapter { ...
MyGallery myGallery = (MyGallery) findViewById(R.id.mygallery);
myGallery.setAdapter(new MyAdapter(this, ...));

You should set the OnClickListener in the getView function of the adapter. When you inflate the view you can make a call to findViewById on the view to get a reference to the Button. You can then set the OnClickListener on the Button object itself.

Move
MyGallery myGallery = (MyGallery) findViewById(R.id.mygallery);
myGallery.setAdapter(new MyAdapter(this, ...));
into your Activity
Oh yeah and make a member reference to the Adapter eg MyAdapter myAdapter = new MyAdapter(....

Related

Requesting logic for listview fragment

I'm making an activity with a listView fragment in it. I am having a button click on the MainActivity load items for an rss feed that populate the listview fragment.
My question is, how should I go about setting it up? I already have the code for it.
My plan now is to have the button click on MainActivity point to my method in my RSSFragment which extends ListFragment. I'm confused about some things like which activity to pass here:
#Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
lvRss.setAdapter(adapter);
progressDialog.dismiss();
}
}
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
I know it's a long question.
Thanks.
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
If you are in Activity the use this or Activity.this . IF you are in Fragment the use getActivity().
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
Use setListAdapter method setting Adapter to ListFragment

setEmptyView not working in custom ListView

I have a custom ListView which is one of the tabs of a viewPager.
public class MyListView extends ListView
I instantiate an object of my class and setEmptyView using
ListView myList = new MyListView(context);
View emptyView = myLayoutInflater().inflate(R.layout.mylist_empty_view, null);
myList.setEmptyView(emptyView);
myList.setAdapter(myAdapter);
However the emptyView never shows up, does not have a parent (which ideally should be the listview) and I cannot see it in the hierarchyViewer. What do I need to do?
AdapterView only manages the visibility of emptyView. If the adapter is empty or null, the visibility of the ListView will be GONE while emptyView is VISIBLE, but to make it show up, you need to add it to the layout yourself.
Just try using this layout
FrameLayout myLayout = new FrameLayout(context);
myLayout.addView(myList);
myLayout.addView(emptyView);
instead of myList directly.

Activity refresh command from custom listview

Environment: Android 2.2+
I have an Activity. The Activity creates a custom listview with its adapter and populates it. When the user clicks on an item (an ImageView thumbnail) in the list, a context menu is shown. The repercussion of selection is that the view of Activity needs to be refreshed.
My question is: How can I send a command from the onClick of the custom adapter to the Activity asking to refresh itself?
Activity
...
ListView lv;
lv = (ListView) findViewById(R.id.lvlist);
lv.setAdapter(null);
MyCustomAdapter lvAdapter = new MyCustomAdapter(...);
lv.setAdapter(lvAdapter);
lv.setOnItemClickListener(myClickListener);
...
MyCustomAdapter (There is a thumbnail ImageView inside each list item called ivLogo)
...
ivLogo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Carry out some tasks
// At this point inform Activity that it needs to refresh
}
});
It must be noted that I'm not looking for anything in myClickListener. I want a signal from the thumbnail's onClickListener.
I scoured the internet, but could not find anything helpful for this specific case.
Any help/pointers will be much appreciated.
TIA
-sph
add this line to your onClick()
lvAdapter.notifyDataSetChanged();
you write his code whenever you need adpater refreshed...its worked
lvAdapter .notifyDataSetChanged();

how to merge buttons for each images in gallery?

I have created a gallery, and it displays a lot of images in a horizontal view.
I need 3 buttons for each image below it. I don't know how to create this.
How can I achieve this layout?
This is my expected design:
You could have a custom ListView, and the layout for each element has the image and the buttons. To do that, you need to subclass BaseAdapter and inject this adapter in your ListView.
final BaseAdapter myCustomAdapter = new GalleryAdapter(this);
final ListView gallery = (ListView) findViewById(R.id.gallery);
gallery.setAdapter(myCustomAdapter);
public class GalleryAdapter extends BaseAdapter {
....
implements the class
....
}
Your getView method in the adapter would spawn the layout for individual item, and fill it dynamically with what you want.

nullpointer on ListView (not using ListActivity)

I've created a layout in XML and it includes a ListView. I can use the ListView just find inside my AsyncTask. In onCreate, I did: listView = (ListView)findViewById(R.id.listView1);
And in onPostExecute of my AsyncTask, I did: listView.setAdapter(new OfferAdapter(Main.this, offers)); THIS WORKS JUST FINE.
But if I try listView.setDivider(null) in onCreate, then the app crashes with a nullpointer there.
How am I supposed to grab hold of my ListView when I'm not using ListActivity?
If findViewById is returning null, I'd make sure you are calling it after setContentView, and that you are using the correct ID.
One option you have is to set the divider in xml, ie:
android:divider="#drawable/icon".
If you want more control, verify that you are following this syntax in your activity:
public class DividerExampleActivity extends Activity {
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
listView.setDivider(null);
}
}
I've examined the ListView.java source code, and setting it to null should be fine.
Another option might be to make yourself a very thin, transparent divider in xml (say 0.5dp).

Categories

Resources