Android: Dealing with CheckBox in a ListView - java

I'm using a SimpleAdapter to fill my ListView with below code.
SimpleAdapter adapter = new SimpleAdapter(this,
saleDriver.getOutstandings(clientId),
R.layout.outstanding_list_row, new String[] { "sale_id",
"sale_date", "invoice_number", "sale_total", },
new int[] { R.id.tt_check_box, R.id.tt_invoice_date,
R.id.tt_invoice_no, R.id.tt_invoice_tot });
setListAdapter(adapter);
According to above code, i have bind sale_id with CheckBox (R.id.tt_check_box) in the listview. When i run the program, value of checkboxes displayed right of the CheckBox as text. but i don't want to display them.
My actual need is, when user checked checkboxes, i need to get sale_ids bind with them.
How could i access sale_ids bind with checked checkboxes in my java programe ?

use
android.R.layout.simple_list_item_multiple_choice

I've attached code to obtain listview that works with multichoice checkboxes..
public class MultiChoiceActivity extends Activity {
String[] choice = { "Choice A",
"Choice B", "Choice C", "Choice D", "Choice E"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
choiceList = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
choice);
choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
choiceList.setAdapter(adapter);
}
Refernce Link

Override the getView() function in the SimpleAdapter and use the value of sale_id to check/uncheck the checkbox. Then use this custom adapter for your list.
EDIT:
In looking at another answer, my refined guess is that you need the Multiple Choice solution rather than this (since you need to find out the selections). This solution is more to display the checkbox based on existing data. If you still need this, let me know and I will post the sample code once I have access to my code.

Related

how to refresh Material Spinner in activity?

Using API through I was adding data in spinner that are added but that are not displayed in spinner at a time, so how can I refresh activity and newly added data to show at a time? i can used this link ....https://github.com/jaredrummler/MaterialSpinner/blob/master/README.md
(Schoolschool:userDetails.getSchools()) {
schoolList.add(school.getSchoolName());}
schoolDropDown.setItems(schoolList);
schoolDropDown.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
}
});
Use this method
adapter.notifyDataSetChanged();
You need to define the Adapter using setAdapter(...), for the Spinner, too. This defines the layout to be used for each individual item. i.e.
schoolDropDown.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_dropdown_item_1line, schoolList));
You only need to call setAdapter() once and you call adapter.notifyDataSetChanged() to update the data.
You need to set adapter to the spinner for displaying data
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, schoolList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schoolDropDown.setAdapter(dataAdapter);
After data change you need to call
dataAdapter.notifyDataSetChanged();
Try like this:
cityAdapter = new ArrayAdapter<>(frgmActivity, android.R.layout.simple_spinner_dropdown_item, listCityNames);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCitys.setAdapter(cityAdapter);
Or
Try below two lines :
String[] schoolsArr = schoolList.toArray(new String[schoolList.size()]);
schoolDropDown.setItems(schoolsArr);
Instead of this line: schoolDropDown.setItems(schoolList);

Android Studio ListView Custom

I recently started developing for android and now im stuck! I created a listview but the standard colour is black, now i want to be able to show the text in whatever colour i want. This is the activity.
public class DisplayMalePage extends ActionBarActivity {
String[] maleArray = { "a","b","c"};
private ListView maleListView;
private ArrayAdapter maleArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_male_page);
maleListView = (ListView) findViewById(R.id.maleList);
maleArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, maleArray);
maleListView.setAdapter(maleArrayAdapter);
}
But this (i think due to simple_list_item_1) gives me a black colour. Also i would like the input of my array to be strings so it wil be easier to change language.
Eventually i would like to have a list with 2 top texts, a dividing bar and then the rest of the list (they will all be clickable).
I hope someone understands what i mean haha.
You cannot change any part of the listviews layout using the default ArrayAdapter. You need to define your own CustomArrayAdapter.
http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown
This link is a very useful guide on how to do that.
Create your own layout which you will use in your list item. Make sure it contains a Textview with id text1. Something like this:
layout/my_list_item.xml
<TextView
android:id-"#+id/text1"
android:textColor="#android:color/white"
...... />
Then use this layout in your ArrayAdapter intialization instead of android.R.layout.simple_list_item_1.
maleArrayAdapter = new ArrayAdapter(this, R.layout.my_list_item, maleArray);
Note: If you want something more complex other than showing a simple text, you should use custom ArrayAdapter, as #user3567040 have pointed out.

