简体   繁体   中英

How to display data fetched from the database table into listview?

My Function In SQLAdapter class is

public ArrayList<Airline> getairlinedetails(String bookingdate) {

    Cursor curCalllog =db.rawQuery("SELECT * FROM "+ BOOK + 
            " WHERE " + date + 
            " BETWEEN '" + startdate + "' AND '" + enddate + "'", null);

    if (curCalllog != null) {
        if (curCalllog.moveToFirst()) {
            do {
                a=new Airline();
                //a.setBookingdate(curCalllog.getString(1));
                a.setPickupadd(curCalllog.getString(2));
                a.setCity(curCalllog.getString(3));
                a.setTrip(curCalllog.getString(4));
                a.setFdate(curCalllog.getString(5));
                a.setFtime(curCalllog.getString(6));
                a.setCdate(curCalllog.getString(7));
                a.setPtime(curCalllog.getString(8));
                a.setSeats(curCalllog.getInt(9));
                a.setAmount(curCalllog.getInt(10));



                update.add(a);
            } while (curCalllog.moveToNext());
        }
    }

    return update;
}

M Fetching data between two dates and I Want To show the fetched data into listview please help me how to do it I m new in android development.

You can use SimpleCursorAdapter for showing Databse contents in Listview. Make instance of SimpleCursorAdapter and pass Cursor object into it. Refer this link

If you want Customized Listview, you can customize SimpleCursorAdapter by extending this with your custom adapter class.

I assume you are getting data from database and there by complete ArrayList with Airline objects.

Now to display data in ListView from ArrayList<Airline> , you have to define Custom adapter class by extending either BaseAdapter or ArrayAdapter .

Here you go: How to define custom adapter for ListView?

I Want To show the fetched data into listview please help me how to do it I m new in android development

Simpliest way is to use ArrayAdapter with build-in ListView's row layout.

ListView list = (ListView) findViewById(R.id.yourList);
ArrayList<Airline> data = db.getairlinedetails("someString");
ArrayAdapter<Airline> adapter = new ArrayAdapter<Airline>(this, 
                                    android.R.layout.simple_list_item_1, data);

list.setAdapter(adapter);

But since this you need to also override toString() method in your Airline class.

Reason is that your ArrayAdapter will convert each child(provided list of airlines) to String and if you won't override toString() you will get default string representation of object but you probably need to show for instance name of airline so your method can looks like

@Override
public String toString() {
   return this.name;
}

Note:

This is simple way. But if you want to get more control over ListView and create custom list i recommend you to create own subclass of ListAdapter for example BaseAdapter and define your own Adapter. Sorry but i won't write you implementation because it requires much more code but nice examples you can find here:

you can follow this example :

DataManipulator.java -helper class

 //to retrieve data in a list
  public List<String[]> selectAll()
{

    List<String[]> list = new ArrayList<String[]>();
    Cursor cursor = db.query(TABLE_NAME, new String[] { "id","name","number","skypeId","address" },
            null, null, null, null, "name asc"); 

    int x=0;
    if (cursor.moveToFirst()) {
        do {
            String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};

            list.add(b1);

            x=x+1;
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    } 
    cursor.close();

    return list;
}

CheckData.java // to show data in a list view

      public class CheckData extends ListActivity  {     
TextView selection;
public int idToModify; 
DataManipulator dm;

List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check);
      dm = new DataManipulator(this);
      names2 = dm.selectAll();

    stg1=new String[names2.size()]; 

    int x=0;
    String stg;

    for (String[] name : names2) {
        stg = name[1]+" - "+name[2]+ " - "+name[3]+" - "+name[4];

        stg1[x]=stg;
        x++;
    }


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(   
            this,android.R.layout.simple_list_item_1,   
            stg1);
    this.setListAdapter(adapter);
    selection=(TextView)findViewById(R.id.selection);

}      

public void onListItemClick(ListView parent, View v, int position, long id) {
    selection.setText(stg1[position]);
}


}

follow this link for complete tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM