Get spinner value from scroll able listview in android - android

I have a list view. Each list view contain spinner and I want to get the value of each spinner. But it doesn't work. It is ok if the listview is not scroll able.
private void checkPrice(){
float total = 0;
Log.d(TAG,"This is qty: "+ quantity);
for (int i=0; i < mProductItems.size(); i++){
View view = mListView.getChildAt(i);
float eachTotal = 0;
float discountAmount = 0;
// Get Quantity
Spinner getQtyView = (Spinner) view.findViewById(R.id.spQty);
String getQty = getQtyView.getSelectedItem().toString();
Integer qty = Integer.parseInt(getQty);
// Get Line item amount view
TextView txvAmount = (TextView) view.findViewById(R.id.txvAmount);
// Get product by view position
OEDataRow eachProduct = (OEDataRow) mProductItems.get(i);
Integer eachPrice = eachProduct.getInt("retail_price");
// Check Discount
if (eachProduct.getBoolean("promotion_disc")){
float promo = (float) eachProduct.getInt("promotion_amount")/100;
Log.d(TAG,"this is promo: "+promo);
discountAmount = (float)( eachPrice * qty ) * promo;
}else {
discountAmount = 0;
}
eachTotal = (float)( eachPrice * qty ) - discountAmount;
txvAmount.setText(String.format("%.2f", eachTotal));
Log.d(TAG, "Each Total: " + eachTotal);
total = eachTotal + total;
Log.d(TAG,"Total: "+ total);
}
btnCheckout.setText("Checkout: "+String.format("%.2f", total));
}
Thanks for your time.

I've the same problem. I'll solve like this.Try this. This is just example Activity.
public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List<String> listString;
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList<String>();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}
List<String> spinnerData = new ArrayList<String>();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fillAdapter();
}
private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ViewHolder {
Spinner spinner;
}
class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>();
public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}
public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
}
In activity_main.xml:
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:stackFromBottom="false"
android:transcriptMode="disabled" >
</ListView>
</LinearLayout>
In item_list_spinner_adaoter.xml:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Related

Delete an item from Array List contact to Custom List View always delteing the latest one

