简体   繁体   English

单击项目时在新活动中打开 webview

[英]open webview in new activity when item is clicked

I'm a beginner in android development.我是 android 开发的初学者。 i have created an app that display data(image and text ) from Firebase in RecyclerView.我创建了一个应用程序,在 RecyclerView 中显示来自 Firebase 的数据(图像和文本)。 everything is working fine, there is also one data (url) and i want when the user will click on any item it will start new activity using Intent and will display this url in a Webview.一切正常,还有一个数据(url),我希望当用户单击任何项目时,它将使用 Intent 开始新的活动,并将在 Webview 中显示此 url。 i have created new activity and set up the WebView in it.我创建了新活动并在其中设置了 WebView。 i just don't know how to pass the url from firebase to this activity.我只是不知道如何将 url 从 firebase 传递给这个活动。 i was searching for 3 days and i did know that i need to use setOnClickItemListener but i didn't find anything realted to my case.我搜索了 3 天,我确实知道我需要使用 setOnClickItemListener,但我没有找到任何与我的案例相关的内容。 that's why i thought about posting it here.这就是为什么我想把它贴在这里。 sorry for my English.对不起我的英语不好。 thank you谢谢你

my Firebase data is like this enter image description here我的 Firebase 数据是这样的在此处输入图像描述

this is model.java这是 model.java

public class model {
    String title, image, description ,url;


    public model() {
        this.url=url ;
        this.title = title;
        this.image = image;
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public String getImage() {
        return image;
    }

    public String getDescription() {
        return description;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

and this is the RecyclerAdapter.java这是 RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    private Context mContext;

    private List<model> modelList;


    public RecyclerAdapter(Context context, List<model> listData) {
        this.modelList = listData;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.title.setText(modelList.get(position).getTitle());
        holder.description.setText((modelList.get(position).getDescription()));
        Picasso.get().load(modelList.get(position).getImage()).into(holder.imageView);

    }


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


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title,description;
        ImageView imageView;
        public ViewHolder(View itemView){
            super(itemView);
            title=itemView.findViewById(R.id.Title);
            description=itemView.findViewById(R.id.Description);
            imageView=itemView.findViewById(R.id.ImageView);
            }
        }
    }

and this is MainActivity.java这是 MainActivity.java

public class MainActivity extends AppCompatActivity {

  private   DatabaseReference reference ;
  private StorageReference mStorageRef ;
  private RecyclerView recyclerView;
  private ArrayList<model> modelList;
  private Context mContext = MainActivity.this;
  private  RecyclerAdapter recyclerAdapter;




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

        LinearLayoutManager layoutManager= new LinearLayoutManager(this );
        recyclerView=findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);


     reference = FirebaseDatabase.getInstance().getReference();
     mStorageRef = FirebaseStorage.getInstance().getReference();

        modelList = new ArrayList<>();

        init();

    }

    private void init(){
        clearAll();

        Query query = reference.child("Data");

        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    model rmodel = new model();
                    rmodel.setImage(snapshot.child("image").getValue().toString());
                    rmodel.setTitle(snapshot.child("title").getValue().toString());
                    rmodel.setUrl(snapshot.child("url").getValue().toString());
                    rmodel.setDescription(snapshot.child("description").getValue().toString());
                    modelList.add(rmodel);

                }

              recyclerAdapter = new RecyclerAdapter(mContext , modelList);
                recyclerView.setAdapter(recyclerAdapter);
               recyclerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

            }


    private void clearAll(){

        if (modelList !=null){
            modelList.clear();
            if(recyclerAdapter != null)
                recyclerAdapter.notifyDataSetChanged();
        }
        modelList = new ArrayList<>();
    }
    } 

In the onBindViewHolder method:onBindViewHolder方法中:

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.title.setText(modelList.get(position).getTitle());
    holder.description.setText((modelList.get(position).getDescription()));
    Picasso.get().load(modelList.get(position).getImage()).into(holder.imageView);
    holder.addOnClickListener(new OnClickListener() {
        @override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(modelList.get(position).getUrl()));
            startActivity(browserIntent);
        }
    });
}

This will add an action listener on the holder to open the URL in a browser.这将在持有人上添加一个动作侦听器,以在浏览器中打开 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为从Webview单击的每个列表项打开一个新活动 - Open a new activity for each list item clicked from webview 单击 ListView 项时打开有关新活动的详细信息 - Open detail on new activity when ListView item is clicked 在Firebase RecyclerView中单击项目时如何打开新活动 - How to open a new activity when an item is clicked in Firebase RecyclerView 使“抽屉导航菜单”项单击时打开新活动 - Make Drawer Navigation Menu Item Open new activity when clicked 如何在传递单击的自定义列表项的数据时单击带有RecyclerView的片段项时打开新活动? - How to open a new activity when an item of a fragment with RecyclerView is clicked while passing the data of the clicked custom list item? 单击列表视图项时是否打开活动? - Open activity when listview Item is clicked? 单击按钮时如何打开新活动? - How to open a new activity when a button is clicked? 单击搜索时为搜索视图打开新活动 - Open new activity for searchview when search is clicked 单击recyclerView Item时如何将数据获取到webView活动中 - how to get data into webView activity when recyclerView Item is clicked 如何根据RecyclerView中的单击项在新活动中打开特定片段 - How to open specific Fragment in new Activity depending on clicked item in RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM