从NewsAdapter类将数据传递给News_Activity

时间:2016-07-06 08:09:10

标签: android json fragment android-adapter

我制作了一个带有图像和两个文本的NewsAdapter。当你点击图像时,它会引导你进入新的活动。现在我想将图像和文本传递给新的活动。我知道用putExtra方法你可以通过数据,但我不知道如何在这里使用它。

这是我的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;
    }
}

这是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();
    }

}

这是我的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);
    }
}

4 个答案:

答案 0 :(得分:0)

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

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

您是否尝试在实际启动之前将数据置于意图中?并使用getActivity()或直接context代替context.getApplicationContext()

答案 1 :(得分:0)

您可以通过这种方式调用适配器 - &gt;

Intent intent = new Intent(context,News_Activity.class);
intent.putExtra("image",IMAGE_URL);
intent.putExtra("data", DATA_TO_PASS);
context.startActivity(intent); 

答案 2 :(得分:0)

在开始活动以传递值之前,您需要将Egxt放入intent中。

问题是Intent标志FLAG_ACTIVITY_NEW_TASK。这将在新任务中打开活动,这可能是数据未正确传递的原因。

对于类似问题,请参阅:

Intent from notification does not have extras

执行以下操作:

    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();

    }
   });

答案 3 :(得分:0)

如果要传递图像,可以通过转换为位图并使用它来传递它。

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);
       }

获取数据..

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

谢谢..