0

I made one NewsAdapter with image and two texts.When u click on image it ll lead you to new activity.Now i want to pass image and text's to new activity.i know with putExtra method you can pass data but i don't know how can i use it here.

this is my News_Fragment

    public class News_Fragment extends Fragment {

    private List<News> newsList = new ArrayList<>();
    private RecyclerView recyclerView;
    private NewsAdapter newAdapter;
    BottomBar bottombar;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_news_, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.News_recycler_view);

        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setAdapter(newAdapter);
        newAdapter = new NewsAdapter(newsList,getContext());

        // Inflate the layout for this fragment
        JSONObject jsonObject = new JSONObject();

        new NewsTask() {
            @Override
            protected void onPostExecute(APIResponse apiResponse) {
                if(apiResponse != null && apiResponse.data != null & apiResponse.data.size()>0) {
                    newsList = apiResponse.data;
                    //apiResponse.data.toArray();
                    //newAdapter.notifyDataSetChanged();

                    newAdapter = new NewsAdapter(newsList,getContext());
                    recyclerView.setAdapter(newAdapter);
                }
                Log.i("resp", "onPostExecute");
            }
        }.execute(jsonObject);
        return rootView;
    }
}

this is NewsAdapter

    public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {

    private List<News> newsList;
    private Context context;


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public ImageView image,nextArrowimage;
        public TextView desc;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.News_title);
            image = (ImageView) view.findViewById(R.id.News_imageView);
            nextArrowimage = (ImageView)view.findViewById(R.id.news_NextArrow);
            desc = (TextView) view.findViewById(R.id.News_desc);
        }
    }


    public NewsAdapter(List<News> newsList,Context context) {
        this.newsList = newsList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list_row, parent, false);
        MyViewHolder holder = new MyViewHolder(itemView);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final News news = newsList.get(position);
        holder.title.setText(news.getTitle());
        //holder.image.setImageResource(Integer.parseInt(news.getImage()));
        context = holder.image.getContext();
        holder.nextArrowimage.setImageResource(R.drawable.nextbutton);

        //Picasso.with(context).load("https://www.simplifiedcoding.net/wp-content/uploads/2015/10/advertise.png").resize(100,100).into(holder.image);

        Picasso.with(context).load("http://bitstobyte.in/upload/"+news.getImage()).placeholder(R.drawable.ic_favorite_white_24dp).error(R.drawable.ic_map_24dp).resize(100,100).into(holder.image);
        //Picasso.with(context).load("http://bitstobyte.in/upload/"+ news.getImage()).resize(100,100).into(holder.image);

        holder.desc.setText(news.getDesc().trim());


        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView  title = (TextView)v.findViewById(R.id.News_title);
                TextView desc = (TextView)v.findViewById(R.id.News_desc);

                String str3 = "http://bitstobyte.in/api/news/"+getItemId(position)+news.getImage();

                Intent intent = new Intent(context.getApplicationContext(), News_Activity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(intent);
                intent.putExtra("title",str3);


                Toast.makeText(context, "You Clicked " + newsList.get(position), Toast.LENGTH_LONG).show();

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

}

