How to add elements in simple_list_item_2? - android

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

Related

Maintaining the previous items after the update listview

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

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

add data to a list view from database

i want to add some info from my database to a list view, i have been able to pass that information to a textView just don't know how to get it to a list view this is my code.
public class ViewClass extends Activity{
ListView myList;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
Initialize();
Database viewAgenda = new Database(this);
viewAgenda.open();
String data = viewAgenda.getAgendaData();
viewAgenda.close();
test.Add(data);
}
private void Initialize() {
// TODO Auto-generated method stub
myList = (ListView) findViewById(R.id.panel_bottom);
}
}
You can add the data you get from the Database in ArrayList and then add this arraylist in List
ArrayList<String> list_data = new ArrayList<>();
list_data.add("item"); // likewise add list items here
ArrayAdapter<String> adapter= new ArrayAdapter<>(context, android.R.layout.activity_list_item, list_data);
myList.setAdapter(adapter);
here is Example
http://wiki.remobjects.com/wiki/Displaying_Data_with_ListView_and_BaseAdapter_(Android)
http://androidsolution4u.blogspot.in/2013/09/android-populate-listview-from-sqlite.html
may be helps you.
You must use the adapter class to link a database with a ListView. Please see the below code.
ListView lv = (ListView) findViewById(The Id of your ListView);
//fetch your data to a Cursor
Cursor c = "The result of a select statement";
SimpleCursorAdapter adap = new SimpleCursorAdapter(this,
R.layout.item2,//Your layout for the list view
c,
new String[]{"fullname", "balance"}, //field names
new int[]{R.id.rowData, R.id.rowMoney});//The ids of elements from your list view layout. The field "fullname" will be loaded into the "R.id.rowData" element.
lv.setAdapter(adap);

Android: Spinners loose their values when I add dynamically new ListView entries on click of spinner item?

I am developing android apps. In my apps having two activities, first activity is displaying list and in second activity having spinner and listview in the same activity and when user click on the item from spinner the listview will be displayed. when user navigate from first activity to second activity then spinner is properly populated with listview. but problem is that after listview displayed properly then spinner item was blank. I don't know where i am doing wrong. Please anybody have solution.
Here i am posting few code of Second Activity
public class ProjectDetailActivity extends SherlockListActivity {
private List<String> list = new ArrayList<String>();
private ArrayList<HashMap<String, String>> list2 = new ArrayList<HashMap<String,String>>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_detail);
//get spinner item from server when user comes from first activity.
new LoadPhaseData().execute();
//Listener for Phase spinner
projSpinnerPhase.setOnItemSelectedListener((OnItemSelectedListener) new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//get listview when user click item from spinner
new LoadPhaseData().execute();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//this adapter for listview when click item from spinner
ListAdapter phaseAdapter = new SimpleAdapter(getApplicationContext(),
list2, R.layout.phase_avail_list_item,
new String[] {PHASE_NAME}, new int[]
{R.id.phaseName});
setListAdapter(phaseAdapter);
}
private class LoadPhaseData extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Here I am calling web service for spinner and listview
}
#Override
protected void onPostExecute(Void result) {
//following adapter for spinner item
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getApplicationContext(),android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
projSpinnerPhase.setAdapter(dataAdapter);
}
}
}
Thanks in advance
This is because you are assigning new Adapter to the Spinner with new values while you are adding new rows to Spinner. Spinner losses its previous row along with the previous data and get another Adapter with new data rows. You need to append these value to the existing data-holder (may be an array or ArrayList) and then call the adapter.notifyDataSetChanged();
You need to move ArrayList to the class-level, (i.e make it class field) then while you have downloaded data. just append new data to the list and call adapter.notifyDataSetChanged();

ListView update

I have two activities with ListView. This two activitises have individual layout-file. First have myListView01, second have myListView02. How to update listView's adapters?
I do so:
ListView list = (ListView)findViewById(R.id.myListView01);
myAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(myAdapter);
I remove some items from the array01 and I need to update my listView without this elements.
But this solution to ListActivity does not work:
myAdapter.notifyDataSetChanged();
my code:
package ...;
import ...;
public class Class01 extends Activity
{
ArrayAdapter<String> mAdapter;
ArrayList<String> array01 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout01);
ListView list = (ListView)findViewById(R.id.my_list01);
mAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(mAdapter);
}
public void onListItemClick (ListView parent, View v, int position, long id)
{
array01 = deleteElementByPosition(array01, position);
mAdapter.notifyDataSetChanged(); //does not work
}
public void deleteElementByPosition()
{
...
}
}
You have not set onItemClickListener for your ListView.
Extend your Class01 from ListActivity. Activity class doesn't have onListItemClick() method so it is never invoked. Or set your listener with setOnItemClickListener() for your ListView.

Categories

Resources