index list view with database cardview

182 Views Asked by At

I know there are alot of duplicates with issues like these but pleas do read mine and help me out.

I am very new to Android development as such I coded these with my own instincts and limited guides available.

I'd like to implement an indexable (A-Z) side panel just like in contacts. All the posts available are array strings with hard-coded entries but mine aren't, I am using DB Browser for SQLite and then populate each data in a cardview.

I have referred to (https://github.com/woozzu/IndexableListView/blob/master/src/com/woozzu/android/widget/IndexableListView.java) and tried all the solutions mentioned related to this post but to no avail. I believe I simply coded it the wrong way as the entries are hard-coded but mine aren't.

I am currently following this guide, (http://androidopentutorials.com/android-listview-with-alphabetical-side-index/) and have amended some codes here and there which I thought is necessary for my situation, such as adding my database into an array. However, there are multiple errors and I don't know what to do.

As such, I have no idea how to solve these errors proceed on. Please do help me out and provide me with, perhaps a step-by-step tutorial or even better video guides so that I could follow through.

Below are what I have tried:

Declaration

//indexable list view
Map<String, Integer> mapIndex;
List<String> dbList = new ArrayList<>();

OnCreate

//indexable list view
String[] dbList= database.getKeyword();
Arrays.asList(dbList);
dbList.setAdapter(new ArrayAdapter<String>(this, 
android.R.layout.simple_expandable_list_item_1, dbList));
etIndexList(dbList);
displayIndex();

//indexable list view
private void getIndexList(String[] dbList){

mapIndex = new LinkedHashMap<String, Integer>();
for(int i = 0; i < dbList.length; i++){
String db = dbList[i];
String index = db.substring(0,1);

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

    }
}

    private void displayIndex(){
    LinearLayout indexLayout = (LinearLayout) findViewById(R.id.side_index);

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

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

}

The errors are:

Error 1

Error 2

Error 3

Error 4

If you require any more codes do let me know and I will update this post. Thank you in advance.

2

There are 2 best solutions below

0
On

follow this lib for indexing list no need to write extra function this lib manage all the case

0
On

Answer to the errors that you are getting:

  1. You are calling dbList.setAdapter(): dbList is not a ListView or RecyclerView Call setAdapter() method on appropriate ListView or RecyclerView variable.

  2. You are passing this to setOnClickListener() method: this is pointing to your KnowledgeActivity and not a View.OnClickListener. Solution: Either implement View.OnClickListener in your activity or call new OnClickListener like this,

    textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    }});
    
  3. database.keyword() returns List and you are trying to cast it to String[]