0

我知道有很多重复这样的问题,但请阅读我的并帮助我。

我对 Android 开发非常陌生,因此我用自己的直觉和有限的指南编写了这些代码。

我想实现一个可索引(AZ)侧面板,就像在联系人中一样。所有可用的帖子都是带有硬编码条目的数组字符串,但我的不是,我使用 DB Browser for SQLite,然后在卡片视图中填充每个数据。

我已经提到(https://github.com/woozzu/IndexableListView/blob/master/src/com/woozzu/android/widget/IndexableListView.java)并尝试了与这篇文章相关的所有解决方案,但无济于事。我相信我只是以错误的方式编码它,因为条目是硬编码的,但我的不是。

我目前正在关注本指南(http://androidopentutorials.com/android-listview-with-alphabetical-side-index/)并在这里和那里修改了一些我认为对我的情况是必要的代码,例如添加我的数据库到一个数组中。但是,有多个错误,我不知道该怎么办。

因此,我不知道如何继续解决这些错误。请帮助我并为我提供分步教程甚至更好的视频指南,以便我可以跟进。

以下是我尝试过的:

宣言

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

创建时

//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()));

}

错误是:

错误 1

错误 2

错误 3

错误 4

如果您需要更多代码,请告诉我,我会更新这篇文章。先感谢您。

4

2 回答 2

0

回答您遇到的错误:

  1. 您正在调用dbList.setAdapter():dbList不是在适当的或ListView变量上RecyclerView 调用 setAdapter() 方法。ListViewRecyclerView

  2. 您正在传递thissetOnClickListener()方法:this指向您的KnowledgeActivity而不是View.OnClickListener. 解决方案:要么在您的活动中实施,要么像这样View.OnClickListener调用 new ,OnClickListener

    textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    }});
    
  3. database.keyword()返回List并且您正在尝试将其转换为String[]

于 2018-11-14T07:22:13.323 回答
0

按照这个索引列表不需要编写额外的函数这个管理所有的情况

于 2018-11-14T07:16:15.957 回答