Incorrect cursor adapter location

I have a custom cursor adapter that retrieves the date from the database and populates the list, and in my onlistItemClick listener I show the view attached to this line and then hide this view by clicking it again,
everything works fine, but I have two problems ,
1) - I have 10 lines, it shows 10, but when I record positions, it gives me only those positions that are suitable for the screen.
2) - when I click on a line, a view appears attached to this line, but when I scroll down, I find that another point of view is visible on the screen,

here is my adapter

    public class CustomCursorAdapter extends CursorAdapter {
    private LayoutInflater mInflater; 

    public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {
        TextView textView = (TextView) view.findViewById(R.id.txtview);
        textView.setText(cursor.getString(cursor.getColumnIndex("text")));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        //in that "list_layout.xml" i made a view that has to show and hide on click listener
        View rowView = mInflater.inflate(R.layout.list_layout, parent, false);
        if(cursor.getPosition()%2 != 0)
        {
            rowView.setBackgroundResource(R.drawable.list_selector);
        }
        return rowView;
    }
}

, , .
, .

0
2

1: Android , , .

2: else :

if(cursor.getPosition()%2 != 0)
{
   rowView.setBackgroundResource(R.drawable.list_selector);
}
else
{
   rowView.setBackgroundResource(R.deawable.whatever); // your view for even number rows
}
0

getView.

:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mInflater
                    .inflate(R.layout.list_layout, parent, false);
        if (getCursor().getPosition() % 2 != 0) {
            convertView.setBackgroundResource(R.drawable.list_selector);
        }else{

            convertView.setBackgroundResource(<-Something->);
        }
        return convertView;

    }

 @Override
    public void bindView(View view, final Context context, final Cursor cursor) {
        TextView textView = (TextView) view.findViewById(R.id.txtview);
        textView.setText(cursor.getString(cursor.getColumnIndex("text")));
        if(cursor.getPosition()%2 != 0)
        {
            view.setBackgroundResource(R.drawable.list_selector);
        }
    }
0

Source: https://habr.com/ru/post/1532050/


All Articles