samedi 23 janvier 2016

Dynamic TableLayout with TextView is empty even with values in ArrayList

I'm trying to create a dynamic TableLayout with data from database. I store the data in an ArrayList and use it to setText for the TextView. I used debug to check and the ArrayList had the correct number of values as in the database. It's not empty. But still the screen is blank. I tried to setTextColor for the TextView but it still was empty. The first 5 TextViews are for the heading. Later I use for loop for setting the text. I'm trying to do this in a Fragment btw.

Here's the code

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    sessionManager = new SessionManager(getActivity());
    databaseHelper = new DatabaseHelper(getActivity());
    bookingHistoryList = new ArrayList();
    view =  inflater.inflate(R.layout.fragment_preferences, container, false);
    tableLayout = (TableLayout)view.findViewById(R.id.hTableLayout);
    TableRow pickDate = new TableRow(getActivity());
    TextView pickDateTxt = new TextView(getActivity());
    pickDateTxt.setText("Pick up Date");
    pickDate.addView(pickDateTxt);
    TableRow pickTime = new TableRow(getActivity());
    TextView pickTimeTxt = new TextView(getActivity());
    pickTimeTxt.setText("Pick up Time");
    pickTime.addView(pickTimeTxt);
    TableRow pickUpLocation = new TableRow(getActivity());
    TextView pickUpLocationTxt = new TextView(getActivity());
    pickUpLocationTxt.setText("Pick up Location");
    pickUpLocation.addView(pickUpLocationTxt);
    TableRow destination = new TableRow(getActivity());
    TextView destinationTxt = new TextView(getActivity());
    destinationTxt.setText("Destination");
    destination.addView(destinationTxt);
    TableRow vehicleType = new TableRow(getActivity());
    TextView vehicleTypeTxt = new TextView(getActivity());
    vehicleTypeTxt.setText("Vehicle Type");
    vehicleType.addView(vehicleTypeTxt);
   // databaseHelper.delete();
    String getUserEmail = sessionManager.getUserEmail();
    bookingHistoryList = databaseHelper.getBookingHistory(sessionManager.getUserEmail());
    rowCount = DatabaseUtils.queryNumEntries(databaseHelper.getReadableDatabase(),"bookingDetails");
    n = (int)rowCount*5;
    for(int i = 0;i<n;i++)
    {
        TableRow tableRow = new TableRow(getActivity());
        TextView textView = new TextView(getActivity());
        textView.setText(bookingHistoryList.get(i).toString());
        textView.setTextColor(Color.BLACK);
        tableRow.addView(textView);

        TableRow tableRow1 = new TableRow(getActivity());
        TextView textView1 = new TextView(getActivity());
        textView1.setText(bookingHistoryList.get(i+1).toString());
        tableRow1.addView(textView1);

        TableRow tableRow2 = new TableRow(getActivity());
        TextView textView2 = new TextView(getActivity());
        textView2.setText(bookingHistoryList.get(i+2).toString());
        tableRow2.addView(textView2);

        TableRow tableRow3 = new TableRow(getActivity());
        TextView textView3 = new TextView(getActivity());
        textView2.setText(bookingHistoryList.get(i+3).toString());
        tableRow2.addView(textView3);

        TableRow tableRow4 = new TableRow(getActivity());
        TextView textView4 = new TextView(getActivity());
        textView2.setText(bookingHistoryList.get(i+4).toString());
        tableRow2.addView(textView4);
        i+=4;
    }

     return view;
}

Here's the code to get from database. It's return value is not empty.

    public ArrayList getBookingHistory(String email)
{
    ArrayList bookingHistoryList = new ArrayList();
    String e = email;
    Cursor c = db.rawQuery("select * from bookingDetails where email = '"+email +"'",null);
    int cou = c.getCount();
    c.moveToFirst();
    do
    {
        bookingHistoryList.add(c.getString(c.getColumnIndex("date")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("time")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("fromLocation")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("destination")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("vehicle")));
    }
    while (c.moveToNext());
    return bookingHistoryList;
}

Aucun commentaire:

Enregistrer un commentaire