无法通过意图发送网址添加额外的方法

时间:2016-10-22 15:34:41

标签: android android-intent android-recyclerview recycler-adapter

Got the solution thanks to thomaspsk

所以我正在从在线数据库解析链接网址并在回收站视图中显示它。我还希望通过Intent的{​​{1}}方法

发送这些网址

我用

尝试了
putExtra()

但它不起作用

这是一个完整的适配器代码 还请检查我是否在正确的位置使用点击监听器 我看到有人在Bundle bundle = intent.putExtra("image_link",my_data.getImage_link()); 下使用它。

ViewHolder

3 个答案:

答案 0 :(得分:0)

要使用Bundle简单发送数据,请执行以下操作:

Intent intent = new Intent(context,article.class);
Bundle bundle = new Bundle();
bundle.putString("IMAGE_LINK", my_data.getImage_link());
intent.putExtras(bundle); // or intent.putExtra("BUNDLE",bundle); and reveive there : Bundle b = getBundleExtra("BUNDLE");
context.startActivity(intent);

要检索您可以关注的其他活动中的数据:

Bundle bundle = getIntent().getExtras();
String imageLink = bundle.getString("IMAGE_LINK");

答案 1 :(得分:0)

首先,您需要使用get方法从列表中获取该项目,而不是该项目中的网址。

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_child,parent,false);

    final ViewHolder holder = new ViewHolder(itemView);
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = holder.getAdapterPosition();
            Intent intent = new Intent(context,article.class);
            intent.putExtra("image_link",my_data.get(position).getImage_link());
            context.startActivity(intent);

        }
    });

    return new ViewHolder(itemView);
}

答案 2 :(得分:0)

MyAdapter 中的

代码行private Context context;很可能不是“活动”上下文。

您需要在创建MyAdapter时传递活动,例如:

MyAdapter myAdapter = new MyAdapter(callerActivity, my_data);

但这容易受到上下文泄漏的影响。

更好的方法是使用听众onClick()引导至您的活动片段

首先,在适配器中创建侦听器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>   {

  private List<MyData> my_data;

  public MyAdapter(List<MyData> my_data) {
    // this.context = context; // You don't need to use context here anymore.
    this.my_data = my_data;
  }

  ...

  // Define listener member variable
  private static OnItemClickListener mListener;

  // Define the listener interface
  public interface OnItemClickListener {
    void onItemClick(int position);
  }

  // Define the method that allows the parent activity or fragment to define the listener
  public void setOnItemClickListener(OnItemClickListener listener) {
    this.mListener = listener;
  }
  ...
}

第二次,点击项目后,在 MyAdapter onCreateViewHolder()中调用听众:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_child,parent,false);
  final ViewHolder holder = new ViewHolder(itemView);
  itemView.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       onItemClick(holder.getAdapterPosition());
     }
   });

   return new ViewHolder(itemView);
}

上次,您需要在使用适配器的Activity或Fragment中实现MyAdapter.OnItemClickListener。类似的东西:

public class YourActivity extends Activity
    implements MyAdapter.OnItemClickListener {

   ...

  @Override 
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // you need to add this after creating the adapter.
    // this only for example.
    MyAdapter myAdapter = new MyAdapter(my_data); // Don't use a context here anymore.
    myAdapter.setOnItemClickListener(this); // Set listener to parent.
  }

  // method implemented from MyAdapter.OnItemClickListener
  @Override 
  public void onItemClick(int position) {
    // Call the intent here
    Intent intent = new Intent(this, article.class);
    Bundle bundle = intent.putExtra("image_link",my_data.get(position).getImage_link());
    this.startActivity(intent);  
  }

  ...
}