I have a custom list view, contains delete button and spinner (the spinner contain A-E characters).
And I have an issue with deleting the true row from my custom list view.
Custom list view code:
public class customListView extends BaseAdapter
{
public Activity context;
ArrayList<MyActivity.UserProperties> userPropertieses;
public String[] spinnerValues;
public LayoutInflater inflater;
public customListView(Activity context, ArrayList<MyActivity.UserProperties> userPropertieses, String[] spinnerArray)
{
super();
this.context = context;
this.userPropertieses = userPropertieses;
spinnerValues = spinnerArray;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() { return userPropertieses.size(); }
#Override
public Object getItem(int i) { return null; }
#Override
public long getItemId(int i) { return 0; }
class ViewHolder
{
Button btnRemove;
Spinner spinner;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup)
{
final ViewHolder holder;
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.custom_layout, null);
holder.spinner = (Spinner) view.findViewById(R.id.spinner);
holder.btnRemove = (Button) view.findViewById(R.id.bu_Remove);
// populate spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(view.getContext(), android.R.layout.simple_spinner_item, spinnerValues);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner.setFocusable(true);
holder.spinner.requestFocus();
holder.spinner.setAdapter(dataAdapter);
view.setTag(holder);
// remove user implementation
holder.btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("custom list view debug", "i = " + i); // debug. verify i value is correct
((MyActivity) context).deleteUser(i);
}
});
}
else
holder = (ViewHolder) view.getTag();
return view;
}
}
And my main activity code looks like this:
public class MyActivity extends Activity
{
ListView listView;
ArrayList<UserProperties> userProperties = new ArrayList<UserProperties>();
customListView adapter;
SensorManager sensorManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
for (int i = 0; i<5; i++) {
userProperties.add(new UserProperties());
}
listView = (ListView) findViewById(R.id.listView);
String[] spinnerValues = new String[] {"A", "B", "C", "D", "E"};
adapter = new customListView(MyActivity.this, userProperties, spinnerValues);
listView.setAdapter(adapter);
}
public void deleteUser (int index)
{
Log.i("debug", "Removing item " + index); // the index is really true and the true node deleting from the ArrayList but somehow the latest delete from the UI
userProperties.remove(index);
adapter.notifyDataSetChanged();
}
}
When I click on the Remove button deleteUser method called with the right index. but although the right node deleting from userProperties ArrayList somehow after notiftDataSetChanged is still alive
and the latest node delete.
So, How can I delete the right node/row (from the ArrayList and UI...)
Thank you!
EDIT:
Just to be clear, i variable contain true index. The true node deleted from the ArrayList. but something append after I called notify method.
I prefer to stay with BaseAdapter and not implement ArrayAdapter. Thank you!
EDIT 2:
After more debugging I found out my question was wrong. the true row really deleted just spinner values somehow update their values. I cannot close the question because it already answered. Thanks.
((MyActivity) context).deleteUser(i);
This line will always delete the first value from the ListView
You can use CAB (contextual action bar)
See if the code helps you(it's basically a ListActivity with a custom adapter to hold the status of checked items(+ different background)):
public class CABSelection extends ListActivity {
private ArrayList<String> mItems = new ArrayList<String>();
private SelectionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for (int i = 0; i < 24; i++) {
mItems.add("Name" + i);
}
// R.layout.adapters_cabselection_row is a LinearLayout(with green
// background(#99cc00)) that wraps an ImageView and a TextView
mAdapter = new SelectionAdapter(this,
R.layout.adapters_cabselection_row, R.id.the_text, mItems);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cabselection_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
StringBuilder sb = new StringBuilder();
Set<Integer> positions = mAdapter.getCurrentCheckedPosition();
for (Integer pos : positions) {
sb.append(" " + pos + ",");
}
switch (item.getItemId()) {
case R.id.edit_entry:
Toast.makeText(CABSelection.this, "Edited entries: " + sb.toString(),
Toast.LENGTH_SHORT).show();
break;
case R.id.delete_entry:
Toast.makeText(CABSelection.this, "Deleted entries : " + sb.toString(),
Toast.LENGTH_SHORT).show();
break;
case R.id.finish_it:
nr = 0;
mAdapter.clearSelection();
Toast.makeText(CABSelection.this, "Finish the CAB!",
Toast.LENGTH_SHORT).show();
mode.finish();
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
nr = 0;
mAdapter.clearSelection();
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle(nr + " rows selected!");
}
});
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
l.setItemChecked(position, !mAdapter.isPositionChecked(position));
}
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(Color.parseColor("#99cc00")); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(Color.RED);// this is a selected position so make it red
}
return v;
}
}
}
Another way
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
getView(final int i,
Do not make i final. You did that to use i in onClick(). But that is not possible. So remove the final. Add:
holder.btnRemove.setTag(i);
And in onClick:
int position = v.getTag();
..deleteUser(position);
Maybe you have to cast something somewhere..
Remark: You have to set the tag always. So do it just before return view;.
Please do not use an i for position.

Spinner selection not working Android

I have a listView with n number of rows each row contains textview1(for text), textview2(For id) and spinner. I want to select spinner in multiple rows. Here the problem is once i scroll the listView the selection is gone (Recycle listView).
I have store the id in a ArrayList(Which rows spinner selected) and selected position of the spinner. In onScroll method of listView i try to use spinner.setSelection(position); but it won't work.
My sample image
And my source code:
level2.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
for (int i = 0; i < level2.getChildCount(); i++) {
RelativeLayout relative_layout = (RelativeLayout)level2.getChildAt(i);
TextView textview_1 = (TextView)relative_layout.getChildAt(0);
TextView textview_2 = (TextView)relative_layout.getChildAt(1);
Spinner spinner = (Spinner)relative_layout.getChildAt(2);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > arg0,
View arg1, int arg2, long arg3) {
//save my id and spinner selection here in a ArrayList
//Here the position is get value from arraylist
spinner.setSelection(position);
}
public void onNothingSelected(AdapterView < ? > arg0) {
// TODO Auto-generated method stub
}
});
}
}
});
It is not complete code it's just piece. Please help me.
Here if u want to set the selected item then you have to use code spinner.setSelection(arg2);, As you have not initialised your position variable and its default value will be 0 , so it would always show the 1st item selected.
I'll solve like this.Try this. This is just example Activity.
public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List<String> listString;
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList<String>();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}
List<String> spinnerData = new ArrayList<String>();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fillAdapter();
}
private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ViewHolder {
Spinner spinner;
}
class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>();
public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}
public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
}
In activity_main.xml:
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:stackFromBottom="false"
android:transcriptMode="disabled" >
</ListView>
</LinearLayout>
In item_list_spinner_adaoter.xml:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

android listview with custom adapter multiselection issue

