Android ListView with fast scroll and alphabetical section index
Asked Answered
C

3

13

How to add testview when touching a letter on right alphabet panel as shown in images? Could you please help me? Below is my code.

In details, I am looking for an example exactly like below image. Currently I did the sorting of data. When I click on right alphabet panel it shows the data properly. But the problem is when touching a letter the right alphabet panel I need to show what letter he is pressing in large size shown in image(E). How can I do it, could you please help me? Thank you in advance!

screenshot of desired UI

// MainActivity.java
public class MainActivity extends Activity implements
                          SearchView.OnQueryTextListener,
                          SearchView.OnCloseListener {

    private IndexableListView listView;
    private SearchView search;
    EfficientAdapter objectAdapter;
    EfficientAdapter2 objectAdapter1;
    int textlength = 0;
    private CheckBox checkStat, checkRoutine, checkTat;
    private ArrayList<Patient> patientListArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homempleb);
        Log.i("scan"," txtScanResult ");

        ActionItem nextItem = new ActionItem();
        final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);
        quickAction.addActionItem(nextItem);
        quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
            @Override
            public void onDismiss() {
                Toast.makeText(getApplicationContext(), "Dismissed", Toast.LENGTH_SHORT).show();
            }
        });

        listView = (IndexableListView) findViewById(R.id.homelistView);
        listView.setTextFilterEnabled(true);
        listView.setFastScrollEnabled(true);
        listView.setFastScrollAlwaysVisible(true);
        objectAdapter = new EfficientAdapter(this);
        listView.setAdapter(objectAdapter);
    }

    @Override
    public boolean onClose() {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
}

...

// EfficientAdapter.java
public class EfficientAdapter extends BaseAdapter implements SectionIndexer {

    private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ArrayList<Patient> patientListArray;
    private LayoutInflater mInflater;
    private Context context;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context=context;
        String patientListJson = CountriesList.jsonData;
        JSONObject jssson;
        try {
            jssson = new JSONObject(patientListJson);
            patientListJson = jssson.getString("PostPatientDetailResult");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray Jarray = parser.parse(patientListJson).getAsJsonArray();
        patientListArray = new ArrayList<Patient>();
        for (JsonElement obj : Jarray) {
            Patient patientList = gson.fromJson(obj, Patient.class);
            patientListArray.add(patientList);
            Collections.sort(patientListArray, new Comparator<Object>() {
                @Override
                public int compare(Object o1, Object o2) {
                     Patient p1 = (Patient) o1;
                     Patient p2 = (Patient) o2;
                    return p1.getName().compareToIgnoreCase(p2.getName());
                }
            });
        }
    }


    public int getCount() {
        return patientListArray.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.homemplebrowview, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.name);
            holder.text2 = (TextView) convertView.findViewById(R.id.mrn);
            holder.text3 = (TextView) convertView.findViewById(R.id.date);
            holder.text4 = (TextView) convertView.findViewById(R.id.age);
            holder.text5 = (TextView) convertView.findViewById(R.id.gender);
            holder.text6 = (TextView) convertView.findViewById(R.id.wardno);
            holder.text7 = (TextView) convertView.findViewById(R.id.roomno);
            holder.text8 = (TextView) convertView.findViewById(R.id.bedno);
            holder.btnList = (Button) convertView.findViewById(R.id.listbutton);
            holder.btnList.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Intent next=new Intent(context, SeviceDetails.class);
                    // context.startActivity(next);
                }
            });
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text1.setText(Util.formatN2H(patientListArray.get(position).getName()));
        holder.text2.setText(patientListArray.get(position).getMrnNumber());
        holder.text3.setText(Util.formatN2H(patientListArray.get(position).getRoom()));
        holder.text4.setText(Util.formatN2H(patientListArray.get(position).getAge()));
        holder.text5.setText(Util.formatN2H( patientListArray.get(position).getGender()));
        holder.text6.setText(Util.formatN2H(patientListArray.get(position).getWard()));
        holder.text7.setText(Util.formatN2H(patientListArray.get(position).getRoom()));
        holder.text8.setText(Util.formatN2H(patientListArray.get(position).getBed()));
        return convertView;
    }

    static class ViewHolder {
        public Button btnList;
        public TextView text8;
        public TextView text7;
        public TextView text6;
        public TextView text5;
        public TextView text4;
        public TextView text1;
        public TextView text2;
        public TextView text3;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    //@Override
    public int getPositionForSection(int section) {
        // If there is no item for current section, previous section will be selected
        for (int i = section; i >= 0; i--) {
            for (int j = 0; j < getCount(); j++) {
                if (i == 0) {
                    // For numeric section
                    for (int k = 0; k <= 9; k++) {
                        if (StringMatcher.match(String.valueOf(patientListArray.get(j).getName().charAt(0)), String.valueOf(k)))
                            return j;
                    }
                } else {
                    if (StringMatcher.match(String.valueOf(patientListArray.get(j).getName().charAt(0)),
                                            String.valueOf(mSections.charAt(i))))
                        return j;
                }
            }
        }
        return 0;
    }

    //@Override
        public int getSectionForPosition(int position) {
        return 0;
    }

    //@Override
    public Object[] getSections() {
        String[] sections = new String[mSections.length()];
        for (int i = 0; i < mSections.length(); i++)
            sections[i] = String.valueOf(mSections.charAt(i));
        return sections;
    }

}
Cantina answered 24/9, 2012 at 7:53 Comment(3)
Isn't "adding text upon touching the alphabet" called a keyboard?Tabanid
@H2CO3 Hi, do you know how to do it?Unsearchable
There used to be an Android tutorial about it but since they made a more complete doc, it disappeared.Hagbut
W
4

