游标适配器数据绑定问题

时间:2014-10-14 08:54:48

标签: android android-cursoradapter

我在我的应用中使用了光标适配器。适配器绑定到listfragment。列表视图显示正确。在我的适配器视图中,我有两个textview和一个imageview。在imageview上,我添加了onClickListener。列表视图正确显示。但是,当我在列表中的任何索引处单击imageview时,不是获取该索引的数据,而是获取其他索引的数据。

例如,如果我点击listview项目2中的imageview(列表中的项目位置将为1),而不是从光标获取该位置的数据,它将获取其他位置的数据( 5,6,0等)。我搜索了很多这个问题,但无法找到任何相关的答案。下面是我的CursorAdapter's代码

public class ClientListAdapter extends CursorAdapter {
    private static final String TAG = "ClientListAdapter";
    private final int PRIMARY_PHOTO_INDEX = 1;
    private final int LAST_NAME_INDEX = 2;
    private final int FIRST_NAME_INDEX = 3;
    private final int CLIENT_GUID_INDEX = 4;
    private NavigateToClientDetailListener mClientDetailListener;

    public ClientListAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.client_list_adapter, parent, false);
        holder.imgClientPhoto = (ImageView) v.findViewById(R.id.imgClientPhoto);
        holder.txtFirstName = (TextView) v.findViewById(R.id.txtFirstName);
        holder.txtLastName = (TextView) v.findViewById(R.id.txtLastName);
        holder.navigateNext = (ImageView) v.findViewById(R.id.navigateNext);
        v.setTag(holder);
        return v;
    }

    @Override
    public void bindView(final View view, final Context context,
            final Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();

        // byte[] imgByteArray = cursor.getBlob(PRIMARY_PHOTO_INDEX);

        holder.txtLastName.setText(cursor.getString(LAST_NAME_INDEX));
        holder.txtFirstName.setText(cursor.getString(FIRST_NAME_INDEX));

        holder.navigateNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.e(TAG, "btnNext onCLick "+" Client Name: "+cursor.getString(LAST_NAME_INDEX)+" "+cursor.getString(FIRST_NAME_INDEX));
                navigateToClientDetail(cursor.getString(CLIENT_GUID_INDEX));
            }
        });
    }

    static class ViewHolder {
        ImageView imgClientPhoto, navigateNext;
        TextView txtLastName, txtFirstName;
    }

    private void navigateToClientDetail(String clientGuid) {
        mClientDetailListener.gotoClientDetail(clientGuid);
    }

    public void setNavigateToClientDetailListener(
            NavigateToClientDetailListener mClientDetailListener) {
        this.mClientDetailListener = mClientDetailListener;
    }
}

1 个答案:

答案 0 :(得分:1)

得到了解决方案。我忘了每个项目都会调用bindView。因此,在前一种情况下,cursor.getString(CLIENT_GUID_INDEX)始终返回光标中最后一项的guid id。我将代码更改为:

final String clientGuid = cursor.getString(CLIENT_GUID_INDEX);

holder.navigateNext.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
       navigateToClientDetailFragment(clientGuid);
    }
});

如果有人认为我的假设是错误的,我想发表意见,可能的原因是其他因为我对cursor adapter不太熟悉:)。