1

hi i have popup button. When I click pop up button, it displays radio button.For every radio button selection, I am fetching datas and updating in UI. Problem is for first radio button it is updating UI. But for second radio button and so on, UI is not updating. How to solve this?

Code is as follows:

public class DataList extends Activity {

    private Button popUpClick;
    private Dialog sortFilterDialog;
    private RadioGroup radioGroup;
    private RadioButton ascToDesradioButton, desToAscradioButton,
            highToLowradioButton, lowToHighradioButton, popularityradioButton;
    private int popupselectionItem;
    private ImageView closeButton;
    private String sessionId;
    private String sortName,sortOrder;
    ArrayList<SortFilterProducts> personsList;
    private ListView list;
    private DataListAdapter gridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.sortfilterclick);
         personsList= new ArrayList<SortFilterProducts>();
        popUpClick = (Button) findViewById(R.id.popupButton);
        list=(ListView)findViewById(R.id.sortFilterList);
        gridAdapter = new DataListAdapter(this, R.layout.sort_filter_listrow, personsList);
        list.setAdapter(gridAdapter);
        popUpClick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                sortFilterDialog = new Dialog(SortFilterPopupActivity.this);
                sortFilterDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                sortFilterDialog.setContentView(R.layout.sortfilterrow);

                radioGroup = (RadioGroup) sortFilterDialog
                        .findViewById(R.id.radioGroup);
                ascToDesradioButton = (RadioButton) sortFilterDialog
                        .findViewById(R.id.asc_to_des);
                desToAscradioButton = (RadioButton) sortFilterDialog
                        .findViewById(R.id.des_to_asc);
                highToLowradioButton = (RadioButton) sortFilterDialog
                        .findViewById(R.id.high_to_low);
                lowToHighradioButton = (RadioButton) sortFilterDialog
                        .findViewById(R.id.low_to_high);
                popularityradioButton = (RadioButton) sortFilterDialog
                        .findViewById(R.id.popularity);
                ascToDesradioButton
                        .setOnClickListener(radioButtonOnClickListener);
                desToAscradioButton
                        .setOnClickListener(radioButtonOnClickListener);
                highToLowradioButton
                        .setOnClickListener(radioButtonOnClickListener);
                lowToHighradioButton
                        .setOnClickListener(radioButtonOnClickListener);
                popularityradioButton
                        .setOnClickListener(radioButtonOnClickListener);
                }


            private final OnClickListener radioButtonOnClickListener = new OnClickListener() {

                @Override
                public void onClick(View v) {

                    switch (popupselectionItem = v.getId()) {
                    case R.id.asc_to_des:
                        sortName="atoz";
                        sortOrder="SORT_ASC";
                        new SortFilterElement().execute();
                        //new AscToDesElements().execute();
                        break;
                    case R.id.des_to_asc:
                        sortName="atoz";
                        sortOrder="SORT_DESC";
                        new SortFilterElement().execute();
                        //new DescToAscElements().execute();
                        break;
                    case R.id.high_to_low:
                        sortName="lowtohigh";
                        sortOrder="SORT_ASC";
                        new SortFilterElement().execute();
                        //new PriceHightoLow().execute();
                        break;
                    case R.id.low_to_high:
                        sortName="lowtohigh";
                        sortOrder="SORT_DESC";
                        //new PriceLowtoHigh().execute();
                        new SortFilterElement().execute();
                        break;
                    case R.id.popularity:
                        sortName="popularity";
                        sortOrder="SORT_ASC";
                        //new Popularity().execute();
                        new SortFilterElement().execute();
                        break;
                    default:
                    }
                    sortFilterDialog.dismiss();

                }
                class SortFilterElement extends AsyncTask<String,String,String>{
                    ProgressDialog dialog;
                    @Override
                      protected void onPreExecute() {
                       // TODO Auto-generated method stub
                       super.onPreExecute();
                       dialog=new ProgressDialog(SortFilterPopupActivity.this);
                       dialog.setMessage("Loading, please wait");
                       dialog.setTitle("Connecting server");
                       dialog.show();
                       dialog.setCancelable(false);


                      }
                    @Override
                    protected String doInBackground(String... args) {
                        try {
                            SoapSerializationEnvelope env = new SoapSerializationEnvelope(
                                    SoapSerializationEnvelope.VER11);
                            env.dotNet = false;
                            env.xsd = SoapSerializationEnvelope.XSD;
                            env.enc = SoapSerializationEnvelope.ENC;
                            if (sessionId == null) {
                            JSONObject json = new JSONObject();
                            json.put("page", "1");
                            json.put("limit", "10");
                            json.put("sort_name", sortName);
                            json.put("sort_order", sortOrder);
                                    String params = json.toString();
                            requests.addProperty("args", params);
                            env.setOutputSoapObject(requests);
                            androidHttpTransport.call("", env);
                            Object results = env.getResponse();
                            Log.e("Sort results", results.toString());
                            if (results.toString() != null) {
                                JSONObject jsono = new JSONObject(results
                                        .toString());
                                JSONArray jarray = jsono
                                        .getJSONArray("result");
                                for (int i = 0; i < jarray.length(); i++) {
                                    JSONObject object = jarray.getJSONObject(i);
                                    SortFilterProducts products = new SortFilterProducts();
                                    String id = object.getString("id");


                                    int productPrice = object.getInt("price");
                                    String imageUrl = object
                                            .getString("image_url");

                                    int ratings=object.getInt("ratings");
                                    products.setProductName(productName);
                                    products.setImageUrl(imageUrl);
                                    personsList.add(products);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(String result) {
                        // TODO Auto-generated method stub
                        super.onPostExecute(result);
                          dialog.cancel();
                           gridAdapter.notifyDataSetChanged();
                    }
                }
            };
        });

    }

    }

and My DataListAdapter:

public class DataListAdapter extends BaseAdapter {

    LayoutInflater layoutInflater;
    int Resource;
    ViewHolder viewHolder;
    Activity activity;
    public ImageLoader loader;
    private final ArrayList<SortFilterProducts> itemLists;

    public DataListAdapter(Activity a, int resource,
            ArrayList<SortFilterProducts> itemList) {
        layoutInflater = (LayoutInflater) a
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        itemLists = itemList;
        activity = a;
        loader = new ImageLoader(a.getApplicationContext());

    }

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


      @Override
     public Object getItem(int position) {
      return itemLists.get(position);
     }
     @Override
     public long getItemId(int position) {
      return position;

     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
        View v= convertView;
        try {
             if(v==null){
                 viewHolder = new ViewHolder();
                  v = layoutInflater.inflate(Resource, null);
                  viewHolder.productName=(TextView)v.findViewById(R.id.productName);
                  viewHolder.productPrice=(TextView)v.findViewById(R.id.price);
                  v.setTag(viewHolder);  

                 }
                 else{
                  viewHolder = (ViewHolder) v.getTag();

                 }
             final String productName=itemLists.get(position).getProductName();

             final int productPrice=itemLists.get(position).getProductPrice();
             viewHolder.productName.setText(productName);
             viewHolder.productPrice.setText(Integer.toString(productPrice));    

             }
        catch (Exception ex) {
              ex.printStackTrace();
             }
        return v;
    }
    static class ViewHolder {
         public TextView productName,productPrice;
    }
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Shadow
  • 6,864
  • 6
  • 44
  • 93
  • There is a previous [post](http://stackoverflow.com/a/2250849/1129468) related with your question. Have you ever tried `notifyDataSetChanged()` on your `DataListAdapter` ? – gokhanakkurt Dec 16 '14 at 12:44
  • I think any of these link will help you.[Link1][1] , [Link2][2] and [Link3][3] [1]: http://stackoverflow.com/questions/21605516/notifydatasetchanged-not-updating-listview [2]: http://stackoverflow.com/questions/9747553/listview-not-updating-with-notifydatasetchanged-call [3]: http://stackoverflow.com/questions/24453692/android-listview-not-refreshing-after-notifydatasetchanged-with-data-as-map – DJhon Dec 16 '14 at 12:47
  • @gokhanakkurt i updated DataListAdapter. will you please tell where i need to add? – Shadow Dec 16 '14 at 13:01

2 Answers2

1

Invalidate the items of the ListView while expanding the ArrayList<SortFilterProducts> passed to adapter.

list.invalidateViews();

Good Luck. :)

Hemendra Sharma
  • 1,063
  • 9
  • 21
1

As I mention in comments, you should call notifyDataSetChanged() method after adding new objects to your list.

First of all, add following method to your DataListAdapter

public void addItem(SortFilterProducts product) {
  itemLists.add(product);
  notifyDataSetChanged();
}

Then, remove the line personsList.add(products); on your onCreate() method and add the following line.

DataListAdapter adapter = (DataListAdapter)list.getAdapter();
adapter.addItem(products);

Your ListView automatically updated whenever new item is added.

Hope this may help.

gokhanakkurt
  • 4,935
  • 4
  • 28
  • 39