Hear is one cool example of what you need https://github.com/woozzu/IndexableListView

IN order to compile the project and get rid of korean text update the StringMatcher class

package com.woozzu.android.util;

public class StringMatcher {
    public static boolean match(String value, String keyword) {
        if (value == null || keyword == null)
            return false;
        if (keyword.length() > value.length())
            return false;

        int i = 0, j = 0;
        do {
            int vi = value.charAt(i);
            int kj = keyword.charAt(j);
            if (isKorean(vi) && isInitialSound(kj)) {
            } else {
                if (vi == kj) {
                    i++;
                    j++;
                } else if (j > 0)
                    break;
                else
                    i++;
            }
        } while (i < value.length() && j < keyword.length());

        return (j == keyword.length())? true : false;
    }

    private static boolean isKorean(int i) {
        return false;
    }

    private static boolean isInitialSound(int i) {
        return false;
    }
}
Welford answered 24/9, 2012 at 8:8 Comment(11)
private final static char KOREAN_UNICODE_START = '가'; private final static char KOREAN_UNICODE_END = '힣'; private final static char KOREAN_UNIT = '까' - '가'; private final static char[] KOREAN_INITIAL = {'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', '�', 'ㅂ', 'ㅃ', 'ㅅ' , 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', '�', 'ㅎ'};Unsearchable
this class is not going to be useful in your real example but in case you want to run the application, I have updated the answearWelford
Thanks a lot Nixit.. this sample works fine.. i will implement to my project nw.. Thanks once again.Unsearchable
accept the answer and close the post, so it can helpful to other as wellWelford
hi nixit... tday i started to implement ur code to my project.. but i am get error.. can u look over a code plz.. i will edit d above code..Unsearchable
can look over above code.. its error in listview... can u say my mistake bro.Unsearchable
static data i have added in CountriesList.Unsearchable
thank u Its working fine but showing error here in charAt(0)) msg: The method charAt(int) is undefined for the type Object. Above there is a codeUnsearchable
its works fine nw.. but problem is when i press A in scroll bar its shows only 1 data of A in listview..i want if i press A in scrollbar all data of A must list in list view.. i will add code code plz help me..bossUnsearchable
data is coming from server It will be not arranged in Alphabetic order.. so when i press scroll bar(A) it must show all content of A in list view>How to do it Nixit.. thank u in advanceUnsearchable
put some little effort in understanding the code then start implementing, we are ready to help but we are not ready to do your work that part is you have to do it your selfWelford
T
0

If you want to show the alphabet clicked as a Toast (a custom toast like the one shown in the image) use this:

view.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_custom_layout,
        (ViewGroup) findViewById(R.id.toast_layout_root));
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        // to position the Toast on the screen
        toast.setDuration(Toast.LENGTH_LONG);
        // to set the duration, the toast should appear
        toast.setView(layout);
        // a custom layout for the toast
        toast.show();
    }
});

and your toast_custom_layout.xml will be a 100 x 100 layout with large text size

Technicolor answered 24/9, 2012 at 8:17 Comment(1)
my alphabet key is very small exactly. Example:- When i am take my finger near to alphabet key it must display in large side like E in image that textview/ toast/some what.. I think realtime example like contacts in android 4.0.. here they used this concept.Unsearchable
T
0

I have displayed the list inside a fragment, if you want to display in main activity then you can pass this, instead of getActivity()

//method to display the side indexed scroll list of alphabets 
    public void displayAlphabetsList() {
        final List<String> listOfAlphabet = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            char alphabet = (char) (ASCII_VALUE_OF_A + i);
            listOfAlphabet.add(String.valueOf(alphabet));
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listOfAlphabet);
       alphabets_List_View.setAdapter(adapter);

        alphabets_List_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ///scroll the recycler view to that position where matching letter was found
                int positionToScroll = 0;
                for (int i = 0; i < mContacts.size(); i++) {
                    if (mContacts.get(i).getFirstName().startsWith(listOfAlphabet.get(position)))
                        break;
                    else
                        positionToScroll++;
                }
                recyclerView.scrollToPosition(positionToScroll);
            }
        });
    }
Tonneau answered 8/5, 2020 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.