根据列表视图中选择的项目显示信息的新活动

时间:2015-04-22 12:51:09

标签: android android-fragments android-activity

我正在开发一个从新闻Feed服务器提取信息的应用。现在我有一个来自网站的最新文章列表,其中包含标题,日期,图像和一些信息。这就是我所拥有的:

    public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder> {
    private NewsModel model = NewsModel.getInstance();
    private NewsListFragment fragment;

public NewsListAdapter(NewsListFragment fragment){
    super();
    this.fragment = fragment;
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView title;
    private TextView short_info;
    private TextView date;
    private NetworkImageView image_url;
    private ProgressBar progressBar;
    public ViewHolder(View itemView) {
        super(itemView);

        // Update the details
        title = (TextView) itemView.findViewById(R.id.title);
        short_info = (TextView) itemView.findViewById(R.id.short_info);
        date = (TextView) itemView.findViewById(R.id.date);
        image_url = (NetworkImageView) itemView.findViewById(R.id.image_url);
        // Set default image when the image is loading
        image_url.setDefaultImageResId(R.drawable.download);
        // Set error image if there is an error
        image_url.setErrorImageResId(R.drawable.error);

        // Click listener
        itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                fragment.onItemClicked(getPosition());
            }
        });
    }

    public void setData(News news){
        // Set values
        title.setText(news.getTitleName());
        short_info.setText(news.getShort_info() + "...");
        date.setText("Posted on " + news.getDate());
        image_url.setImageUrl(news.getImage_url(), NewsApp.getInstance().getImageLoader());
    }
}

public NewsListAdapter() {
    super();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    // Create a new view
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_news_item, viewGroup, false);
    // Save the view in the view holder
    ViewHolder vh = new ViewHolder(v);
    // Return the view holder
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
// Get the relevant object
    News news = model.getNewsArrayList().get(i);
    //Populate the view
    viewHolder.setData(news);
}

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

选择项目后,将启动新活动。我遇到的问题是将信息从选择的上一个新闻项加载到新活动中。我知道我需要使用&#34;新闻&#34;我必须使用正确的值填充UI的对象。新活动也将显示标题,日期,信息和图像。我真的很感激任何帮助。

public class NewsDetailsFragment extends Fragment {

public void updateDetails(int id){

    News news = NewsModel.getInstance().getNewsArrayList().get(id);

    // Update the UI with data
}

public NewsDetailsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_news_details, container, false);

    return view;
}


}

2 个答案:

答案 0 :(得分:0)

News实施Parcelable,然后在creating the Fragment时将News对象放入Fragment包中。

答案 1 :(得分:0)

您需要通过News中的BundleIntent对象传递给您的详细活动。要做到这一点,您的News课程必须实施SerializableParcellable。或者你可以以同样的方式传递唯一的身份 Take a look for example here