I want to make a listview item selected and the text "select" to be made to "selected", but when i click an item mutiple items get selected if I select an item at position 0 , items get selected at at a pattern, that is 0,7,14,21 and if i change the view to landscape: it will be 0 ,5,10,15, etc.
my main activity is:
public class two extends Activity implements OnQueryTextListener,OnItemClickListener {
GroupAdapter grpAdapter;
public static ArrayList<GroupsModel> arrayOfList;
public static ListView listView;
public static String base_url = "myurl";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
arrayOfList = new ArrayList<GroupsModel>();
listView = (ListView) findViewById(R.id.group_listview);
listView.setOnItemClickListener(this);
listView.setTextFilterEnabled(true);
new ProgressTask(two.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
#SuppressWarnings("unused")
private two activity;
public ProgressTask(two two) {
this.activity = two;
context = two;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
grpAdapter = new GroupAdapter(two.this, R.layout.two_row,arrayOfList);
listView.setAdapter(grpAdapter);
}
protected Boolean doInBackground(final String... args) {
//arrayOfList = new ArrayList<GroupsModel>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("",""));
JSONParser jp = new JSONParser();
JSONArray groups_obj = jp.makeHttpRequest(base_url + "groups/all", "GET", params);
for (int i = 0; i < groups_obj.length(); i++) {
GroupsModel group = new GroupsModel();
try {
JSONObject grp = groups_obj.getJSONObject(i);
group.setGroupId(grp.getInt("id"));
group.setGroupname(grp.getString("name"));
arrayOfList.add(group);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText))
{
listView.clearTextFilter();
}
grpAdapter.getFilter().filter(newText.toString());
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.CYAN);
}}
My adapter is:
public class GroupAdapter extends ArrayAdapter<GroupsModel> implements Filterable{
private Context activity;
private ArrayList<GroupsModel> items ;
private List<GroupsModel> arrayList;
private ArrayFilter mFilter;
private int resource;
public GroupAdapter(Activity act, int resource, ArrayList<GroupsModel> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.resource = resource;
this.items = new ArrayList<GroupsModel>();
this.items.addAll(arrayList);
this.arrayList = new ArrayList<GroupsModel>();
this.arrayList.addAll(arrayList);
}
public View getView(final int position, View convertView,final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = ((Activity) activity).getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(resource,parent, false);
holder = new ViewHolder();
holder.group_name = (TextView) convertView.findViewById(R.id.group_name);
holder.select = (TextView) convertView.findViewById(R.id.select);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try{
GroupsModel groups = items.get(position);
holder.group_name.setText(groups.getGroupName());
}catch(Exception e){
e.printStackTrace();
}
holder.select.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
holder.select.setText("my new text");
}
});
return convertView;
}
public class ViewHolder {
public TextView group_name,select;
}
#Override
public int getCount() {
// Total count includes list items and ads.
return items.size();
}
#Override
public GroupsModel getItem(int position)
{
// TODO Auto-generated method stub
return items.get(position);
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
#Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (arrayList == null) {
synchronized (this) {
arrayList = new ArrayList<GroupsModel>(items);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<GroupsModel> list;
synchronized (this) {
list = new ArrayList<GroupsModel>(arrayList);
}
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();
ArrayList<GroupsModel> values;
synchronized (this) {
values = new ArrayList<GroupsModel>(arrayList);
}
final int count = values.size();
final ArrayList<GroupsModel> newValues = new ArrayList<GroupsModel>();
for (int i = 0; i < count; i++) {
final String value = values.get(i).getGroupName();
final String valueText = value.toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(values.get(i));
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(values.get(i));
break;
}
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items = (ArrayList<GroupsModel>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}}
I cant figure this out. Please help
You need to maintain the selected item in adapter and use it to change the text :
Adapter Code
private int selectedIndex;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater inflater = ((Activity) activity).getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(resource,parent, false);
holder = new ViewHolder();
holder.group_name = (TextView) convertView.findViewById(R.id.group_name);
holder.select = (TextView) convertView.findViewById(R.id.select);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(selectedIndex!= -1 && position == selectedIndex)
{
convert_view.setBackgroundColor(Color.CYAN);
holder.select.setText("selected");
}
else
{
convert_vie.wsetBackgroundColor(default_color);
holder.select.setText("Select");
}
//Your other code .....
return convertView ;
}
public void setSelectedIndex(position)
{
selectedIndex = position;
}
Now set the selectedIndex variable when a list item clicked.
public class MainActivity extends Activity implements OnItemClickListener
{
// Implemented onItemClickListener
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
adapter.setSelectedIndex(position);
}
}
You can add a member "checked" in GroupsModel, and initial it assign false;
In activity
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
final boolean isChecked = listView.getItem(position).isChecked();
listView.get(position).setChecked(!isChecked);
}
In getView() in adapter:
public View getView(...) {
...
if(getItem(position).isChecked()) {
// You must set root view in holder
holder.getBackground().setBackgroundColor(Color.CYAN);
}
...
}
Your problem is that Android is reusing your views, so when you scroll, the first view disappears and appears at the bottom with the same state.
What you need to do is everytime you check an item, you need to store de id/position of the item checked (maybe ArrayList<Integer>), this way everytime your getView method is getting called you will look at this class/structure you have created and see it the row needs to be checked or not.
Note: If the row is not checked you will have to call to myCheck->setChecked(false); in order to assure that the row is in a coherent state.
You must use a array or option object to record which position is selected.
And detect the array or option object in getView() in adapter.
So, your need move the code: "view.setBackgroundColor(Color.CYAN)" to getView() method.
You have been bitten by what they say "recycling" issues.
When re-using your views, this kind of problems happens.
There are several methods(for example saving checked positions in an arraylist, ...) to deal with it, but in my opinion the simplest and straight forward solution is using tags.
setTag() and getTag()
Here is a tutorial using it.
Hope it helps.

