[英]SimpleCursorAdapter duplicates click action on bindView
trying to setOnClickListener on a listView child item to change list item background on click, so i customized SimpleCursorAdapter as below.It works but also changes other list items color that out of screen (8 step down than clicked one, eg: if clicked items parent position is 0 it also changed background at position 7, In the same way for 3 to 10,). 尝试在listView子项上设置setOnClickListener以更改单击时的列表项背景,因此我按如下方式自定义了SimpleCursorAdapter。它可以工作,但也可以更改其他列表项的颜色,使其不显示在屏幕上(比单击的项低8位,例如:如果单击的项为父项如果位置为0,则也会以7到10的相同方式更改位置7的背景。) i also have a setOnItemClickListener that works fine.
我也有一个很好的setOnItemClickListener。 Cant get what wrong i did?
不能弄错我做了什么? Thanks in advance.
提前致谢。
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import com.bbt.alright.imeetup.R;
public class ProgramsCA extends SimpleCursorAdapter{
public ProgramsCA(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_program, parent, false);
return view;
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
super.bindView(view, context, cursor);
final ImageView iv = (ImageView) view.findViewById(R.id.imageViewCalV);
final RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.fTheProgram);
iv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
View parentRow = (View) view.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
iv.setBackgroundColor(Color.TRANSPARENT);
rl.setBackgroundColor(Color.parseColor("#D98BC34A"));
Toast.makeText(context, "Clicked "+position , Toast.LENGTH_LONG).show();
}
});
}
}
And here is my List: 这是我的清单:
public class EventsList extends Fragment {
private SQLiteHandler db;
private ListView lv;
// Store instance variables
private String title;
private int page;
// newInstance constructor for creating fragment with arguments
public static EventsList newInstance(int page, String title) {
EventsList fragmentFirst = new EventsList();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
db = new SQLiteHandler(getActivity().getApplicationContext());
View view = inflater.inflate(R.layout.frag_event_list,container,false);
lv = (ListView) view.findViewById(R.id.programs_list);
makeMyList();
return view;
}
public void makeMyList() {
Cursor cursor = db.getEventsByDate(title);
String[] source = new String[]
{
AppConfig.EVENT_NAME,
AppConfig.EVENT_TIME,
};
int[] toView = new int[]
{
R.id.event_title,
R.id.event_time,
};
ProgramsCA scA = new ProgramsCA
(
getActivity(),
R.layout.list_program,
cursor,
source,
toView,
1
);
ListView myList = lv;
myList.setAdapter(scA);
clickMyListItem(myList);
}
public void clickMyListItem(ListView lv)
{
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Cursor cursor = (Cursor) adapterView.getItemAtPosition(i);
int iCol_Name = cursor.getColumnIndex(AppConfig.EVENT_SERVER_ID);
int sid = cursor.getInt(iCol_Name);
Intent intent = new Intent(getActivity().getBaseContext(),
ItemActivity.class);
intent.putExtra("icon", (int) R.drawable.ic_action_event);
intent.putExtra("title", "Program Detail");
intent.putExtra("s_id", (int) sid);
getActivity().startActivity(intent);
}
});
}
public void myClickHandler(View v)
{
ListView lvItems = lv;
for (int i=0; i < lvItems.getChildCount(); i++)
{
lvItems.getChildAt(i).setBackgroundColor(Color.BLUE);
}
//get the row the clicked button is in
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
}
private selectedItem = -1;
then on getView add: 然后在getView上添加:
if(position == selectedItem)
//set selected background color
else
//set normal background color
onClick: 的onClick:
selectedItem = position;
notifiyDataSetChanged();
try this: 尝试这个:
private int selectedItemSid = -1;
@Override
public void bindView(View view, final Context context, Cursor cursor) {
super.bindView(view, context, cursor);
final ImageView iv = (ImageView) view.findViewById(R.id.imageViewCalV);
final RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.fTheProgram);
int iCol_Name = cursor.getColumnIndex(AppConfig.EVENT_SERVER_ID);
final int sid = cursor.getInt(iCol_Name);
if (sid == selectedItemSid) {
iv.setBackgroundColor(Color.TRANSPARENT);
rl.setBackgroundColor(Color.parseColor("#D98BC34A"));
} else {//set normal background color,assume white
iv.setBackgroundColor(Color.WHITE);
rl.setBackgroundColor(Color.WHITE));
}
iv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
selectedItemSid = sid;
notifiyDataSetChanged();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.