问题
i have a custom cursor adapter that fetches date from database and populate list , and its onlistItemClick listener i show a view attached to that row, and then hide that view by clicking it again,
everything is working fine, but i have two problems,
1)- i have 10 rows, it shows 10, but when i log positions, it gives me only those positions, that fits to screen.
2)- when i click on a row, view appears that is attached to that row, but when i scroll down, i found another view is visible down 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;
}
}
I am working from hours to figure out the issue, but in vain..
Please help any care taker.
回答1:
Answer to question 1 : Android initializes rows that fit in the screen at a given time not all of them at once for performance issues.
Answer to question 2 : try providing an else clause like this :
if(cursor.getPosition()%2 != 0)
{
rowView.setBackgroundResource(R.drawable.list_selector);
}
else
{
rowView.setBackgroundResource(R.deawable.whatever); // your view for even number rows
}
回答2:
Try writing getView method instead.
For example:
@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;
}
EDIT
@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);
}
}
来源:https://stackoverflow.com/questions/20278974/custom-cursor-adapter-wrong-position