Spinners selection changes on adding new items or scrolling listview android

I have added two spinners on custom adapter list view. All is good but when I add new item to list view then value of previous item's spinner is transformed to new item. And on scrolling list view values of spinners also rotates. Any Help please.
public class CustomAdapter extends BaseAdapter {
private ArrayList<MyMessageDetails> _data;
Context _c;
ProductsItemViewHolder holder;
CustomAdapter (ArrayList<MyMessageDetails> data, Context c){
_data = data;
_c = c;
}
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
final MyMessageDetails msg = _data.get(position);
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listview_item_row, null);
holder = new ProductsItemViewHolder();
holder.image = (ImageView) v.findViewById(R.id.icon);
holder.fromView = (TextView) v.findViewById(R.id.textView1);
holder.spinnersizes = (Spinner) v.findViewById(R.id.spinner1);
holder.spinnercopies = (Spinner) v.findViewById(R.id.spinner2);
String Photo_copies[];
Photo_copies=new String[100];
int x = 1;
while( x < 101 ) {
if(x == 1){
Photo_copies[x-1]= String.valueOf(x) + " Copy";
}else{
Photo_copies[x-1]= String.valueOf(x) + " Copies";
}
x++;
}
String array_spinner[];
array_spinner=new String[5];
array_spinner[0]="4x6|Plastic|RS 20";
array_spinner[1]="option 2";
array_spinner[2]="option 3";
array_spinner[3]="option 4";
array_spinner[4]="option 5";
ArrayAdapter adapter = new ArrayAdapter(_c,android.R.layout.simple_spinner_item, array_spinner);
ArrayAdapter adapter2 = new ArrayAdapter(_c,android.R.layout.simple_spinner_item, Photo_copies);
holder.spinnersizes.setAdapter(adapter);
holder.spinnercopies.setAdapter(adapter2);
holder.spinnercopies.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
String sizes = holder.spinnersizes.getSelectedItem().toString();
holder.spinnersizes.setTag(position);
String copies = holder.spinnercopies.getSelectedItem().toString();
String mycopies = copies;
String myprice = sizes;
myprice = myprice.substring(myprice.lastIndexOf(" ") + 1);
mycopies = mycopies.substring(0, mycopies.lastIndexOf(" "));
int finalprice = Integer.parseInt(myprice) * Integer.parseInt(mycopies);
holder.fromView.setText(holder.image.getTag().toString() + " Copies:" + mycopies + " Price:" + finalprice);
msg.setCopies(mycopies);
msg.setSize(String.valueOf(finalprice));
// lab_gallery.Calculate_Bill();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
holder.spinnersizes.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
// your code here
String sizes = holder.spinnersizes.getSelectedItem().toString();
holder.spinnersizes.setTag(position);
String copies = holder.spinnercopies.getSelectedItem().toString();
String mycopies = copies;
String myprice = sizes;
myprice = myprice.substring(myprice.lastIndexOf(" ") + 1);
mycopies = mycopies.substring(0, mycopies.lastIndexOf(" "));
int finalprice = Integer.parseInt(myprice) * Integer.parseInt(mycopies);
holder.fromView.setText(holder.image.getTag().toString() + " Copies:" + mycopies + " Price:" + finalprice);
msg.setCopies(mycopies);
msg.setSize(String.valueOf(finalprice));
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
holder.image.setImageBitmap(msg.getIcon());
holder.fromView.setTag(msg.getUrl());
holder.fromView.setText(msg.getName());
holder.image.setTag(msg.getName());
msg.setCopies("1");
msg.setSize("20");
v.setTag(holder);
} else {
holder = (ProductsItemViewHolder) v.getTag();
}
if (holder.spinnersizes.getTag() != null){
holder.spinnersizes.setSelection(Integer.parseInt(holder.spinnersizes.getTag().toString()));
}
//image.setScaleType(ScaleType.FIT_XY);
return v;
}
static class ProductsItemViewHolder {
ImageView image;
TextView fromView;
Spinner spinnersizes;
Spinner spinnercopies;
}
}
This is because of view recycling. View are recycled with the last state. If your spinner had something selected, and if that view is reused it'll also pick the state of the spinner. You need to add logic to preserve state of your Spinners. You can maintain a dictionary with row position and selectedIndex.
//Class level field
Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
inside your getView() method check if we have a state saved for the spinner at this row psition
if (myMap.containsKey(position)) {
spinner.setSelection(myMap.get(position));
}
Saving state when an item is selected in spinner
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int selectedIndex, long id) {
//...
myMap.put(position, selectedIndex);
//...
}

