单击recyclerview中的项目:从输入流

时间:2017-06-21 11:19:50

标签: android

我是Android开发的新手,我一直在关注一些教程并设法获得一个回收站视图,其中包含来自返回json对象的url的数据。

最近,我尝试在回收站视图中的项目中添加一个点击监听器,但每当我点击这些项目时,应用程序就会停止工作,我收到以下错误

W/ErrorProcessor: onFatalError, processing error from engine(4) com.google.android.aps.gsa.shared.speech.a.g: Error reading from input stream

这是我的界面:

public interface ItemClickListener {

    public void onClick(View view, int position, boolean isLongClick);
}

我的Adapter类的代码,它使用我的用户类,但只是获取/设置:

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
    Context context;

    private List<User> mDataset;

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        public TextView mUserId;
        public TextView mUsername;

        private ItemClickListener itemClickListener;

        public ViewHolder(View v) {
            super(v);

            mUserId = (TextView) v.findViewById(R.id.user_id);
            mUsername = (TextView) v.findViewById(R.id.username);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        public void setItemClickListener(ItemClickListener itemClickListener) {
            this.itemClickListener = itemClickListener;
        }

        @Override
        public void onClick(View v) {
            itemClickListener.onClick(v, getAdapterPosition(), false);
        }

        @Override
        public boolean onLongClick(View v) {
            itemClickListener.onClick(v, getAdapterPosition(), true);
            return true;
        }
    }

    public UserAdapter(List<User> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public UserAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mUserId.setText(mDataset.get(position).getUserId());
        holder.mUsername.setText(mDataset.get(position).getUsername());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {
                if (isLongClick) {
                    Toast.makeText(context, "Long Click: " + mDataset.get(position), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "" + mDataset.get(position), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

这是我处理凌空调用和回收站视图的Activity (我使用一个单独的类来解析输出并使其成为我可以使用的json对象,如果有必要的话我可以发布代码,但我没有看到它的相关性:

public class UserActivity extends AppCompatActivity {

    List<User> mDataset;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private DividerItemDecoration mDividerItemDecoration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);

        mRecyclerView = (RecyclerView) findViewById(R.id.rvUsers);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mDividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), mLayoutManager.getOrientation());
        mRecyclerView.addItemDecoration(mDividerItemDecoration);

        sendRequest();
    }

    private void sendRequest() {
        StringRequest stringRequest = new StringRequest("http://myurl.com/api/users",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        ParseCallJSON pj = new ParseCallJSON(response);
                        pj.parsecallJSON();
                        mDataset = pj.getCalls();

                        mAdapter = new CallAdapter(mDataset);
                        mRecyclerView.setAdapter(mAdapter);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(UserActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}

1 个答案:

答案 0 :(得分:1)

更改

public UserAdapter(List<User> myDataset) {
        mDataset = myDataset;
    } 

要:

public UserAdapter(List<User> myDataset, Context context) {
        mDataset = myDataset;
        this.context=context;
    } 

然后替换:

mAdapter = new CallAdapter(mDataset);

为:

mAdapter = new CallAdapter(mDataset,UserActivity.this);

并点击通话:

Toast.makeText(context, "Long Click: " + mDataset.get(position).getUserId(), Toast.LENGTH_SHORT).show();