Android autocompletetextview show previous input

I got a AutoCompleteTextView , but I want it to use the users' previous input instead of some strings I said it to complete, so question is: How do I do this?
AutoCompleteTextView do not propose this feature by default but you could implement it yourself.
storing each user input in a file or a database (I recommend to use a database)
retrieving those input and setting an adaptater to the AutoCompleteTextView.
Example to set an adpatater with pre defined string array from Google doc:
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Instead to use a fix array COUNTRIES like in the example above, use a String array retrieved from the database,
If you want your AutoCompleteTextView to show suggestions based on some string rather than taking input from user then just call:
myAutoCompleteTextView.setText("cou", true);
and then in case drop down is not shown, call:
myAutoCompleteTextView.showDropDown();

Add the contents of a database to a ListView via a Cursor

So I'm gunna try give as much information as I can to get this sorted. My application contains a database with the standard methods implemented; retrieving a record or all records returns a Cursor.
My application has a ListView, a text box and an add button. Here's what I'm trying to achieve (please note, the first is the most important):
I would like to display the current contents of the database in the ListView area.
I want to have the button insert whatever is in the text box into the database (and the ListView should automatically update to show the insertion)
I would like the ability to tap an item in the ListView and have it deleted from the database.
I have tried to tackle the first bullet point through assigning a Cursor to the return of the getAllRecords() method; Cursor c = dba.getAllRecords();. I have tired to get it to add field entries via a for-loop which didn't turn out too well.
Button add;
EditText tm;
ListView lv;
DBAdapter dba = new DBAdapter(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general);
add = (Button) findViewById(R.id.button1);
tm = (EditText) findViewById(R.id.editText1);
lv = (ListView) findViewById(R.id.generalList);
Cursor c = dba.getAllRecords();
c.moveToFirst();
// Trying to add database contents to ListView here.
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
insertIntoDatabase();
}
});
}
If you have a Cursor from a database and whant to show into a list, and is simple data like a few text values. Take a look at SimpleCursorAdapter.
The situation is this, you need an adapter, the adapter is the one that loads from the cursor and push into the layout that represent the correspoding list item. Anything that inherits from a CursorAdapter is good. All depends on how much flexibility you need, so you may implement as much as you need.
This is a snnipet from a sample app that I posted at github.
https://github.com/soynerdito/trainningSample/blob/master/src/com/example/sample/app/Dashboard.java
Also the app is in the marketplace. Is just a very simple app used during a "trainning" the app is called "Nerdito Sample" search for it, try and the code is on github. Adds items to a database and show in list.
Sample:
Device device = new Device();
Cursor cursor = mdb.get(device);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cursor, new String[] {
device.mDescription.mName, device.mDeviceID.mName },
new int[] { android.R.id.text1, android.R.id.text2 }, 0);
// Create Cursor and set to List
mDeviceListView.setAdapter(adapter);

I need help with my first Spinner!! Not Displaying values

This is the first time I am trying to use a spinner and I need some help.. I made a layout with a spinner object on it and then I also made a array.xml with the numbers that I would like in the spinner in it. I run the following code and the screen displays without any values in my spinner??
public class SpinnerExaple extends Activity {
private Spinner numbersSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.numbersSpinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> numbersArray =
new ArrayAdapter<String>(this, R.layout.main,
getResources().
getStringArray(R.array.numbers));
}
}
You got the reference to the spinner, you got your AdapterArray set correctly- but you didn't attach the adapter to your spinner.
Add the line:
numbersSpinner.setAdapter( numbersArray );
You never set your numbers array into the spinner. Try adding the following at the end of the method:
numbersSpinner.setAdapter(numbersArray);
Also check out the hello-spinner tutorial.

Categories

Resources