SimpleCursorAdapter 在 bindView 上重复单击操作

Posted

技术标签:

【中文标题】SimpleCursorAdapter 在 bindView 上重复单击操作【英文标题】:SimpleCursorAdapter duplicates click action on bindView 【发布时间】:2017-01-08 14:15:56 【问题描述】:

试图在 listView 子项上设置 OnClickListener 以在单击时更改列表项背景,因此我自定义 SimpleCursorAdapter 如下。它可以工作,但也会更改屏幕外的其他列表项颜色(比单击一个下降 8 步,例如:如果单击项目的父位置为 0 它还在位置 7 处更改了背景,以相同的方式将 3 更改为 10,)。我也有一个工作正常的 setOnItemClickListener。不明白我做错了什么?提前致谢。

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();
        
    );


这是我的清单:

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();






 

【问题讨论】:

【参考方案1】:
private selectedItem = -1;

然后在 getView 上添加:

if(position == selectedItem)
//set selected background color 
else 
//set normal background color 

点击:

selectedItem = position;
notifiyDataSetChanged();

【讨论】:

谢谢,但没有帮助【参考方案2】:

试试这个:

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();
        
    );
 

【讨论】:

以上是关于SimpleCursorAdapter 在 bindView 上重复单击操作的主要内容,如果未能解决你的问题,请参考以下文章

在 SimpleCursorAdapter 上使用 notifyDataSetChanged 不起作用

使用 EditText 过滤 SimpleCursorAdapter 支持的 ListView

子类化 SimpleCursorAdapter 以包含 convertView 以节省内存

使用 SimpleCursorAdapter 基于 sqlite 查询更新列表视图

更新 SimpleCursorAdapter,同时保持 ListView 中的滚动位置

具有 ListView 可见性的 SimpleCursorAdapter