如何在另一个Activity中从ListView传递和检索领域结果?

时间:2019-04-12 09:41:29

标签: android realm

我有一个领域数据库对象,每个对象都有4个属性。在第一个活动中,我放置了一个Listview仅显示其中一个属性,即Event。单击任何显示的事件后,我要转到活动2(详细信息视图),该活动将显示与单击的事件相关联的所有其他3个属性。如何故意这样做,以及如何检索要在活动2中单独显示的所有属性?

public class Events extends RealmObject {

    public String fileRef;
    public String event;
    public String venue;
    public String date;


    @Override
    public String toString() {
        return " \n Events{" +
                "fileRef='" + fileRef + '\'' +
                ", parties='" + event + '\'' +
                ", \n court='" + venue + '\'' +
                ", courtAt='" + date + '\'' +
                '}';
    }

    public String getFileRef() {
        return fileRef;
    }

    public void setFileRef(String fileRef) {
        this.fileRef = fileRef;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getVenue() {
        return venue;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }

    public String getDate() {
        return date;
    }

    public void setCourtAt(String date) {
        this.date = date;
    }


}

下面的代码在第一个活动中。我所得到的只是传递一个属性,即事件。如何传递和检索Activity2中的所有属性?

public class AllEventActivity extends AppCompatActivity {
    private ArrayAdapter adapter;


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


        ListView list = (ListView) findViewById(R.id.listView);




        Realm realm;
        realm = Realm.getDefaultInstance();

        RealmResults<Events> results = realm.where(Events.class).findAll();

        ArrayList<String> rows = new ArrayList<>();
        for (Events events : results)

        {
            rows.add(Events.event);


        }

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, rows);
        list.setAdapter(adapter);


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String selectedEvent = parent.getItemAtPosition(position).toString();

                Intent eventDetailIntent = new Intent(AllEventActivity.this, Activity2.class);
                eventDetailIntent.putExtra("event", selectedEvent);
                startActivity(eventDetailIntent);
            }
        });

    }

下面是Activity2中的代码。我只是设法使烤面包片显示了事件。

public class Activity2 extends AppCompatActivity {


    private String selectedEvent;

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

        selectedEvent = getIntent().getExtras().get("event").toString();
        Toast.makeText(Activity2.this, selectedCase, Toast.LENGTH_LONG).show();

    }
}

非常感谢您的回答。非常感谢。

3 个答案:

答案 0 :(得分:0)

您可以使用Gson库,需要在gradle文件中添加导入才能使用它

使用Gson,您可以将Event转换为String

Gson gson = new Gson();
intent.putExtra("event", gson.toJson(selectedEvent));

您可以在Gson的下一个活动中获得对象

Gson gson = new Gson();
Events yourEvent = gson.fromJson(getIntent().getStringExtra("event"), Events.class);

答案 1 :(得分:0)

使用捆绑包,

public class Activity2 extends AppCompatActivity {


private String selectedEvent;

Bundle bundle;

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


    bundle = getIntent().getExtras();

    if (bundle != null){

        try {
            String selectedEvent = bundle.getString("event");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
}

答案 2 :(得分:0)

请使用以下代码: 我将事件模型的第一到第二活动传递给了

  

模型类

public class Events extends RealmObject implements Serializable {

    public String fileRef;
    public String event;
    public String venue;
    public String date;


    @Override
    public String toString() {
        return " \n Events{" +
                "fileRef='" + fileRef + '\'' +
                ", parties='" + event + '\'' +
                ", \n court='" + venue + '\'' +
                ", courtAt='" + date + '\'' +
                '}';
    }

    public String getFileRef() {
        return fileRef;
    }

    public void setFileRef(String fileRef) {
        this.fileRef = fileRef;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getVenue() {
        return venue;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }

    public String getDate() {
        return date;
    }

    public void setCourtAt(String date) {
        this.date = date;
    }


}
  

Liveview点击事件:

Intent eventDetailIntent = new Intent(AllEventActivity.this, Activity2.class);
        eventDetailIntent.putExtra("event", results.get(position)); // Selected postion item
        startActivity(eventDetailIntent);
  

在第二个活动中,您可以检索传递的模型数据。

 Events eventModel = ( Events) getIntent().getSerializableExtra("event");

因此,您将把数据保存在eventModel对象中。现在您可以从该模型中获取数据并设置到您的TextViews中。

textview1.setText(eventModel.getEvent())
textview2.setText(eventModel.getVenue())
textview3.setText(eventModel.getDate())

希望有帮助。