Spinner in listview

I have a listview (multi choice).
It works perfect. I used a custom adapter for it. There is a text in each row and a checkbox.
Marked items will be shown as a result in a textview.
I want to add a spinner inside listview rows. I get null point exception error.
Thanks in advance.
public class MainActivity extends Activity {
ListView myListView;
Button getResult;
Spinner spinner;
private ArrayList<String> dayOfWeekList = new ArrayList<String>();
private void initDayOfWeekList(){
dayOfWeekList.add("Sunday");
dayOfWeekList.add("Monday");
dayOfWeekList.add("Tuesday");
dayOfWeekList.add("Wednesday");
dayOfWeekList.add("Thursday");
dayOfWeekList.add("Friday");
dayOfWeekList.add("Saturday");
}
String[] androidBooks =
{
"Hello",
"Professional",
"Unlocking"
};
MyArrayAdapter myArrayAdapter;
ArrayAdapter<String> spinnerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDayOfWeekList();
setContentView(R.layout.activity_main);
spinnerAdapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item, androidBooks);
myListView = (ListView)findViewById(R.id.list);
myArrayAdapter = new MyArrayAdapter(
this,
R.layout.row,
android.R.id.text1,
dayOfWeekList
);
myListView.setAdapter(myArrayAdapter);
myListView.setOnItemClickListener(myOnItemClickListener);
getResult = (Button)findViewById(R.id.getresult);
getResult.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String result = "";
/*
//getCheckedItemPositions
List<Integer> resultList = myArrayAdapter.getCheckedItemPositions();
for(int i = 0; i < resultList.size(); i++){
result += String.valueOf(resultList.get(i)) + " ";
}
*/
//getCheckedItems
List<String> resultList = myArrayAdapter.getCheckedItems();
for(int i = 0; i < resultList.size(); i++){
result += String.valueOf(resultList.get(i)) + "\n";
}
myArrayAdapter.getCheckedItemPositions().toString();
Toast.makeText(
getApplicationContext(),
result,
Toast.LENGTH_LONG).show();
}});
}
OnItemClickListener myOnItemClickListener
= new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
myArrayAdapter.toggleChecked(position);
}};
private class MyArrayAdapter extends ArrayAdapter<String>{
private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();
public MyArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
for(int i = 0; i < objects.size(); i++){
myChecked.put(i, false);
}
}
public void toggleChecked(int position){
if(myChecked.get(position)){
myChecked.put(position, false);
}else{
myChecked.put(position, true);
}
notifyDataSetChanged();
}
public List<Integer> getCheckedItemPositions(){
List<Integer> checkedItemPositions = new ArrayList<Integer>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItemPositions).add(i);
}
}
return checkedItemPositions;
}
public List<String> getCheckedItems(){
List<String> checkedItems = new ArrayList<String>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItems).add(dayOfWeekList.get(i));
}
}
return checkedItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
spinner = (Spinner) row.findViewById(R.id.planets_spinner);
spinner.setAdapter(spinnerAdapter);
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
checkedTextView.setText(dayOfWeekList.get(position));
Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}
return row;
}
}
}
You have your code out of order in getView(..)
You are trying to retrieve your spinner before you inflate your view.. If convertView is null (which it will be the first time through your list) your spinner will not be found.. you can't call findViewById on something that's null.. Change the order of your operations to the following
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
spinner = (Spinner) row.findViewById(R.id.planets_spinner);
spinner.setAdapter(spinnerAdapter);
CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
checkedTextView.setText(dayOfWeekList.get(position));
Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}
return row;
}
Notice how i moved retrieving the spinner to AFTER checking the view for null and inflating if needed

Categories

Resources