开发者

Creating listView in android using data got by sql server

开发者 https://www.devze.com 2023-03-21 17:59 出处:网络
I have got the data from my local sql to activity ... now I want to create a new activity called listView activity and I want to display a listView using the data I previously got. What is the best me

I have got the data from my local sql to activity ... now I want to create a new activity called listView activity and I want to display a listView using the data I previously got. What is the best method to transfer data between activities and create an adapt开发者_运维问答er for the listview?


So using Intent to send data to the next activity is good but please don't send all the data from your database using Intent. The best way to implement this will be to create some data provider class which won't hold the actual data but can fetch it when it's needed

public class DataProvider {
    public Cursor fetchData() {
        // fetch the data here and return a cursor
    }
}

Than in your activity pass this object with the intent

Intent i; // init it
Bundle bundle = new Bundle();
bundle.putSerializable("dataProvider", new DataProvider());
i.putExtras(bundle);
startActivity(i);

And finally in your started activity get the data provider - i won't write any code cause i believe you can handle this by yourself. When you got the DataProvider just call fetchData() method to fetch the data. It is very IMPORTANT to access db asynchronous or your activity may cause ANR, so the best way to do this will be using AsyncTask. I will also let the implementation of AsyncTask to you.

If you have some troubles feel free to ask and we'll help you.


Create new intent and put data you want. You can put various types of data.

Intent i = new Intent(this, YourClass.class);
i.putExtra(name, value); <--- with this you send data to next activity
startActivity(i);

And what about adapter, if you are not experienced with listviews use ArrayAdapter. Dont forget to use getView() method wisely :)

More: How you get resources in next activity? Well... In onCreate(Bundle savedInstanceState) you do this

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
this.getParent().getIntent().get<DATA-TYPE-YOU-HAVE-SENT>("KEY-NAME");   //this can be anything you sent, like this.getParent().getIntent().getString("myString"); 


this.getParent().getIntent() <---- returns the calling intent, i.e the intent that called this activity...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号