Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在custom`CursorAdapter中重用动态添加的视图`_Android_Cursor_Simplecursoradapter_Android Cursoradapter_Android Cursor - Fatal编程技术网

Android 如何在custom`CursorAdapter中重用动态添加的视图`

Android 如何在custom`CursorAdapter中重用动态添加的视图`,android,cursor,simplecursoradapter,android-cursoradapter,android-cursor,Android,Cursor,Simplecursoradapter,Android Cursoradapter,Android Cursor,我使用bindView()方法是自定义CursorAdapter实现来动态地将文本视图添加到列表中 每个列表项由list\u itemlayout表示,其中包含flow\u layoutlayout from 要重新迭代,我希望每个flow\u布局中每个list\u项实例的文本视图数反映光标返回的行值数 public void bindView(View view, Context context, Cursor cursor) { // Flow layout wraps new v

我使用
bindView()
方法是自定义
CursorAdapter
实现来动态地将文本视图添加到列表中

每个列表项由
list\u item
layout表示,其中包含
flow\u layout
layout from

要重新迭代,我希望每个
flow\u布局
中每个
list\u项
实例的文本视图数反映光标返回的行值数

public void bindView(View view, Context context, Cursor cursor) {

    // Flow layout wraps new views around the screen.
    FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flow_Layout);

    // getRowValues() puts row values from cursor into an array list.
    ArrayList<> rowValues = getRowValues(cursor);

    // A new text view is created and inserted into Flow layout
    // for each value in rowValues 
    TextView tv;
    for value in rowValues {
        tv = = new TextView(ctx);
        tv.setText(value);
        flowLayout.addView(tv); 
    }

}
但是,每次我在列表项上重新滚动时,该特定项中的文本视图数量会翻倍,此外,绑定数据有时会在光标位置和列表项位置之间对称地反映。我认为这个问题与旧文本视图的回收有关。如何防止新文本视图堆叠到旧文本视图上

下面是自定义游标适配器的完整实现

public class DatabaseAdapter extends CursorAdapter {

        Context ctx;

        public DatabaseAdapter(Context context, Cursor cursor, int flags) {

            super(context, cursor, 0);
            ctx = context;
        }

        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View v = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
            return v;
        }

        public void bindView(View view, Context context, Cursor cursor) {

            // Flow layout wraps new views around the screen.
            FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flow_Layout);

            // getRowValues() puts row values from cursor into an array list.
            ArrayList<> rowValues = getRowValues(cursor);

            // A new text view is created and inserted into Flow layout
            // for each value in rowValues array list
            TextView tv;
            for value in rowValues {
                tv = = new TextView(ctx);
                tv.setText(value);
                flowLayout.addView(tv); 
            }
        }
}
公共类数据库适配器扩展游标适配器{
上下文ctx;
公共数据库适配器(上下文上下文、光标、int标志){
超级(上下文,游标,0);
ctx=上下文;
}
公共视图newView(上下文上下文、光标、视图组父对象){
视图v=LayoutFlater.from(上下文)。充气(R.layout.list_项,父项,false);
返回v;
}
公共void bindView(视图、上下文上下文、光标){
//Flow layout将新视图环绕在屏幕上。
FlowLayout=(FlowLayout)视图.findViewById(R.id.flow\u布局);
//getRowValues()将光标中的行值放入数组列表中。
ArrayList rowValues=getRowValues(光标);
//将创建一个新的文本视图并将其插入到流布局中
//对于rowValues数组列表中的每个值
文本视图电视;
对于行值中的值{
tv==新文本视图(ctx);
tv.setText(值);
flowLayout.addView(电视);
}
}
}

线性布局中
您必须已经添加了
文本视图
,比如id
tv1

而不是:

TextView tv1=newtextview()

这样做:

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout);
TextView tv1 = layout.findViewById(R.id.tv1);
tv1.setText(Title);

为您的
tv1
设置一个id,然后您可以调用
findViewById
来检查该视图之前是否已添加,但实际上,您为什么不在
R.layout.list\u item
中添加该
TextView
?@pskink谢谢。成功了。事实上,我确实尝试过设置ID并使用
findViewById
,但我没有考虑在
createView
方法中使用
findViewById
,而是在
bindView
中使用了它。。。区别是什么?为什么不在
R.layout.list\u item
中添加
TextView
?@pskink谢谢,你帮我找到了解决方案……你也可以替代getView()。
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout);
TextView tv1 = layout.findViewById(R.id.tv1);
tv1.setText(Title);
if(LinearLayout.getChildCount() > 0)
        LinearLayout.removeAllViews();