问题
i am create a listview which has two TextView
and one custom view as a selection indicator , to load data i am using CursorAdapter
and overriding bindView
and newView
. but the issue is in preserving the selection index.
when i am tapping a list item , i am saving the position of it into view as follows
View v1=(View)view.findViewById(R.id.viewColor);
v1.setTag(position)
but inside a bindView
i not getting a position where i can perform a match by fetching position from view's tag as follows
Integer position = (Integer) v1.getTag();
but i am not getting a position to compare just the way we get in getView
. i tried cur.getPosition() is the position of a record in cursor which wont match with the v1.getTag().
i tried overriding getView as follows , but not getting accurate position
@Override
public View getView(final int position, View convertView, ViewGroup parent){
this.position = position;
return super.getView(position, convertView, parent);
}
so how i can i get the position in bindView ? do i need to use getView or what ? i explored many threads but i didn't get any definite answer when it comes to bindView.
EDIT
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long arg3) {
view.setTag(position);
// i save the position on list item click so that when adapter loads the
// the list i can match the position with the current one
}
回答1:
For This first you need to override getView()
and set the position here and access it in bindview()
later.
@Override
public View getView(int position, View convertview, ViewGroup arg2) {
if (convbertview == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertview = inflater.inflate(R.layout.your layout,
null);
}
convertview.setTag(position);
return super.getView(position, convbertview, arg2);
}
and in bindView()
get your position like
@Override
public void bindView(View view, Context context, Cursor cursor) {
int position=(Integer) view.getTag();//here is the position
}
回答2:
Don't override getView since you use CursorAdapter. You can register onClickListener in the bindView. Let's say you listen for tapping on the icon, then you do like this:
@Override
public void bindView(View vw, Context ctx, final Cursor cursor) {
/*
* do your binding
*/
ImageView icon = (ImageView) vw.findViewById(R.id.your_icon);
icon.setTag(cursor.getPosition());
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// here you pass right position to your activity
((MainActivity) context).onIconClick((int)v.getTag());
}
});
}
In your activity just:
public void onIconClick(int position) {
// do your stuff
}
回答3:
Change your onItemClick like below
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAdapter.setId(id);
//Set the selector here
}
Then maintain one id variable in the adapter and write the setters and getters for it. then change your bindView() method as below
@Override
public void bindView(View view, Context context, Cursor cursor) {
long tempId = cursor.getLong(cursor.getColumnIndex("_id"));
if(tempId==id){
//Set the selector color here
}
else{
//Set the default color here
}
//Remaing your logic here
}
In this way you no need to override getView() method.
If you want to select multiple views you have to maintain the ArrayList of ids...
回答4:
Inside the "bindView" method but out of "onClickListener()":
final int position = cursor.getPosition();
Inside the "onClickListener()":
cursor.moveToPosition(position); //retrieve any data you want after this
Good luck~
来源:https://stackoverflow.com/questions/27666203/get-a-position-in-bindview-using-cursoradapter