我在哪里做错了:CursorAdapter和Listview

时间:2017-09-10 17:15:55

标签: android listview

我的第一个应用。

当我点击第1行的按钮时,我希望它为该特定行隐藏TextView。这样做但是当我向下滚动时,我发现它也隐藏了其他的`TextView'。我读了一下,发现随着回收的发生,这是预料之中的。

现在,我想知道如何确保在单击Button时只隐藏特定行的TextView。我可能有多行甚至7000+

以下是CursorAdapter代码:

public class ToDoCursorAdapter extends CursorAdapter {
    public ToDoCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.listview, parent, false);
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    final TextView engText = (TextView) view.findViewById(R.id.engText);
   final TextView arabText = (TextView) view.findViewById(R.id.arabText);
    final TextView refText = (TextView) view.findViewById(R.id.refText);
    Button buttonIA = (Button) view.findViewById(R.id.buttonIA); //For Arabic Text
    Button buttonIR = (Button) view.findViewById(R.id.buttonIR); //For Ref Text

    String arabic = cursor.getString(cursor.getColumnIndexOrThrow("PlainArab_Text"));
    String english = cursor.getString(cursor.getColumnIndexOrThrow("PlainEng_Text"));
    String ref = cursor.getString(cursor.getColumnIndexOrThrow("REF"));

    arabText.setText(arabic.trim().replaceAll(" +", " "));
    engText.setText(english.replaceAll("[\n]{2,}", "TWOFEEDS").replaceAll("\n", " ").replaceAll(" +", " ").replaceAll("<br/>", "\n").replaceAll("TWOFEEDS", "\n"));
    refText.setText(ref.trim().replaceAll(" +", " "));
    arabText.setVisibility(View.VISIBLE);
    engText.setVisibility(View.VISIBLE);

    buttonIA.setOnClickListener(new View.OnClickListener()
                                { @Override
                                    public void onClick(View v) {
                                        arabText.setVisibility(View.GONE);
                                    }
                                });

    buttonIR.setOnClickListener(new View.OnClickListener()
                                { @Override
                                public void onClick(View v) {
                                    refText.setVisibility(View.GONE);
                                    Log.d("MYTAG", "INSIDE2");
                                }
                                });
    }
}

Activity代码:

public class ListViewActivity extends Activity {
ListView listView ;
private SQLiteDatabase hadithlistDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    .....

    // Find ListView to populate
        ListView lvItems = (ListView) findViewById(R.id.list);
// Setup cursor adapter using cursor from last step
        ToDoCursorAdapter todoAdapter = new ToDoCursorAdapter(this, todoCursor);
// Attach cursor adapter to the ListView
        lvItems.setAdapter(todoAdapter);

        todoAdapter.changeCursor(todoCursor);
    }

}

已更新

确定。我刚刚在我的应用中插入了调试评论/消息,以了解此CursorAdapter的工作原理。当您向上和向下滚动时,它将在调试注释中显示您的行#或记录号(在我的情况下)。

所以我想出了我将在创建时标记所有视图,当我点击行(例如1)上的按钮时,应该隐藏第1行上的TextView加上标记,即光标应该将位置添加到数组中。

到目前为止我实现的逻辑确实可以防止其他行受到Button的影响。

我点击了Button1并将它隐藏在第1行的Textview1上。然后我向下滚动以查看它是否影响了任何其他行,但它没有。我向上滚动并看到第1行上的Textview1可见,但在第2行它被隐藏。

任何人都可以看到此代码有什么问题:

public class ToDoCursorAdapter extends CursorAdapter {
List<Integer> selectedItemsPositions;//to store all selected items position

public ToDoCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
    selectedItemsPositions = new ArrayList<>();

}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    //ViewHolderItem viewHolder;
    View rowView = LayoutInflater.from(context).inflate(R.layout.listview, parent, false);

    rowView.setTag(cursor.getPosition());

    return rowView;
}

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

    final TextView arabText = (TextView) view.findViewById(R.id.arabText);
    final TextView engText = (TextView) view.findViewById(R.id.engText);
    final TextView refText = (TextView) view.findViewById(R.id.refText);

    Button buttonIA = (Button) view.findViewById(R.id.buttonIA); //For Arabic Text
    Button buttonIR = (Button) view.findViewById(R.id.buttonIR); //For Ref Text

    String arabic = cursor.getString(cursor.getColumnIndexOrThrow("PlainArab_Text"));
    String english = cursor.getString(cursor.getColumnIndexOrThrow("PlainEng_Text"));
    String ref = cursor.getString(cursor.getColumnIndexOrThrow("REF"));

    arabText.setText(arabic.trim().replaceAll(" +", " "));
    engText.setText(english.replaceAll("[\n]{2,}", "TWOFEEDS").replaceAll("\n", " ").replaceAll(" +", " ").replaceAll("<br/>", "\n").replaceAll("TWOFEEDS", "\n"));
    refText.setText(ref.trim().replaceAll(" +", " "));

    view.setTag(cursor.getPosition());

    if(selectedItemsPositions.contains(cursor.getPosition())) {
        arabText.setVisibility(View.GONE);
    }
    else {
        arabText.setVisibility(View.VISIBLE);
    }

    buttonIA.setOnClickListener(
            new View.OnClickListener()
                    { @Override
                        public void onClick(View v) {
                            Integer position = (Integer) v.getTag();

                            if(selectedItemsPositions.contains(position)) {
                                arabText.setVisibility(View.VISIBLE);

                                selectedItemsPositions.remove((Object) cursor.getPosition());
                            }
                            else {
                                arabText.setVisibility(View.GONE);
                                Log.d("MYTAG", "CLICKE! INSIDE ELSE");
                                selectedItemsPositions.add( cursor.getPosition());
                            }
                        }
                    }
    );

    buttonIR.setOnClickListener(
            new View.OnClickListener()
                { @Override
                    public void onClick(View v) {
                        Log.d("MYTAG", "INSIDE2");
                    }
                }
    );
}

}

1 个答案:

答案 0 :(得分:0)

我能够通过覆盖getView方法来获取单元格的位置值来修复此问题,使用游标的getPosition有点乱,因为它是每个{{的更新1}}调用more info,但仍然调用newViewgetView会使视图膨胀两次(需要在那里进行一些调整)

这是最终代码(此代码类似于您的其他问题,没有时间更新它)

newView