0

I want to get link data in OnClickListener so I have created array Model with an object and store all data which is dynamic.

now I want to extract data so I have use array object to get data. like I want to link data with string format and I use String urls=object.link; and when I generate toast it shows me Null.

So please Tell me How can I get link data into String Format so I can pass it on Uri.parse(urls));

File Code:

package com.ejobbox.ejobbox;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter{
    private ArrayList<Model> dataset;
    private Context mContext;

    public RecyclerViewAdapter(ArrayList<Model> mlist, Context context) {
      this.dataset=mlist;
      this.mContext=context;
    }

    public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
        TextView title,subtitle,link,date;
        ImageView imageView;

        public ImageTypeViewHolder(View itemView){
            super(itemView);
            this.title=(TextView)itemView.findViewById(R.id.title);
            //this.link=(TextView)itemView.findViewById(R.id.link);
            this.subtitle=(TextView) itemView.findViewById(R.id.subtitle);
            this.imageView=(ImageView) itemView.findViewById(R.id.icon);
            this.date=(TextView)itemView.findViewById(R.id.date);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.postdetails,parent,false);
        return new ImageTypeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final Model object=dataset.get(position);


        ((ImageTypeViewHolder) holder).title.setText(object.title);
        ((ImageTypeViewHolder) holder).subtitle.setText(object.subtitle);
        //((ImageTypeViewHolder) holder).link.setText(object.link);
        ((ImageTypeViewHolder) holder).date.setText((CharSequence) object.date);

        ((ImageTypeViewHolder) holder).title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String urls=object.link;
                Toast.makeText(mContext,urls, Toast.LENGTH_SHORT).show();

                Intent browserIntent = new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(urls));
                mContext.startActivity(browserIntent);

            }
        });

    }

    @Override
    public int getItemCount() { return dataset.size();}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ejobbox
  • 103
  • 1
  • 1
  • 9

2 Answers2

0

Use the getText() method of the link object of EditText.

Do this:

String urls = ((ImageTypeViewHolder) holder).link.getText().toString();

But before this, remove the lines you have commented out.

Pulak
  • 768
  • 7
  • 16
  • No it show error "Can not find Symbol method getText() " – ejobbox Jan 14 '18 at 07:29
  • Have you removed the two commented lines? Since `link` is an `EditText` object, it must be having a `getText()` method – Pulak Jan 14 '18 at 07:32
  • @ejobbox, see [this](https://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field) question – Pulak Jan 14 '18 at 07:37
  • Yes i have removed it also show this: ((ImageTypeViewHolder) holder).title.setText(object.title); ((ImageTypeViewHolder) holder).subtitle.setText(object.subtitle); ((ImageTypeViewHolder) holder).date.setText((CharSequence) object.date); ((ImageTypeViewHolder) holder).title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String urls = object.link.getText().toString(); Toast.makeText(mContext,urls, Toast.LENGTH_SHORT).show(); – ejobbox Jan 14 '18 at 07:38
  • No it show me 'Void android.widget.TextView.getText()' on null obeject reference and crash application. – ejobbox Jan 14 '18 at 08:08
  • Make sure you have initialized the holder items properly – Pulak Jan 14 '18 at 08:28
  • If `NullPointerException` is happening, it should happen where you set the text for `link` and not where you mention since the line where the text is set runs before `getText()` line. Still, make sure `link` isn't null. It shouldn't be if the correct id is referenced and that id is indeed in the layout mentioned – Pulak Jan 14 '18 at 17:15
0
        ((ImageTypeViewHolder) holder).title.setText(object.title);

I don't know why u casting here use holder directly so instead of the above line use this

          holder.title.setText(object.title)

this will work perfectly and if it didn't` work plz let me know

moahmed ayed
  • 636
  • 7
  • 10
  • when i put holder.title.setText(object.title) it Automatically convert in to: ((ImageTypeViewHolder) holder).title.setText(object.title); – ejobbox Jan 14 '18 at 07:41
  • final Model object=dataset.get(position); could u plz log this in the log cat like this Log.i("tag", "Model: "+object); – moahmed ayed Jan 14 '18 at 23:55