Regenerate checkboxes in listview from last session - android

I generate a working listview from an adapter with checkboxes:
final ArrayList<String> ArrayListHouse = new ArrayList<String>();
do
{
String ColumnNameTemp = myDBcursor.getString(ColumnIndexName);
ArrayListHouse.add(ColumnNameTemp);
}while (myDBcursor.moveToNext());
ListView = new ListView(getActivity());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_checked, ArrayListHouse);
listView.setAdapter(adapter);
Now I want do regenerate the state of the checkboxes when the app comes
up after a stop. It is no problem for me to save the checked checkboxes
with sharedprefs or my database. But I have a lot of trouble trying to
load the list with the predefined checkboxes. What is the right mechanism?
Any examples?
Bye Karsten

http://developer.android.com/reference/android/widget/AbsListView.html#setItemChecked(int, boolean)
Here is what you need:
AbsListView.setItemChecked(int, boolean)
by the way, how many checkboxes do you have? probably it would be better to put them in LinearLayout and ScrollView manually ?

Related

How to update adapter?

I have several spinners in a class.
For the first spinner I have set of data.
Other spiners will download data from server according to the selection of the fist spinner.
However, after downloading data, it does not update the spinner adapters.
Adapter for the second spinner:
sectionField = new String[] {"Error"};
adapterSection = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, sectionField);
adapterSection
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
section.setAdapter(adapterSection);
Code sample from fisrts spinner onItemSelected, which I believe should update the adapter:
sectionField = new String[data.length()];
sectionField = data;
section.setVisibility(View.VISIBLE);
adapterSection.notifyDataSetChanged();
You need to clean your adapter, then add items and notify.
adapterSection.clear();
adapterSection.addAll(data);
adapterSection.notifyDataSetChanged();
Hope it's help.

Getting checkbox values from ListView

I have a listview of that's tied to an array adapter. For the life of me I can't figure out how to get a list of the checked boxes in the listview.
CheckViewArrayAdapter adapter;
int[] intarray;
paramListView = (ListView) findViewById(R.id.datalog_paramselectlist);
// get all supported params
intarray = ConMan.Ecu.getSupportedParamArrayVals();
LinkedHashMap<Integer,String> hm = new LinkedHashMap<Integer,String>();
for( x=0;x<intarray.length;x++){
hm.put(intarray[x] , ConMan.Ecu.paramToText(intarray[x]));
}
adapter = new CheckViewArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice , android.R.id.text1, hm);
adapter.setBoolArray(ConMan.Ecu.getSelectedParamFlagArray());
// Assign adapter to ListView
paramListView.setAdapter(adapter);
I have a setOnItemClickListener for paramListView that works, but I just want to get the final set of checked checkboxes when the screen exits. I simply don't know where to look.
I have a setOnItemClickListener for paramListView that works, but I just want to get the final set of checkboxes when the screen exits.
I assume that you are not setting the choice mode for your ListView. If you do use:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then in your onPause() method, you can ask for an index of the checked rows with ListView#getCheckedItemPosition() and you don't need to change the checked state manually in your OnItemClickListener.

Adapt ArrayList into ListView

I have a XML File and I had parsed the data into the textView successfully now I want to bind that data into the ArrayList or List and display it in ListView.
But I don't know how to bind arraylist data into the ListView.
I have added all data into the arraylist successfully as mentioned in the below code.
List al = new ArrayList();
al.add(parser.getAttributeValue(null, "firstnames"));
Kindly please help me with the code syntax for the above issue.
Regards .
Thanks in advace
please have a look at the sample at http://codinglookseasy.blogspot.in/2012/07/android-list-view-sample.html
Instead of this
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, months);
setListAdapter(aa);
use this in your case
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
setListAdapter(aa);
You need to use an Adapter to bind a List to a ListView, like this:
List<String> list = new ArrayList<String>();
// add data to list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
Notice that the subtype of List (which is String) matches the subtype of the ArrayAdapter (also String). The layout android.R.layout.simple_list_item_1 defines how the String is displayed in every row. You can look up the specifics of this layout in your SDK, if you want you can also use you own layout. Hope that helps, good luck learning Android!

Android ListView setAdapter issue

I'm trying to bind data do a listview on android, but I'm not able to.
I saw some code on the internet and it worked, but I just don't know why, and I don't want to create a new ListView on the fly, I want to use the one that is listed on the main.xml
Why I can do this:
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,PEOPLE));
setContentView(lv);
But I can't do this:
ListView listPessoas = (ListView) findViewById(R.id.listPessoas);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listPessoas, PESSOAS);
listPessoas.setAdapter(adapter);
What is R.id.pessoas??
This works fine for me:
String[] x = new String[]{"AAA","BBB","CCC"};
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> test = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,x);
lv.setAdapter(test);
Your error here is when you are creating the new ArrayAdapter. You are passing R.id.listPessoas as the row view to use for each row. This is the id of the ListView. The adapter is looking for a layout id containing a text view to be used for each row of the list. Change the R.id.listPessoas to android.R.layout.simple_list_item_1 and your code should work. The simple_list_item_1 layout is just a TextView that the data will be bound to.
What you have written is not correct, the second parameter in the array adapter constructor should be the simple layout for each list item not again your list view.
If your have a custom complex layout for your list item you need to write a custom adapter as well.
If you are getting error.
getApplicationContext(); will work instead of 'this'.

How do you add array to listview

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Categories

Resources