如何以升序显示列表视图。

时间:2017-03-08 12:11:48

标签: android sorting listview android-adapter

ListView listview , reailerlist;

List<BeanShowHierarchy> showHierarchies = new ArrayList<BeanShowHierarchy>();
List<BeanReatailerHierarchy> reatailerHierarchies = new ArrayList<BeanReatailerHierarchy>();
DistributorListAdapter distributorListAdapter;

RatailerListAdapter retailerListAdapter;

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

    pref = new AppPref(getApplicationContext());

    listview = (ListView) findViewById(R.id.listview);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    // Sets the Toolbar to act as the ActionBar for this Activity window.
    // Make sure the toolbar exists in the activity and is not null
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });

    new get_dis_ret_name().execute();
}

public class get_dis_ret_name extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            loadingView = new ProgressActivity(Distributor_Retailer_User_Profile.this, "");

            loadingView.setCancelable(false);
            loadingView.show();

        } catch (Exception e) {

        }
    }

    @Override
    protected String doInBackground(Void... params) {
        try {

            List<NameValuePair> parameters = new ArrayList<>();
            if (pref.getRole_id().equalsIgnoreCase(C.COMP_SALES_ROLE)) {

                parameters.add(new BasicNameValuePair("sales_per_id", pref.getUser_id()));
                parameters.add(new BasicNameValuePair("role_id", pref.getRole_id()));

                Log.e("Parameters", "" + parameters);

            } else {

                parameters.add(new BasicNameValuePair("sales_per_id", pref.getUser_id()));
                parameters.add(new BasicNameValuePair("role_id", pref.getRole_id()));
                parameters.add(new BasicNameValuePair("owner_id", pref.getOwner_id()));

                Log.e("Parameters", "" + parameters);
            }

            String json = new ServiceHandler().makeServiceCall(Web.LINK + Web.HIERARCHY, ServiceHandler.POST, parameters);

            System.out.println("List" + json);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error: " + e.toString());

        }
        return json;
    }

    @Override
    protected void onPostExecute(String result_1) {
        super.onPostExecute(result_1);
        loadingView.dismiss();

        try {
            if (result_1.equalsIgnoreCase("")) {
                Toast.makeText(Distributor_Retailer_User_Profile.this, "SERVER ERROR", Toast.LENGTH_LONG).show();
                loadingView.dismiss();

            } else {

                JSONObject main = new JSONObject(result_1);

                if (!main.getBoolean("status")) {

                    String message = main.getString("message");
                    Toast.makeText(Distributor_Retailer_User_Profile.this, "" + message, Toast.LENGTH_SHORT).show();

                    Log.e("messsage", "" + message);
                } else {

                    String message = main.getString("message");
                    Toast.makeText(Distributor_Retailer_User_Profile.this, "" + message, Toast.LENGTH_SHORT).show();

                    Log.e("messsage", "" + message);
                    Log.e("Data",main.toString());

                    JSONArray dataArray = main.getJSONArray("data");

                    for (int i = 0; i < dataArray.length(); i++) {

                        JSONObject arrayObject = dataArray.getJSONObject(i);
                       JSONArray retailers = arrayObject.getJSONArray("Retailers");

                        BeanShowHierarchy showHierarchy = new BeanShowHierarchy();
                        showHierarchy.setFirm_shop_name(arrayObject.getString("firm_shop_name"));


                        List<BeanReatailerHierarchy> reatailerHierarchies=new ArrayList<>();

                        Log.e("name" , arrayObject.getString("firm_shop_name"));
                       for (int j = 0; j < retailers.length(); j++){

                            JSONObject retailermainObject = retailers.getJSONObject(j);

                            JSONObject userObject = retailermainObject.getJSONObject("User");
                            JSONObject distributorObject = retailermainObject.getJSONObject("Distributor");

                            BeanReatailerHierarchy reatailerHierarchy = new BeanReatailerHierarchy();
                            reatailerHierarchy.setId(userObject.getString("id"));
                            reatailerHierarchy.setFirm_shop_name(distributorObject.getString("firm_shop_name"));

                           reatailerHierarchies.add(reatailerHierarchy);

                          /* reatailerHierarchies.add(reatailerHierarchy);*/
                        }
                        showHierarchy.setReatailerHierarchies(reatailerHierarchies);

                       retailerListAdapter = new RatailerListAdapter(reatailerHierarchies, getApplicationContext(), Distributor_Retailer_User_Profile.this);
                        retailerListAdapter.notifyDataSetChanged();
                        reailerlist.setAdapter(retailerListAdapter);

                        showHierarchies.add(showHierarchy);
                    }

                    Log.e("333330" , "djkjjkjdk");

                    distributorListAdapter = new DistributorListAdapter(showHierarchies, getApplicationContext(), Distributor_Retailer_User_Profile.this);
                    distributorListAdapter.notifyDataSetChanged();

                    listview.setAdapter(distributorListAdapter);
                    Log.e("333330" , "djkjjkjdk");
                }
            }
        } catch (JSONException j) {
            j.printStackTrace();
            Log.e("excePtioN", j.getMessage());
        }
    }
}
}

实际上我用两个列表视图制作了一个名单。这就是我在我的活动中显示列表视图的方式。我想按升序对列表视图进行排序。我尝试使用collections.sort()但它什么也没反映出来。 给我一些建议。

1 个答案:

答案 0 :(得分:0)

只需在API文件中更改查询即可。与ORDER BY field_name

类似