this is my News_Activity

    public class News_Activity extends AppCompatActivity {
    TextView textView1,
             textView2;
    ImageView imageView;

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

        getSupportActionBar().setTitle(Html.fromHtml("<font color='#fdfafa'> News View </font>"));

        final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        //upArrow.setColorFilter(getResources().getColor(R.color.ActionBarText), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(upArrow);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        String Title1 = getIntent().getStringExtra("title");

        textView1 = (TextView)findViewById(R.id.activity_news_Title);
        textView1.setText(Title1);

        textView2 = (TextView)findViewById(R.id.activity_news_desc);
        textView2.setText(Title1);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • You should first setup getter and setter methods in Adapter class and then use interface to pass data from fragment to another class. – Mohammed Atif Jul 06 '16 at 08:10

4 Answers4

0
Intent intent = new Intent(context, News_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("title",str3);
context.startActivity(intent);

or

Intent intent = new Intent(getActivity(), News_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("title",str3);
getActivity().startActivity(intent);

Did you try putting the data into intent before actually starting it?? And use getActivity() or directly context instead of context.getApplicationContext()

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • BDW, you dont need to make a call to `'getApplicationContext()` just try with `context` – Mohammed Atif Jul 06 '16 at 08:22
  • or a getActivity() will also work. But his code is fine as well. – Kushan Jul 06 '16 at 08:26
  • 1
    @Kushan http://stackoverflow.com/a/4128799/5471104, this might help to know the difference. – Mohammed Atif Jul 06 '16 at 08:29
  • thanks man, that was informative :) well i have been using getActivity() for the same reason. had no idea getApplicationContext() was troublesome though – Kushan Jul 06 '16 at 08:31
  • ok thank you so much frnds....i got title , description but not gettting image ?? – Srikanth Saini Jul 06 '16 at 08:42
  • this is my adapter code : String title = news.getTitle(); String desr = news.getDesc(); String image = "http://bitstobyte.in/upload/"+news.getImage(); Intent intent = new Intent(context, News_Activity.class); intent.putExtra("title", title); intent.putExtra("desc",desr); intent.putExtra("imageUrl",image); context.startActivity(intent); – Srikanth Saini Jul 06 '16 at 08:47
0

From the adapter you can call this way ->

Intent intent = new Intent(context,News_Activity.class);
intent.putExtra("image",IMAGE_URL);
intent.putExtra("data", DATA_TO_PASS);
context.startActivity(intent); 
Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
  • i didnot get title but in textView replace the title with www.bitstobyte.in/upload/55345wedsd8937411.jpg – Srikanth Saini Jul 06 '16 at 08:35
  • do you know how to pass data from one activity to another activity ? – Zahidul Islam Jul 06 '16 at 08:45
  • thank u...i got title and description but not getting image then how to get it – Srikanth Saini Jul 06 '16 at 08:48
  • you can't pass the full image , just pass the url(`"http://bitstobyte.in/upload/"+news.getImage()`) which you set in `Picasso` . and from the News_activity you have to set the image same as you set in your adapter – Zahidul Islam Jul 06 '16 at 08:50
  • see this once in adapter class code : String title = news.getTitle(); String desr = news.getDesc(); String image = "http://bitstobyte.in/upload/"+news.getImage(); Intent intent = new Intent(context, News_Activity.class); intent.putExtra("title", title); intent.putExtra("desc",desr); intent.putExtra("imageUrl",image); context.startActivity(intent); – Srikanth Saini Jul 06 '16 at 08:52
  • yup, this is it . now you can get the data in your News_activity – Zahidul Islam Jul 06 '16 at 08:53
  • this is News_Activity code : String Title1 = intent.getStringExtra("title"); String Desc = intent.getStringExtra("desc"); String Image = intent.getStringExtra("imageUrl"); textView1 = (TextView) findViewById(R.id.activity_news_Title); textView1.setText(Title1); textView2 = (TextView) findViewById(R.id.activity_news_desc); textView2.setText(Desc); imageView = (ImageView)findViewById(R.id.activity_news_ImageView); imageView.setImageResource(); – Srikanth Saini Jul 06 '16 at 08:54
  • in News_Activity how to set ImageView – Srikanth Saini Jul 06 '16 at 08:54
  • `Picasso.with(context).load(Image).into(imageView);` – Zahidul Islam Jul 06 '16 at 08:55
0

You need to putExtras into the intent before starting the activity to pass the values :)

The issue is the Intent flag FLAG_ACTIVITY_NEW_TASK. This opens the activity in a new task which is probably why the data isn't passed properly.

for similar issue see:

Intent from notification does not have extras

Do the following:

    holder.image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        TextView  title = (TextView)v.findViewById(R.id.News_title);
        TextView desc = (TextView)v.findViewById(R.id.News_desc);

        String str3 = "http://bitstobyte.in/api/news/"+getItemId(position)+news.getImage();

        Intent intent = new Intent(context.getApplicationContext(), News_Activity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("title",str3);
        context.getApplicationContext().startActivity(intent);



        Toast.makeText(context, "You Clicked " + newsList.get(position), Toast.LENGTH_LONG).show();

    }
   });
Community
  • 1
  • 1
Kushan
  • 5,855
  • 3
  • 31
  • 45
0

If you want to pass image , you can pass it by converting to bitmap and use it.

try{
String str3 = "http://bitstobyte.in/api/news/"+getItemId(position)+news.getImage();
URL url = new URL(str3);

Bitmap imagebitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                Intent intent = new Intent(context.getApplicationContext(), News_Activity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(intent);
                intent.putExtra("title",str3);
        intent.putExtra("imagebitmap", imagebitmap);
        context.startActivity(intent);

    } catch (Exception e) {
            Log.error("Exception", e);
       }

For getting data..

Intent intent = getIntent();
String title = getIntent().getExtras().getString("title"); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("imagebitmap");

Thanks..

Kush
  • 1,080
  • 11
  • 15