0

当我单击 RecyclerView 中的卡片时,我正在尝试使用自定义 chrome 选项卡打开 URL。但是customTabsIntent.launchUrl() 希望第一个参数是一个活动。

我该如何实施?

这是我的代码:

我的 ArticleViewAdapter.java

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

        private ImageLoader imageLoader;
        private Context context;
        Intent intent;

        List<Article> articleFeed;

        public ArticleViewAdapter(List<Article> articleFeed, Context context){
            super();
            //Getting all the articles
            this.articleFeed = articleFeed;
            this.context = context;
        }





        @Override
        public ArticleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.article_view, parent, false);
         Log.i("Errore","in viewholder");
            ViewHolder holder=new ViewHolder(v);
            return holder;
        }

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

            final Article currentArticle=articleFeed.get(position);
            imageLoader = MySingleton.getInstance(context).getImageLoader();
            imageLoader.get(currentArticle.getImageUrl(), ImageLoader.getImageListener(holder.image, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

            holder.image.setImageUrl(currentArticle.getImageUrl(), imageLoader);
            holder.title.setText(currentArticle.getTitle());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    // set toolbar color and/or setting custom actions before invoking build()
                    CustomTabsIntent customTabsIntent = builder.build();

//THE ERROR OCCURES HERE
                    customTabsIntent.launchUrl(context,Uri.parse(currentArticle.getURL()));
                    Log.i("Errore",currentArticle.getURL());
                }
            });

        }

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

        class ViewHolder extends RecyclerView.ViewHolder{

            TextView title;
            NetworkImageView image;
            public ViewHolder(View itemView) {
                super(itemView);
                title=(TextView)itemView.findViewById(R.id.heading);
                image=(NetworkImageView)itemView.findViewById(R.id.image);
            }

        }
    }

我的 NewsActivity.java

public  class NewsActivity extends AppCompatActivity {

    private static String API_KEY="xxxxxxxxxxx";
    private List<Article> listOfArticles;
    static Article curArticle ;

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;
    private JSONArray jsonArticles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        Intent intent=getIntent();
        String id=intent.getStringExtra("id");
        String sortBy=intent.getStringExtra("sortBy");
        Toast.makeText(NewsActivity.this, id, Toast.LENGTH_SHORT).show();

        recyclerView = (RecyclerView) findViewById(R.id.recycview);
        recyclerView.setHasFixedSize(true);


        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        //Initializing our article list
        listOfArticles = new ArrayList<>();
        final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);

        //Creating a newe json object request
        Log.i("Errore","https://newsapi.org/v1/articles?source=" + id + "&sortBy="+sortBy+"&apiKey=" + API_KEY);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, "https://newsapi.org/v1/articles?source=" + id + "&sortBy="+sortBy+"&apiKey=" + API_KEY, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                Log.i("Errore","getting json");
                loading.dismiss();
                try {
                    jsonArticles=response.getJSONArray("articles");

                    for(int i=0;i<jsonArticles.length();i++){

                        Article article=new Article();
                        JSONObject jsonObject=jsonArticles.getJSONObject(i);
                        article.setTitle(jsonObject.getString("title"));
                     //   Log.i("Errore",jsonObject.toString());
                        article.setImageUrl(jsonObject.getString("urlToImage"));
                        article.setURL(jsonObject.getString("url"));
                        listOfArticles.add(article);

                    }

                    //Finally initializing our adapter
                    adapter = new ArticleViewAdapter(listOfArticles,NewsActivity.this);

                    //Adding adapter to recyclerview
                    recyclerView.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i("Errore","failed getting json");
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        //Adding request to the queue
        MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
    }



}

我不断收到错误:

Wrong 1st argument type.Found:android.content.Context, required: android.app.Activity

我应该怎么做才能修复它?有没有更好的方法来使用允许自定义 chrome 选项卡的 OnClick?

4

1 回答 1

0
customTabsIntent.launchUrl(MainActivity.activity,Uri.parse(currentArticle.getURL()));
                    Log.i("Errore",currentArticle.getURL());

在您的主要活动中执行此操作

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     activity=this;



}
于 2016-08-16T06:19:30.783 回答