将所选对象传递给另一个活动

时间:2017-05-02 08:41:00

标签: java android android-intent android-activity mobile

我有一个新闻活动,其中有一个新闻列表。我希望用户从列表中选择一个新闻并将他引导到news_details页面,在那里我提供有关所选新闻的详细信息,但是当用户选择时这个消息,节目很快转到news_details并再次回到新闻中。

  News:

  public void Listen() {

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {  // ana sayfada herhangi bir item seçildiğinde
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
            Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
            i.putExtra("title", selectedNews.getTitle());
            i.putExtra("date", selectedNews.getNewsDate().toString());
            i.putExtra("image_id", selectedNews.getImageId());
            i.putExtra("text", selectedNews.getText());
            setResult(RESULT_OK, i); 
            startActivity(i);




        }

    });
}


  News_Details:

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

    Intent i = new Intent(this,MainActivity.class);

    startActivityForResult(i, GET_NEWS);

}


@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GET_NEWS) {  // Check which request we're responding to

        if (resultCode == RESULT_OK) {  // Make sure the request was successful

            title.setText(data.getStringExtra("title"));
            date.setText(data.getStringExtra("date"));
            news_img.setImageResource(data.getIntExtra("image_id", 0));
            news_text.setText(data.getStringExtra("text"));


        }

    }
}
}

5 个答案:

答案 0 :(得分:1)

首先,删除此行:

setResult(RESULT_OK, i); 

另外,从newsdetails活动中删除此行:

startActivityForResult(i,GET_NEWS);

进行如下更改:

public void Listen() {

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
            Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
            i.putExtra("title", selectedNews.getTitle());
            i.putExtra("date", selectedNews.getNewsDate().toString());
            i.putExtra("image_id", selectedNews.getImageId());
            i.putExtra("text", selectedNews.getText());
            startActivity(i);
        }

    });
}

然后,在您的新闻详情活动中:

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

    Intent i = getIntent();
    String title = i.getStringExtra("title");
    String date = i.getStringExtra("date");
    int imageId = i.getIntExtra("image_id");
    String text = i.getStringExtra("text");
}

不需要OnActivityResult方法,只需将其删除即可。

<强>供参考:

startActivitystartActivityForResult他们都开始了新的活动,但startActivityForResult顾名思义表示您期望从您开始的活动中获得结果。这个结果应该用onActivityResult方法获得。

例如,假设您要从Activity1启动Activity2,并且希望在完成Activity2时将一些数据传递回Activity1。您只需使用setResult()方法在Activity2中设置Result。当Activity1再次恢复时,将调用其onActivityResult(),您将覆盖Activity1中的onActivityResult()以接收Activity2设置的结果。

希望你现在明白这一点。

答案 1 :(得分:1)

Remove these lines 
setResult(RESULT_OK, i); 
Intent i = new Intent(this,MainActivity.class);
startActivityForResult(i, GET_NEWS);

使用google Gson序列化对象。

    NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
    String strNews = new Gson().toJson(selectedNews);

    Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
                i.putExtra("news", strNews);
                startActivity(i);

另一方面新闻详情onCreate()执行此操作

    Bundle bundle = getIntent().getExtras();
    String newsStr = bundle.getString("news");
            Gson gson = new Gson();
            Type type = new TypeToken<NewsItem>() {
            }.getType();
            NewsItem selectedNews = gson.fromJson(newsStr, type);

答案 2 :(得分:0)

发送字符串值

  NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
        Intent i=new Intent(MainActivity.this, News_Details_Activity.class);
        i.putExtra("title", selectedNews.getTitle());
        i.putExtra("date", selectedNews.getNewsDate().toString());
        i.putExtra("image_id", selectedNews.getImageId());
        i.putExtra("text", selectedNews.getText());
        startActivity(i);

收到News_Details_Activity

    Intent i = getIntent();
    title = i.getStringExtra("title");
    date= i.getStringExtra("date");
    text= i.getStringExtra("text");

答案 3 :(得分:0)

你可以通过以下方式使用Serializable来实现

公共类新闻实现Serializable {

String title;
String desc;
String time,imageUrl;

}

然后新闻列表活动

Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
        i.putExtra("news",newsObject);

并获取关于NewsDetail的创建

    News news=(News)  getIntent().getExtras().getSerializable("news");
and user it like  title.setText(news.getTitle());

答案 4 :(得分:0)

制作实施Serialazable的新闻类

创建新的新闻对象类,并将其置于putSerialazable();

在你的第二个活动中只需getIntent()。getSerialazable(“key”)并将你的数据设置为视图。