java - Android SimpleCursorAdapter 保持滚动样式

标签 java android simplecursoradapter

我有一个简单的光标适配器,它工作正常并显示所有数据。 我的监听器在单击时更改颜色:

listViewM.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    boolean exists = false;
                    TextView item = (TextView) view.findViewById(R.id.r_lv_name);
                    String selectedAnswer = item.getText().toString();
                    MultiSelection multiSelection = new MultiSelection((int) id, selectedAnswer);

                    for (MultiSelection mm : mMultiSelectionsArray) {
                        if (id == mm.getId()) {
                            mMultiSelectionsArray.remove(mm);
                            exists = true;
                            break;
                        } else {
                            exists = false;
                        }
                    }

                    if (!exists) {

                        mMultiSelectionsArray.add(multiSelection);
                        item.setTextColor(Color.parseColor("#2EFE2E"));
                    } else {

                        mMultiSelectionsArray.remove(multiSelection);
                        item.setTextColor(Color.parseColor("#000000"));
                    }

                }
            });

现在滚动适配器正在回收 View 并将新项目标记为选定(通过添加颜色)。我想我需要以某种方式保持状态,然后将其应用于 View 创建,但经过 3 天的查看后我放弃了。有人可以帮忙吗?

最佳答案

尝试使用自定义适配器,例如:

public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {

    public CustomSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return super.newView(context, cursor, parent);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        //HERE you can set the correct color for each item

        TextView item = (TextView) view.findViewById(R.id.r_lv_name);
        boolean exists = //check is item is selected
        if (!exists) {
             item.setTextColor(Color.parseColor("#2EFE2E"));
        } else {
             item.setTextColor(Color.parseColor("#000000"));
        }
    }
}

关于java - Android SimpleCursorAdapter 保持滚动样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41409387/

相关文章:

android - 方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) 一直给我不适用的错误

android - ListView 性能低下

java - 如何在 iReport 中截断数据(避免分页)?

java - 如何加载一个文件以供多种方法访问?

java - 没有打印任何内容

java - 反射清除Android应用程序缓存的方法

android - 在颜色选择器中使用 appcompat 的 ?colorAccent 似乎不起作用

android - Quickblox Android SDK : Login with facebook token issue

java - 自定义 Swing 组件向父类返回值

android - 使用简单的光标拖放