如何在android中显示字母索引对应的listview

时间:2015-03-27 11:09:00

标签: android listview

public class MainActivity extends Activity implements OnClickListener {
    String result;

    Point p;
    Map<String, Integer> mapIndex;

    String URL = "http://www.taxmann.com/TaxmannWhatsnewService/mobileservice.aspx?service=topstories";
    ArrayList<DataModel> list = new ArrayList<DataModel>();
    JSONArray _jarray1;

    String ImagePath;
    ListView fruitList;

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

        Button btn_show = (Button) findViewById(R.id.show_popup);

        btn_show.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                // Open popup window
                if (p != null)
                    // new download().execute();

                    showPopup(MainActivity.this, p);

            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        int[] location = new int[2];
        Button button = (Button) findViewById(R.id.show_popup);
        button.getLocationOnScreen(location);
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    }

    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {

        LinearLayout viewGroup = (LinearLayout) context
                .findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup, viewGroup);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(LayoutParams.MATCH_PARENT);
        popup.setHeight(LayoutParams.MATCH_PARENT);
        popup.setFocusable(true);

        int OFFSET_X = 30;
        int OFFSET_Y = 30;
        fruitList = (ListView) layout.findViewById(R.id.list_fruits);
        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
                + OFFSET_Y);

        new download().execute();
        List<DataModel> arrlist = new ArrayList<DataModel>();

        String[] stringArray = arrlist.toArray(new String[list.size()]);

        getIndexList(stringArray);

        LinearLayout indexLayout = (LinearLayout) layout
                .findViewById(R.id.side_index);

        TextView textView;
        List<String> indexList = new ArrayList<String>(mapIndex.keySet());
        for (String index : indexList) {
            textView = (TextView) getLayoutInflater().inflate(
                    R.layout.side_index_item, null);
            textView.setText(index);
            textView.setOnClickListener(this);
            indexLayout.addView(textView);
        }
    }

    private void getIndexList(String[] fruits) {
        mapIndex = new LinkedHashMap<String, Integer>();
        for (int i = 0; i < fruits.length; i++) {

            String fruit = fruits[i];
            String index = fruit.substring(0, 1);

            if (mapIndex.get(index) == null)
                mapIndex.put(index, i);
        }
    }

    public void onClick(View view) {
        TextView selectedIndex = (TextView) view;
        fruitList.setSelection(mapIndex.get(selectedIndex.getText()));
    }

    class download extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            try {
                result = JSONfunctions.getJSONfromURL(URL);
                _jarray1 = new JSONArray(result);

                for (int i = 0; i < _jarray1.length(); i++) {
                    DataModel datamodel = new DataModel();
                    JSONObject _obj = _jarray1.getJSONObject(i);
                    ImagePath = _obj.getString("news_title");
                    if (ImagePath != null) {
                        datamodel.setImagepath(_obj.getString("news_title"));

                        Log.e("Valueeeeeeeeeeeeeeeeeee",
                                "IMAGE PATHAAAAAAAAAAAA: "
                                        + _obj.getString("news_title"));

                    }
                    list.add(datamodel);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            CustomList adapter = new CustomList(MainActivity.this, list);
            fruitList.setAdapter(adapter);

            super.onPostExecute(result);
        }
    }

}

这是我的代码我试图按字母顺序显示索引对应列表视图对应列表视图,就像我点击索引t然后列表滚动查看t数据来的基本上我想按字母顺序搜索列表我将显示Listview在弹出窗口,但我无法显示我们搜索的索引请建议我在哪里做错我得到空白数据当我转换列表到DataModel的字符串数组请建议我在哪里做错了告诉我解决方案

1 个答案:

答案 0 :(得分:0)

希望这些对您有帮助!!!!

    @Override
    protected void onPostExecute(Void result) {
        Collections.sort(list);
        CustomList adapter = new CustomList(MainActivity.this, list);
        fruitList.setAdapter(adapter);
         super.onPostExecute(result);
    }

快乐的编码……