Maintaining the previous items after the update listview - android

I have a problem with updating my ListView. I don't want to lose my ListView's data after the first initialization.
I searched on this site and some other sites and found different solutions. At last I used the below code, but I didn't get any result.
private void handelpost(List<Posts> posts,Page pages) {
ListView listView;
final CustomListAdapterForPostOrgan adapter;
if (pages.getCurrentPage() > 1) {
listView = (ListView) findViewById(R.id.lv_for_post_organ);
adapter = new CustomListAdapterForPostOrgan(this, posts);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
} else {
listView = (ListView) findViewById(R.id.lv_for_post_organ);
adapter = new CustomListAdapterForPostOrgan(this, posts);
listView.setAdapter(adapter);
}
}

You're creating new Adapter with new data every time. Which is not advisable when you're working with pagination. You need to do following changes.
Declare your adapter and list view and an ArrayList class level.
public class MyActivity extends Activity{
private ListView mListView;
private CustomListAdapterForPostOrgan mAdapter;
private List<Posts> mPosts;
}
Initialize your ArrayList, then Adapter and then set it as adapter to ListView once in onCreate() of Activity.
private void onCreate(Bundle savedInstanceState) {
super.onCreate();
//After you've called setContentView() etc etc.
mPosts = new ArrayList();
mListView = (ListView) findViewById(R.id.lv_for_post_organ);
mAdapter = new CustomListAdapterForPostOrgan(this, mPosts);
mListView.setAdapter(mAdapter);
}
When you receive new data, first add it to your class-level array and then call notifyDataSetChanged() like this:
private void handelpost(List<Posts> posts,Page pages) {
//add new posts to existing array..
mPosts.addAll(posts);
//since adapter already has your array, simply make it refresh.
mAdapter.notifyDataSetChanged();
}

Related

List of Text and Grid of Images on the same screen android

I'm following the following tutorial http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html
I'm attempting to change the list view to use an array of strings, and display text rather than images for the bottom part of this application.
I'm not entirely sure what I should be using as my ListView ID here. It seems like I can only use a findViewbyID, but I'd rather just give it an array of strings to populate that list view. Am I missing something simple here?
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
ListView lv = (ListView)findViewById(????);
lv.setAdapter(adapter);
Here is the code
public class ListViewActivity extends Activity {
private ExpandableHeightGridView gridView;
private ListView listView;
//drawables array for listview
private static final int[] corpImage = { R.drawable.apple, R.drawable.blackberry_logo,
R.drawable.google, R.drawable.microsoft, R.drawable.mozilla, R.drawable.oracle };
static final String[] CORPS = new String[] { "Microsoft", "Google", "Apple" };
//drawables array for gridview
private static final int[] osImage = { R.drawable.bbos, R.drawable.firefox_os,
R.drawable.ic_launcher, R.drawable.ios, R.drawable.tizen, R.drawable.ubuntu_touch,
R.drawable.wpos, R.drawable.symbian };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
addGridViewAsListViewHeader();
setAdapters();
}
/**
* set header for listview (it's a gridview)
*/
#SuppressLint("InflateParams")
private void addGridViewAsListViewHeader() {
View header = (View) getLayoutInflater().inflate(R.layout.layout_gridview, null);
listView.addHeaderView(header);
gridView = (ExpandableHeightGridView) header.findViewById(R.id.gridview);
// set expand to show all gridview rows
gridView.setExpanded(true);
}
/**
* set adapters for list view and gridview
*/
private void setAdapters() {
// convert int array to Integer array by apache common lang library
Integer[] osImageList = ArrayUtils.toObject(osImage);
Integer[] corpImageList = ArrayUtils.toObject(corpImage);
//Added by me but not working correctly
//ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
//ListView lv = (ListView)findViewById(R.id.image);
// lv.setAdapter(adapter);
// set listview and gridview adapters
CustomAdapter gridViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, osImageList);
CustomAdapter listViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, corpImageList);
gridView.setAdapter(gridViewAdapter);
listView.setAdapter(listViewAdapter);
}
}
If your goal is to only use one string in each line, and display it as a list of items you can simply do this by using array adapters as follows.
ArrayList<String> companies = new ArrayList<>();
companies.add("Google");
companies.add("Microsoft");
// Find a reference to the {#link ListView} in the layout
ListView companiesListView = (ListView) findViewById(R.id.list);
// Create a new {#link ArrayAdapter} of earthquakes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, earthquakes);
// Set the adapter on the {#link ListView}
// so the list can be populated in the user interface
companiesListView.setAdapter(adapter);
In your layout file you should declare a list view where you want to display this list of items. In this case id of that list view would be "list"

How to call multiple ArrayList one by one in GridView Android

I have more then one ArrayList think that ArrayList1,ArrayList2,ArrayList3... In ArrayList1 have some images showing it in GridView android, when I click on images it should show ArrayList2 in that GridView only and ArrayList2 also having images. Same for ArrayList2 and ArrayList3. How to achieve this
Assuming that all your ArrayLists contain the same object type.
Create a new ArrayList, lets call it AdapterArrayList and use it as the main ArrayList for the Adapter. Now If you want to use ArrayList1, clear() elements from AdapterArrayList and then addAll() the ArrayList1. and of course notifyDataSetChanged() for the Adapter.
Do the same when you want to use ArrayList2 and ArrayList3.
[Edit]
For example:
public class MyAdapter extends BaseAdapter {
List<Image> mImagesList;
public MyAdapter(List<Image> imagesList) {
mImagesList = imagesList;
...
}
}
And to use that adapter with the ArrayLists you have:
public class MyActivity extends Activity {
MyAdapter mAdapter;
List<Image> mainList = new ArrayList();
List<Image> arrayList1 = new ArrayList();
List<Image> arrayList2 = new ArrayList();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mAdapter = new MyAdapter(mainList);
myListView.setAdapter(mAdapter);
}
private void switchToList1() {
mainList.clear();
mainList.addAll(arrayList1);
mAdapter.notifyDataSetChanged();
}
private void switchToList2() {
mainList.clear();
mainList.addAll(arrayList2);
mAdapter.notifyDataSetChanged();
}
}

