Android RecyclerView:如何将数据从适配器放入片段?

时间:2016-08-02 04:56:02

标签: android sqlite android-fragments android-recyclerview

我有一个RecyclerView列表。我试图在MainActivity中设置一个onItemLongClick方法来启动DialogFragment。该对话框将要求用户确认他们要删除RecyclerView列表中的项目。我想从Adapter文件中捕获RecyclerView项的位置,以便片段可以从SQLite数据库中删除正确的项。

我正在尝试使用setArguments传递Bundle中的RecyclerView项目位置。 Android Studio在“bundle.putInt”行上发出以下错误:错误的第二个参数类型。找到:'联系'(我的数据模型),需要'int'。我在这里错过了什么?

P.S。我还有一个“onItemClick”方法正确加载另一个名为“ActActivity”的Activity,下面也会显示该参考。 onItemClick和onItemLongClick都在Adapter文件中设置了ClickListener接口。两种点击方法都使用Toasts进行测试,并且工作正常。

来自活动档案:

...
@Override
public void onItemLongClick(int position, View view) {
    // For API >=14, use getFragmentManager() below.
    Bundle bundle = new Bundle();
    bundle.putInt("item position", **contactListAdapter.getItem(position)**); // error here.

    android.app.FragmentManager fm = getFragmentManager();
    fm.setArguments(bundle);
    DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();
    delCardViewDialog.show(fm,"delcardview dialog");
}

@Override
public void onItemClick(int position, View view) {
    ActActivity.start(this, contactListAdapter.getItem(position)); 
}

来自RecyclerView的适配器文件:

public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ContactHolder>{

    private List<Contact> contactList;
    private Context context;

    private RecyclerItemClickListener recyclerItemClickListener;

    public ContactListAdapter(Context context) {
        this.context = context;
        this.contactList = new ArrayList<>();    
    ...

    **public Contact getItem(int position)** {
        return contactList.get(position);
    }

片段文件:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {    
...
Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
    btnOK.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                                      // removeItem() here?
            dismiss();
        }
    });

2 个答案:

答案 0 :(得分:1)

让您的活动实施View.OnLongClinkListner

    public class MainActivity extends AppCompatActivity implements View.OnLongClinkListner {
@Override
    public boolean onLongClick(View v) {
int position=(Integer)v.getTag();
        return false;
    }
}



 public void remove(int position){
   adapter.remove(position);
}

并在你的适配器

View.OnLongClinkListner  mLongClickListner;
public ContactListAdapter(Context context) {

 mLongClickListner=(View.OnLongClinkListner)context;
}

@Override
        public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.some_layout, parent, false);
view.setOnLongClickListner(mLongClickListner);
            return new ContactHolder(view);
        }



@Override
        public void onBindViewHolder(final ContactHolder holder, final int position) {
... your code;
holder.itemView.setTag(position);
}

//从recycler-view

中删除项目
public void removeItem(int position)
{
contactList.remove(position);
notifyDataSetChanged();
}

答案 1 :(得分:0)

bundle.putInt函数需要第二个参数中的整数,您传入的是&#39; Contact&#39;。 通过&#39;位置&#39;作为第二个参数而不是&#39; contactListAdapter.getItem(position)&#39;