0

I am using json parsing, on button's click I have one alert dialog with listview and Radio Button . List is dynamically generated. by default first radio button of list is selected, Now issue is if I select second item and close the dialog , and again when I open it shows first radio button selected instead of second . Following is my code can any one help?

Adapter

 public class CustomListAdapterSize extends BaseAdapter {
        private Context context;

        private LayoutInflater inflater;
        private List<ProdSizes> movieItems;
        private List<ProductPackModel> packItems;
        private RadioButton selected =null;
        private int mSelectedPosition = 0;
        private RadioButton mSelectedRB;
        public CustomListAdapterSize(Context context,List<ProdSizes> movieItems) {
            this.context = context;
            this.movieItems=movieItems;

        }

        @Override
        public int getCount() {
            return movieItems.size();
        }

        @Override
        public Object getItem(int position) {
            return movieItems.get(position);
        }

        @Override
        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(com.vetsupply.au.project.R.layout.listitem_productsizes, null);
                holder.txtproname = (TextView) convertView.findViewById(com.vetsupply.au.project.R.id.txtproductsizenames);
                holder.txtproprice = (TextView) convertView.findViewById(com.vetsupply.au.project.R.id.txtproductsizeprice);
                holder.radioBtn = (RadioButton)convertView.findViewById(com.vetsupply.au.project.R.id.productsize_radio);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            // getting movie data for the row
            final ProdSizes m = movieItems.get(position);
            // title
            holder.txtproname.setText(m.getProViews_Size());
            holder.txtproprice.setText("$"+m.getProViews_MinPrice());

            if (position == getCount() - 1) {
                if (selected == null) {
                    holder.radioBtn.setChecked(true);
                    selected = holder.radioBtn;
                    /*for(int i=0;i<movieItems.size();i++)
                    {
                        sizenms =  movieItems.get(i).getProViews_Size();
                    }
*/

                }
            }

            holder.radioBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (selected != null) {
                        selected.setChecked(false);
                    }
                    holder.radioBtn.setChecked(true);
                    selected = holder.radioBtn;
                    ids = m.getProViews_ID();
                    sizenms = m.getProViews_Size();
                    System.out.println(">>>>>>>>>>>>>" + ids);
                    System.out.println("<<<<<<<<<<<" + sizenms);
                    dialogf.dismiss();
                }
            });



            return convertView;
        }
        class ViewHolder{

            TextView txtproname,txtproprice;

            public RadioButton radioBtn;
        }

    }
chris
  • 699
  • 4
  • 12
  • 35

3 Answers3

1

Try in this way....

int mSelectedPosition = -1;

public class CustomListAdapterSize extends BaseAdapter{


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            // Your Code

            holder.checks.setChecked(position==mSelectedPosition );

            holder.checks.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked)
                    {
                        mSelectedPosition =  position;
                    }
                    else{
                         mSelectedPosition = -1;
                    }
                    notifyDataSetChanged();
                }
            });
            return convertView;


        }
}
Mohd Saquib
  • 580
  • 4
  • 14
  • i need one help – chris Aug 08 '17 at 11:20
  • two buttons, on first button i show sizes in custom popup window on second button i show packs in another popup window now by default first size is selected but if i click on second button it shows packs of last size insted of selected – chris Aug 08 '17 at 11:29
  • if you will declare int mSelectedPosition = -1; then by default none will be selected bro because adapter position start from 0. – Mohd Saquib Aug 08 '17 at 11:34
  • yes second button will get selected , but what actually you want to do which size ?please explain – Mohd Saquib Aug 08 '17 at 11:37
  • 1)first size from list must be selected and its pack must be shown to second button's click 2)on user's selection change and size and its relative packs – chris Aug 08 '17 at 11:40
  • means you have to show first button selected default and fetch value from arraylist of first pos but when u click on second then you will have to fetch from 2nd pos of list , am i clear? – Mohd Saquib Aug 08 '17 at 11:43
  • https://stackoverflow.com/questions/45582427/how-to-load-child-json-array-and-display-it – chris Aug 09 '17 at 05:39
0

Please try this. I think you are not storing position into mSelectedPosition

 private int mSelectedPosition = 0;//declare it publically not locally into your adapter
             if (position == mSelectedPosition ) {
                            if (selected == null) {
                                holder.radioBtn.setChecked(true);
                                selected = holder.radioBtn;
                                /*for(int i=0;i<movieItems.size();i++)
                                {
                                    sizenms =  movieItems.get(i).getProViews_Size();
                                }
            */

                            }
                        }

                        holder.radioBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                if (selected != null) {
                                    selected.setChecked(false);
                                }
                          mSelectedPosition =position;
                                holder.radioBtn.setChecked(true);
                                selected = holder.radioBtn;
                                ids = m.getProViews_ID();
                                sizenms = m.getProViews_Size();
                                System.out.println(">>>>>>>>>>>>>" + ids);
                                System.out.println("<<<<<<<<<<<" + sizenms);
                                dialogf.dismiss();
                            }
                        });
SripadRaj
  • 1,687
  • 2
  • 22
  • 33
Mohd Saquib
  • 580
  • 4
  • 14
  • Use Check box instead of Radio button. This is problem because of radio button.And before dismissed your dialog once check to notify your adapter. – Mohd Saquib Aug 01 '17 at 05:43
  • I need only one selection at a time, and checkbox is for multiple selection . will it work? – chris Aug 01 '17 at 05:43
  • Yes it will work, "mSelectedPosition" this varible will work you for single selection you will have to notify adapter before dismiss the dialog. – Mohd Saquib Aug 01 '17 at 05:45
  • I tried with checkbox ,but the issue is that If i select second then first should be uncheck, but its not working like that way. – chris Aug 01 '17 at 06:00
  • If you select second then first should be uncheck this condition you will have to implement and notify your adapter just before dialogf.dismiss(). if (position == mSelectedPosition ) holder.checkBox.setChecked(true); else holder.checkBox.setChecked(false); – Mohd Saquib Aug 01 '17 at 06:14
  • did you check pastie?? – chris Aug 01 '17 at 06:16
  • Yes bro you have not notify your adapter in onCheckedChanged().Please notify at the end. – Mohd Saquib Aug 01 '17 at 06:17
0

Please use Choice Mode and store the position either locally[Shared Preferences] or use to API to store

public class MainActivity extends AppCompatActivity {

    ListView mListView;
    ArrayList<String> mArrayList;
    ArrayAdapter mArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.listView_id);

        mArrayList = new ArrayList();
        mArrayList.add("ONE");
        mArrayList.add("TWO");
        mArrayList.add("THREE");
        mArrayAdapter = new ArrayAdapter(getApplicationContext(),
                android.R.layout.simple_list_item_single_choice,
                mArrayList);
        mListView.setAdapter(mArrayAdapter);
        /***Save your position **/
        int position = 1;//Save your position in Shared preferences or get it from API
        mListView.setItemChecked(position, true);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckedTextView textView = (CheckedTextView) view;
                for (int i = 0; i < mListView.getCount(); i++) {
                    if (textView != null) {
                        textView.setTextColor(Color.GREEN);
                    }
                }
                mListView.invalidate();
                textView = (CheckedTextView) view;
                if (textView != null) {
                    textView.setTextColor(Color.BLUE);
                }
            }
        });
    }
}

Please let me know if it helps.

Jeelan
  • 173
  • 1
  • 11