Instantiating and setting a new custom adapter works, but notifyOnDataSetChanged() does not

I'm experiencing an issue with notifyOnDataSetChanged(). I am able to get my list view to load new data by creating a new CustomAdapter, but not by using notifyOnDataSetChanged().
private void repopulateListView(JSONObject resp) throws JSONException {
arrayList.clear();
List<MyObject> newArrayList = getData();
arrayList.addAll(newArrayList); //I've checked that arrayList contains new data
/* WORKS, new data is loaded in the list view */
adapter = new CustomAdapter(MainActivity.this, arrayList);
listView.setAdapter(adapter);
}
whereas the following doesn't work - the list view is not refreshed, it simply stays as it was before retrieving new data. I have checked that the new data is correctly retrieved.
private void repopulateListVIew(JSONObject resp) throws JSONException {
arrayList.clear();
List<MyObject> newArrayList = getData();
arrayList.addAll(newArrayList); //I've checked that arrayList contains new data
/* DOES NOT WORK, list view does not refresh with new data */
((BaseAdapter)adapter).notifyOnDataSetChanged();
}
My adapter was defined like so:
adapter = new CustomAdapter(MainActivity.this, arrayList);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//additional code
startActivity(DetailsActivity);
}
}
);
UPDATE: #Sabeeh's answer worked. Based on answers from #Sabeeh and #ViniciusRodriguesLima below, I had to edit my CustomAdapter.java as follows to reference the list variable (in addition to adding the update() method):
public class CustomAdapter extends ArrayAdapter<MyObject> {
private Context context;
private List<MyObject> list = new ArrayList<>(); //add this variable
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inf = LayoutInflater.from(getContext());
View customView = inf.inflate(R.layout.custom_beer_row, parent, false);
//final MyObject chosenObj = getItem(position); Changed to the statement below
final MyObject chosenObj = list.get(position); //this is CORRECT
...
}
I have tried suggestions from the following pages to no avail:
notifyDataSetChange not working from custom adapter
notifyDataSetChanged not working
Android: notifyDataSetChanged(); not working
Following the instructions on the top-voted answer, using adapter.clear() seems to clear my arrayList as well, so by the time I use notifyOnDataSetChanged(), the arrayList is empty)
Any help would be appreciated.
Please try this:
private void repopulateListVIew(JSONObject resp) throws JSONException {
arrayList.clear();
List<MyObject> newArrayList = getData();
arrayList.addAll(newArrayList);
adapter.update(arrayList);
}
and in your CustomAdapter class write following function
public void update(ArrayList<MyObject> list) {
//replace arrayList variable with your class ArrayList variable
this.arrayList = list;
this.notifyDataSetChanged();
}
when you pass your data source object to the adapter, you just pass it by value. When you refresh your data in the Activity scope, your adapter doesn't know anything about it. That's why Sabeeh created an update method inside the adapter, this way he can updates its adapter data source. – Vinicius Rodrigues Lima
I hope this helps you.
Declare your adapter variable as ArrayAdapter and try using following code -
private void repopulateListVIew(JSONObject resp) throws JSONException {
adapter.clear();
List<MyObject> newArrayList = getData();
adapter.addAll(newArrayList);
adapter.notifyOnDataSetChanged();
}

How to add elements in simple_list_item_2?

I want to add elements in simple_list_item_2. but i don't know how to add. I created 2 ArrayLists. Here is my code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listv=(ListView) findViewById(R.id.listview);
arraylist1=new ArrayList<String>();
arraylist1.add("a");
arraylist1.add("b");
arraylist1.add("c");
arraylist2=new ArrayList<String>();
arraylist2.add("1");
arraylist2.add("2");
arraylist2.add("3");
adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_2,arraylist1);
listv.setAdapter(adapter);
}
ArrayList<String> arraylist1,arraylist2;
ArrayAdapter adapter;
ListView listv;
}
Just as you added the elements before you can add them anytime you want as,
arraylist1.add("a");
And then you just have notify the adapter that the data had been changed, to do that put the below line of code after you add or remove data from the array list...
adapter.notifyDatasetChanged();

Using json with listview in Android

I have a dummy listview that I have setup in my app that uses the following code:
public class Roster extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roster);
// ListView resource.
mainListView = (ListView) findViewById( R.id.rosterListView );
// List of names.
String[] players = new String[] { "Dude1", "Guy2"};
ArrayList<String> playerlist = new ArrayList<String>();
playerlist.addAll( Arrays.asList(players) );
//ArrayAdapter using the list.
listAdapter = new ArrayAdapter<String>(this, R.layout.rosterrow, playerlist);
// Add more Players, If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Person8" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
getActionBar().setDisplayHomeAsUpEnabled(true);
}
which gives me a listview of three players.
I now want to use the json object to populate the list instead of using a string. How can I populate the list using my json object?
Try to get the value of Json by key and put it in to string array.
JsonObject myjsondata = new JsonObject(jsondata.toString());
String[] players = new String[] {myjsondata.getString("key") , myjsondata.getString("key1")};

Categories

Resources