Getting checkbox values from ListView - android

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.

Related

Open a row in listview automatically (android project)

For example; I have long listview and it has 30 lines(items). I want to show this listview but after open the screen, it will show 15. lines. That is, the middle of the listview will be shown automatically. Is it possible? (NOTE: I don't want to show 15. line as first row.)
UPDATE:
i don't want to delete rows. I have listview and it works well. I want to show the middle of list. Scrool will flow until 15. row and i will see all of them but it will show 12. or 15. row when listview opens.
Bind your ListView to ArrayAdapter
final ListView lv = (ListView) findViewById(R.id.lv);
String[] fruits = new String[] {
"Cereus peruvianus",
"Bacupari",
"Beach Plum",
"Black raspberry"};
// Create a List from String Array elements
final List fruits_list = new ArrayList(Arrays.asList(fruits));
// Create an ArrayAdapter from List
final ArrayAdapter arrayAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, fruits_list);
// DataBind ListView with items from ArrayAdapter
lv.setAdapter(arrayAdapter);
Remove / Delete first item from List. You may remove multiple in a loop.
fruits_list.remove(0);
// Notify adapter
arrayAdapter.notifyDataSetChanged();
Edit:
// For scrolling to specific item in your list
lv.smoothScrollToPosition(15); // Here 15 is the position of the item
Answer is lv.setSelection(15);

Regenerate checkboxes in listview from last session

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 ?

How do you set the method "isChecked()" state to default to true in a ListView?

I need the checkboxes in my listview checked by default so that it returns true for the value of isChecked().
I am using the Simple Multiple choice list item layout provided by the sdk supplied in android.R.layout.simple_list_item_multiple_choice
I have set the ListView adapter like this:
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter);
I have used the following code for Setting the Check boxes Default Checked in my application
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter);
l1 = getListView();
for (i = 0; i < getListAdapter().getCount(); i++) {
l1.setItemChecked(i, true);
}
and the output, listview came with multiple check boxes default checked

spinner with no select option

I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.

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