I'm trying to display a list of items fetched from a url but I only want to fetch 20 of them at a time... so I've implemented an OnScrollListener to fetch the items when the users is on the last item of the listview.
The items are fetched but my only problem is that the listview is not updated: here's my code so far:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.activity_event_speakers_alphabetically, container,false);
listView = (ListView) parentView
.findViewById(R.id.speakersAlphabeticallyActivityList);
nameOrderedMembers.addDataBatch(communityMembers);
nameOrderedAdapter = DelegateViewStateAdapterFactory
.makeUserListAdapter(getActivity(),
nameOrderedMembers.getSortedData(), DelegateViewStateAdapterFactory.OrderType.NAME);
listView.setAdapter(nameOrderedAdapter);
AQuery aQuery = new AQuery(listView);
aQuery.id(R.id.speakersAlphabeticallyActivityList).scrolled(new EndlessScrollListener());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (nameOrderedAdapter.getItem(arg2) instanceof User) {
User u = (User) nameOrderedAdapter.getItem(arg2);
Intent i = new Intent(getActivity(),
UserProfileFragmentActivity.class);
startActivity(i);
}
}
});
return parentView;
}
Now - in the AsynkTask in the onPostExecute method - i notify the adapter like this:
communityMembers.addAll(newMembers);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
nameOrderedAdapter.addAll(communityMembers);
nameOrderedAdapter.notifyDataSetChanged();
}
});
but the listview doesn't refresh - only if I go back and reopen the activity - the new data is shown.
So how can I notify the adapter that there's new data to display?
call listView.invalidateViews() after notifyDataSetChanged()
add below line after nameOrderedAdapter.notifyDataSetChanged(); :
listView.setAdapter(nameOrderedAdapter);
use
notifyDataSetInvalidated()
method
if you are using any custom ListView or Custom GridView like /
HorizontalListView
Related
I have a listView with 2 items in the list, the first item in position 0 responds to clicks but the other does not! i have tried to copy the same onclick method and changed the if statement to the list item in position 1 but it is not working out for me so i am looking how to get this working if someone could help me? i have included the only code i think is needed to resolve this,
public class TopLevelActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_level);
//Create an OnItemClickListener
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
Intent intent = new Intent(TopLevelActivity.this,
DrinkCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
//what ive tried to open the foodCategoryActivity list item
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 1) {
Intent intent = new Intent(TopLevelActivity.this,
FoodCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
}
}
thank you.
Currently using two different click listeners for same ListView to do different on different items click in ListView with is not valid way to do task according to click position in ListView.
Use single click listener and inside onItemClick method use switch-case or if-else ladder for doing task like:
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
}
else if (position == 1) {{
}
}
};
and also remove following line which you are using two times:
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
because no need to create listView and call setOnItemClickListener multiple times just do it once.
You can only set one OnItemClickListener
you need to differentiate in the implemented method via either the position or the View object itself:
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
//code for drink category
}
else {
//code for food category
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
I have a FragmentActivity that controls a ListFragment; that ListFragment contains a generic ListView, Adapter, and draws an ArrayList from a Singleton that I have created.
When in the onCreateView method of my ListFragment I put the following code:
public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
Bundle SavedInstantState) {
cycleviewfragment = viewInflation.inflate(
R.layout.cycleviewfragment_page, container, false);
context = getActivity().getApplicationContext();
Singleton mySingleton = Singleton.getInstance();
basicList = (ListView) cycleviewfragment.findViewById(android.R.id.list);
adapter = new ArrayAdapter<listControlObject>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
mySingleton.getA1());
this.setListAdapter(adapter);
addButton = (Button) cycleviewfragment.findViewById(R.id.addbutton);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),
listaddactivity.class);
getActivity().startActivity(myIntent);
}
});
basicList.setOnItemClickListener(new ListView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.d("basicListtester", "Testing onClickItem call");
Intent myIntent = new Intent(getActivity(),
listdetailactivity.class);
myIntent.putExtra("selectedObjectIndex",arg2);
getActivity().startActivity(myIntent);
}
});
adapter.notifyDataSetChanged();
return cycleviewfragment;
}
Any ideas as to why when I add items to my list they do not react and the OnItemClick is not called?
Thanks guys.
[Update]
I tried implementing it with basicList.setAdapter(adapter); which still did not work.
also tried having my ListFragment implement OnItemClickListener and added the method to the class; which did not work either.
Since you use ListFragment you shouldn't set onItemClickListener to your list. There is already a method in ListFragment that you should override.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Do your thingy.
}
Use
basicList.setListAdapter(adapter);
instead of
this.setListAdapter(adapter);
Use same instance of ListView for setting Adapter and setOnItemClickListener.
you should override onListItemClick, since your class extends ListFragment. From the doc:
This method will be called when an item in the list is selected.
Subclasses should override.
Here the documentation
OK guys, so after 4 hours of struggling i finally decided to ask here my problem. I developed a slide menu with a listView. I got the slide menu all good and i managed to make it doing just one action if one of the Items is clicked. My problem is that i do not know how to manage it further. I am thinking about making a switch case statement but the problem is that i cannot use "position" due to: "position cannot be resolved to a variable"
Here is my list code so far:
public class RandomList extends SherlockListFragment{
String[] list_contents = {
"Add me on Google+",
"Linkedin",
"W&T Top Tips Forum",
};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.list, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
final ListView list1;
list1 = getListView();
list1.setAdapter(new ArrayAdapter<String> (getActivity(),android.R.layout.simple_list_item_1, list_contents));
list1.setOnItemClickListener(
new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Object listItem = list1.getItemAtPosition(position);
//long index = arg0.getSelectedItemId();
switch(position){
}
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://plus.google.com/u/0/115467961053443202630/about"));
startActivity(myWebLink);
}
}
);
}
}
Use the variable arg2 in the switch case.This indicates the position of the item which has been clicked.
I have been attempting to add a footer button to the end of my list. My list is working appropriately however I cannot figure out why I cannot add the footer view. My end goal is to inflate the view and add a footer button via xml however I need to get this to work at least first.
public void onActivityCreated(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// storing string resources for users inventory into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
ListView lv = getListView();
// Binding Array to ListAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.inventory_list, R.id.label, adobe_products);
// LoadMore button
Button btnScan = new Button(getActivity());
btnScan.setText("Scan Inventory");
// Adding Load More button to list view at bottom
lv.addFooterView(btnScan);
this.setListAdapter(adapter);
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
//Intent i = new Intent(PhotosFragment.this, InventoryItem.class);
// sending data to new activity
//i.putExtra("product", product);
//startActivity(i);
}
});
btnScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Starting a new async task
//Intent send_Scan=new Intent(PhotosFragment.this, ScanActivity.class);
//PhotosFragment.this.startActivity(send_Scan);
}
});
}
Edit :
When running the application it seems to be stuck in an infinite loop. Consistently loading.
You can't get ListView in onCreateView.
Use in onStart():
#Override
public void onStart() {
super.onStart();
LayoutInflater inflater = getActivity().getLayoutInflater();
getListView().addFooterView(inflater.inflate(R.layout.ly_footer_acreditacao, null));
}
I'm using the tabbed layout (with swipe).
Here I have 3 tabs with controlled by a SectionsPagerAdapter. Each tab is a ListFragment.
Now I want to get an event fired when one of the items in the list is clicked. I would like a listener for each tab.
Here's the code now (Which isn't working, event is not fired).
public class NyhederFragment extends ListFragment {
public static final String ARG_SECTION_NUMBER = "section_number";
private static final String TAG="NyhederFragment";
private List<Item> newsItems;
private ArrayList newsHeadlines;
private ArrayAdapter adapter;
private BroadcastReceiver updateReciever;
public NyhederFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView newsList = new ListView(getActivity());
newsList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
newsList.setId(R.id.list);
DatabaseHelper dbConn = new DatabaseHelper(getActivity());
newsItems = dbConn.getAllItemsFromNews();
newsHeadlines = new ArrayList();
for(Item i : newsItems){
newsHeadlines.add(i.getTitle());
}
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, newsHeadlines);
setListAdapter(adapter);
newsList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("debug", "single click");
}
});
dbConn.close();
getActivity().registerReceiver(updateReciever, new IntentFilter("ArticlesUpdated"));
return newsList;
}
}
What is it, I'm doing wrong?
Thanks a lot in advance!
If you are using ListFragment then you can simply use its override method onListItemClick()
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
It is due to custom list item. The default focus is with custom list item (button/textview). It causes this issue.
Please add android:descendantFocusability="blocksDescendants" in root layout of list element.
Hope this will help someone.
change setListAdapter(adapter); to newsList.setAdapter(adapter);
If you wanted to create or re-use or an existing handler that implements AdapterView.OnItemClickListener, rather than implementing onListItemClick() within the ListFragment, you can do so by getting a reference to the ListView of the ListFragment and setting its listener. In your ListFragment.onResume() you could do:
#Override
public void onResume() {
super.onResume();
ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("TAG", "Stop touching me");
}
});
}
In my case, I can use the same listener from an Activity with a ListView, a ListActivity or a ListFragment, e.g.
listView.setOnItemClickListener(new MyListViewOnClickListener(getActivity()));