将选定的对象传递到另一个活动



我有一个新闻活动,其中有新闻列表。我希望用户从列表中选择新闻,并将他引导到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"));

        }
    }
}
}

首先,删除此行:

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方法,因此简单地将其删除。

fyi:

startActivitystartActivityForResult都开始了新的活动,但是startActivityForResult的名字表明您期望您正在启动的活动会产生结果。该结果应在onActivityResult方法中获得。

说例如,您想从Activity1启动Activity2,并且要在完成Activity2时将一些数据传递回活动1。您只需使用setResult((方法在Activity2中设置结果。虽然Activity1再次恢复,但将调用其onActivityResult(),您将覆盖Activity1中的onActivityResult()以接收Activity 2。

的结果。

希望您现在对此清楚。

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

另一方面,新闻详细信息((执行此操作

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

发送字符串值

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

您可以使用以下方式使用序列化

公共课程新闻实现序列化{

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

举行实现序列化的新闻类

创建新闻类的新对象,并以putserialazable((;

为单位。

在您的第二个活动中,Just getIntent((。geterialazable("键"(并将您的数据设置为视图。

相关内容

  • 没有找到相关文章

最新更新