如何在单击recyclerView项时将数据导入webView活动

时间:2017-11-21 08:36:49

标签: java android android-intent webview

这是适配器类

package com.example.murarilal.xyz;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.EventLogTags;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Murarilal on 19-11-2017.
 */

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

            private List<ListItem> listItems;
            private Context context;

        public MyAdapter(List<ListItem> listItems, Context context) {
            this.listItems = listItems;
            this.context = context;
        }

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

            View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {

            ListItem listItem=listItems.get(position);

            holder.headText.setText(listItem.getHead());
            holder.desText.setText(listItem.getDes());
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    Intent intent=new Intent(context,ArticleActivity.class);
                    intent.putExtra("description",);
                    context.startActivity(intent);
                }
            });

          // Picasso.with(context).load(R.drawable.netfix).resize(900,300).into(holder.imageView);

        }

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

        public class ViewHolder extends RecyclerView.ViewHolder{

                public  TextView headText;
                public  TextView desText;
            public LinearLayout linearLayout;
                //public ImageView imageView;

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

                headText=(TextView) itemView.findViewById(R.id.heading);
                desText=(TextView) itemView.findViewById(R.id.description);
              //  imageView= (ImageView) itemView.findViewById(R.id.imageView);
                    linearLayout=(LinearLayout)itemView.findViewById(R.id.linearlayout);

            }
        }

    }

这是主要活动

package com.example.murarilal.xyz;

import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    public List<ListItem>listItems;
    RequestQueue queue;

    private String url="https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=d6eaad863cc640a7a6864d18196bea9e";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        queue= Volley.newRequestQueue(this);

       recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new Dec(20));
        listItems=new ArrayList<>();

        home();




    }

    public void home()
    {
        JsonObjectRequest request =new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONObject obj = new JSONObject(String.valueOf(response));

                    JSONArray array = obj.getJSONArray("articles");
                    for (int i=0;i<array.length();i++) {
                        JSONObject o = array.getJSONObject(i);
                        ListItem item =new ListItem(o.getString("title"),o.getString("description"));

                        listItems.add(item);
                    }
                    adapter=new MyAdapter(listItems,getApplicationContext());
                    recyclerView.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }
}

列表项活动

package com.example.murarilal.xyz;

/**
 * Created by Murarilal on 19-11-2017.
 */
public class ListItem {



     public String head;
     public String des;
    //private String image;



    public ListItem(String head, String des) {
        this.head=head;
     //  this.image=image;
        this.des=des;
    }


    public String getHead() {

        return head;
    }

    public String getDes() {
        return des;
    }


  /* public String getImage() {
        return image;
    }*/
}

我应该在Myadpater类的OnclickListener中传递什么字符串来获取WebView中的描述数据。应该是使用Description.or在putextra方法中传递的字符串请一般告诉我如何在工作之间传递数据与api和Json。

2 个答案:

答案 0 :(得分:0)

从适配器获取数据后

获取您要发送的对象的实例,并添加或设置您想要的任何属性:

//ListItem listItem = new ListItem();
ListItem listItem = listItems.get(position);

当您要发送时,请在下面添加额外内容:

Intent intent=new Intent(context, ArticleActivity.class);
intent.putExtra("listItem", listItem);
context.startActivity(intent);

在ArticleActivity onCreate方法中检索它,获取以下内容:

ListItem listItem = (ListItem) getIntent().getSerializableExtra("listItem");

在这里,您可以从该listItem对象获取所有属性

例如,获取说明:

String description = listItem.getDes();

请注意,您的类应该是可实现的Serializable:

class ListItem implements Serializable {

} 

希望它有所帮助。

答案 1 :(得分:0)

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ListItem listItem=listItems.get(position);

        holder.headText.setText(listItem.getHead());
        holder.desText.setText(listItem.getDes());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent=new Intent(context,ArticleActivity.class);
                intent.putExtra("description",);
                context.startActivity(intent);
            }
        });

      // Picasso.with(context).load(R.drawable.netfix).resize(900,300).into(holder.imageView);

    }

您可以intent.putExtra("description",);listItem.getDes()这里传递intent.putExtra("description", listItem.getDes());ListItem listItem=(ListItem) listItems.get(position);@Override public void onBindViewHolder(ViewHolder holder, int position) { ListItem listItem=listItems.get(position); holder.headText.setText(listItem.getHead()); holder.desText.setText(listItem.getDes()); holder.linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(context,ArticleActivity.class); intent.putExtra("description",listItem.getDesc()); context.startActivity(intent); } }); // Picasso.with(context).load(R.drawable.netfix).resize(900,300).into(holder.imageView); } 此行可能需要投射。

最后它将是

Intent intent = getIntent();
String description= intent.getStringExtra('description');

在您拥有webview的活动中。使用它来获取通过intent command to generate B.mk include B.mk compile and link 传递的数据 //现在你有了数据。

享受:)