How to use the standard functions after modification ListView - android

I use this Pull-to-Refresh, it works, but after the installation can not call standard functions for listView, such as:
public static RefreshableListView lv = (RefreshableListView) findViewById(R.id.messageListView); //this work
lv.setOnItemLongClickListener(longClickListener); //don`t work
lv.setSelection(listViewCountPosition); //don`t work
Parcelable state = lv.onSaveInstanceState(); //don`t work
lv.onRestoreInstanceState(state); //don`t work
Compiler writes error.

RefreshableListView implements FrameLayout ( not listview). Listview is added as a child to the FrameLayout. You have to call getListView() for list view instance.
something like this..
public RefreshableListView lv = (RefreshableListView) findViewById(R.id.messageListView);
lv.getListView().setOnItemLongClickListener(longClickListener);

Related

How do I call findViewById() in ListFragment without overriding onCreateView?

I don't want to override onCreateView because there is no need. With a ListFragment all I am doing is taking an array of data, putting it in an ArrayAdapter and calling setListAdapter(arrayGoesHere) and then I have my populated ListFragment.
I am calling findViewById inside onActivityCreated() as it is the recommended place to find and store references to your views. And as you know, this is called after onCreateView() in the Android framework.
I can't do viewReturnedFromOnCreateView.findViewById because I'm not using onCreateView.
getActivity().findViewById doesnt work, because I'm actually not sure why.
getListView().findViewById doesnt work (because the element im trying to access is not a child of getListView()).
Edit 1: getView() didn't work either, I forgot to mention I am using the support library, android.support.v4.app.ListFragment. not sure if that matters
This is my code:
public class SomeFragment extends ListFragment {
private int someButtonId;
private Button someButton;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] someList = getResources().getStringArray(R.array.someData);
ArrayAdapter<String> someAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, someList);
setListAdapter(someAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
context = getActivity().getApplicationContext();
someButtonId = context.getResources().getIdentifier("someButton", "id", context.getPackageName());
someButton = (Button) getActivity().findViewById(someButtonId);
Log.i("hello", someButton.toString()); //Null pointer exception
}
}

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();

android auto refresh listview item

First I setup a ListView adapter
this.chapters = new ArrayList<String>();
ListView lvChapters = (ListView) findViewById(R.id.mangaChapterList);
lvChapters.setAdapter(new ChapterListAdapter(context, R.id.lvChapters, this.chapters));
After that, I popular some data into "this.chapters" in an AsyncTask class. But the listview still empty??
Show the code for your ChapterListAdapter, are you sure you are calling the registered observers?
If ChapterListAdapters is extending an ArrayAdapter, then you should call notifyDataSetChanged.
public void notifyDataSetChanged ()
Since: API Level 1
Notifies the attached View that the underlying data has been changed and it should refresh itself.
Reference your adapter through any variable like this
ChapterListAdapter adapter = new ChapterListAdapter(context, R.id.lvChapters, this.chapters);
and then set adapter.
pters.setAdapter(adapter);
and then just call adapter.notifyDataSetChanged(); after you add the data to the this.chapters.

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