Android ListView with radio button - android

> I want to change the selected item's checkbox color of listview so that when user selects the item its color gets changed this is my code :
final ListView listView = (ListView)findViewById(R.id.lvcancelorder);
//this is my listview
ArrayAdapter<String> adapter =
new ArrayAdapter<String(this,android.R.layout.simple_list_item_single_choice, countries); //this is the adapter
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) //this is the item click event
{
selectedFromList = (listView.getItemAtPosition(position)).toString();
}}); //this is the selected item from the listview
How can I do this ? Please suggest something.
I know to do this thing List view custom adapter but have no idea to do it.

It is recommended to create your own custom adapter extends from BaseAdapter or ArrayAdapter and on selection of item simply change background color of root layout view.
use following link to get know about how to create a listview with checkboxes.
see here

You can try this:
//Your button to get selected list
getChoice = (Button)findViewById(R.id.getchoice);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, android.R.layout.simple_list_item_multiple_choice, countries);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
String selected = "";
int cntChoice = myList.getCount();
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++){
if(sparseBooleanArray.get(i)) {
selected += myList.getItemAtPosition(i).toString() + "\n";
}
}
Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();
}});

Related

How to find the position of item in listview

I have created a Custom listView where i put some text and image but when i clicked in list view their is no response.I have created a custom Listview.
if any one known then please tell me.
my code is here
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<pdf> pdfList;
//the listview
final ListView listView;
//initializing objects
pdfList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list);
//adding some values to our list
pdfList.add(new pdf("Book1", "First Year"));
pdfList.add(new pdf( "Book2", "First Year"));
//creating the adapter
ListAdptot adapter = new ListAdptot(this, R.layout.customlistview, pdfList);
//attaching adapter to the listview
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
if(position==0)
{
Toast.makeText(MainActivity.this, "Position 0", Toast.LENGTH_SHORT).show();
}
else if(position==1)
{
Toast.makeText(MainActivity.this, "Position 1", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
Get list item positon click as below
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
String value = listView.getItemAtPosition(position).toString();
}
}
);
If you have any clickable views inside your list item, then it will consume click event of your ListView item click.
Set android:focusable="false" on your Button or ImageButton if you have any.
Reference SO answer

Creating dynamic spinners and getting value of every spinner, Whenever a spinner item changed, get all spinner values

I'm developing a e-com app. I'm trying to create dynamic spinner. Spinner is dependent upon product attributes. I'm able to create spinners also mapped data on them but I want to get all spinners selected item whenever a spinner change its data so I can match to correct variant of product.
Here is my code snippet
final List<Attribute> attributes = product_.getAttributes();
for (i = 0; i < attributes.size(); i++) {
ArrayList<String> spinnerArray = (ArrayList<String>) attributes.get(i).getOptions();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
Spinner spinner = new Spinner(getActivity());
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
dynamicProductProperties.addView(spinner);
}
First, you need to set same instance of OnItemSelectListener to your all spinners. Let your activity or fragment implement OnItemSelectListener then call spinner.setOnItemSelectListener(this);
In onItemSelected method you can run this code to have selecteds.
ArrayList<String> selectedList = new ArrayList();
for (int i = 0; i < dynamicProductProperties.getChildCount(); i++) {
View view = dynamicProductProperties.getChildAt(i);
if (view instanceof Spinner){
String selected = (String) ((Spinner) view).getSelectedItem();
selectedList.add(TextUtils.isEmpty(selected) ? "" : selected);
}
}
Good luck
Emre

How do I pull the selected items from a simple_list_item_multiple_choice with checkboxes?

I am trying to pull the items selected in a multiple checkbox ListView but getcheckitempositions() keeps returning null.
Relevant code:
Checkbox binding:
final ListView list = (ListView) whoView.findViewById(R.id.listView);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_multiple_choice, friendList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = ((CheckedTextView) view);
checkedTextView.setChecked(!checkedTextView.isChecked());
}
});
Trying to pull the items:
ListView list = (ListView) whoView.findViewById(R.id.listView);
List<String> invited = new ArrayList<String>();
SparseBooleanArray checked;
if (list != null) {
checked = list.getCheckedItemPositions(); {
for (int i = 0; i <= checked.size(); i++) {
if (checked.valueAt(i)) {
invited.add(list.getAdapter().getItem(i).toString());
}
}
}
}
I suspect it has to do with the getcheckitempositions() pulling from the ListView and not the checkboxes, but I'm not sure how I would go about fixing this.

android on click list item i want to select 2 item(tick mark)

I a newbie,i would like to ask on click list view i want to select (tick) mark on particular item,i know how to get position on click item.Here is my code below
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[]{"FirstName",
"FullName",
"Simple List View In Android"};
ArrayAdapter < String > adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setClickable(true);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
listView.getAdapter().getView(position, null, null).performClick();
}
});
}
}
Thank you..Please
Check this sample project. It may help.
Use this if you need multiple selections
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
If you need a brief description visit
http://theopentutorials.com/tutorials/android/listview/android-multiple-selection-listview/

How to delete ListView Items by ID

I'm trying to delete the items in my ListView by their ID. Currently they are being deleted by the first position on the List. I can have many items in the list.(example) When I try to delete the sixth item from the list the first item is deleted and the sixth remains.
How do I get the List to delete by ID like in this case m1aa or stringl1. My database deletes stringl1 perfectly but it remains in the list unless it's in the top position.
int pos;
long id;
final ListView lv = getListView();
I forgot to add this to my question
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
adapter=new ArrayAdapter<String>(this,
R.layout.singlelistview,listItems);
setListAdapter(adapter);
Sorry
Button button5 = (Button) dialogView.findViewById(R.id.button5);
button5.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView textviewlay1 =(TextView)findViewById(R.id.m1aa);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
final String str=lv.getItemAtPosition(pos).toString();
Log.i("ListView", "onLongListItemClick string=" + str);
{listItems.remove(pos);
adapter.notifyDataSetChanged();}
controller1.deletename(stringl1);
}});
It seems that you do not set variable pos.It is an int variablae,so it is 0 when you call:
listItems.remove(pos);
And as result first item(that it's index is equal to 0) will be deleted.
This is happening because getView() called for all the items then you click on button to delete row at that time position is set to last row thats why last item is deleted every time. You have to set position as tag to button and on the time of deletion you can get index by this tag. Thanks
int pos;
long id;
final ListView lv = getListView();
Button button5 = (Button) dialogView.findViewById(R.id.button5);
button5.setTag(pos);
button5.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView textviewlay1 =(TextView)findViewById(R.id.m1aa);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
final String str=lv.getItemAtPosition((Button)v.getTag()).toString();
Log.i("ListView", "onLongListItemClick string=" + str);
{listItems.remove(String.vauleOf((Button)v.getTag()));
adapter.notifyDataSetChanged();}
controller1.deletename(stringl1);
}});
It can also be done through onItemClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
list.remove(item);
adapter.notifyDataSetChanged();
}
});

Categories

Resources