0
public void Showdata(View view)
    {
        Cursor c = db.rawQuery("SELECT * FROM Student2", null);
        int count = c.getCount();
        c.moveToFirst();
        TableLayout tablelayout = new TableLayout(getApplicationContext());
        tablelayout.setVerticalScrollBarEnabled(true);
        TableRow tablerow;
        TextView textview, textview1, textview3, textview5;
        tablerow = new TableRow(getApplicationContext());

        textview = new TextView(getApplicationContext());
        textview.setText("Name");
        textview.setTextColor(Color.RED);
        textview.setTypeface(null, Typeface.BOLD);
        textview.setPadding(20, 20, 20, 20);
        tablerow.addView(textview);


        textview5 = new TextView(getApplicationContext());
        textview5.setText("Email");
        textview5.setTextColor(Color.RED);
        textview5.setTypeface(null, Typeface.BOLD);
        textview5.setPadding(20, 20, 20, 20);
        tablerow.addView(textview5);
        tablelayout.addView(tablerow);

        for(Integer j=0; j< count; j++)
        {
            tablerow = new TableRow(getApplicationContext());

            textview1 = new TextView(getApplicationContext());
            textview1.setText(c.getString(c.getColumnIndex(name)));


            textview3 = new TextView(getApplicationContext());
            textview3.setText(c.getString(c.getColumnIndex(email)));

            textview1.setPadding(20, 20, 20, 20);
            textview3.setPadding(20, 20, 20, 20);

            tablerow.addView(textview1);
            tablerow.addView(textview3);

            tablelayout.addView(tablerow);
            c.moveToNext();
        }
        setContentView(tablelayout);
        db.close();
    }
Ankush soni
  • 1,439
  • 1
  • 15
  • 30
Pushpendra
  • 1,492
  • 1
  • 17
  • 33

1 Answers1

0

I've tested your code with some dummy students, it is working fine for me.

Please verify that you are getting students by iterating the cursor. Put some Log.d("tag","some_student") for testing

Also, You should use YourActivityName.this in place of getApplicationContext(). For example

tablerow = new TableRow(this);

Full Code using the dummy students

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Student> students = new ArrayList<>();
        students.add(new Student("Rahul","rahul@gmail.com"));
        students.add(new Student("Shweta","shweta@gmail.com"));
        students.add(new Student("Test","test@gmail.com"));
        students.add(new Student("Mohan","mohan@gmail.com"));


        TableLayout tablelayout = new TableLayout(this);
        tablelayout.setVerticalScrollBarEnabled(true);
        TableRow tablerow;
        TextView textview, textview1, textview3, textview5;
        tablerow = new TableRow(this);

        textview = new TextView(this);
        textview.setText("Name");
        textview.setTextColor(Color.RED);
        textview.setTypeface(null, Typeface.BOLD);
        textview.setPadding(20, 20, 20, 20);
        tablerow.addView(textview);


        textview5 = new TextView(this);
        textview5.setText("Email");
        textview5.setTextColor(Color.RED);
        textview5.setTypeface(null, Typeface.BOLD);
        textview5.setPadding(20, 20, 20, 20);
        tablerow.addView(textview5);
        tablelayout.addView(tablerow);

        for(Integer j=0; j< students.size(); j++)
        {
            tablerow = new TableRow(this);

            textview1 = new TextView(this);
            textview1.setText(students.get(j).getName());


            textview3 = new TextView(this);
            textview3.setText(students.get(j).getEmail());

            textview1.setPadding(20, 20, 20, 20);
            textview3.setPadding(20, 20, 20, 20);

            tablerow.addView(textview1);
            tablerow.addView(textview3);

            tablelayout.addView(tablerow);
        }
        setContentView(tablelayout);

    }

    class Student {
        String name;
        String email;

        public Student(String name, String email) {
            this.name = name;
            this.email = email;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }
}
Rahul Chaurasia
  • 1,601
  • 2
  • 18
  • 37
  • Hey Dear Did you try that on real Device or AVD Manager, actually when i'm trying that on my AVD manager then it's not working, the message is "Application name Unfortunately has stop working", now what should i do for that and this problem remain same with some another application – Pushpendra Oct 01 '15 at 06:01
  • Above code will work on both emulator or real device. If you are getting message saying "Unfortunately app has stopped", it means your app is crashing due to some exception. Check your logs using logcat in eclipse and you'll get to know exception. You can paste your stack trace so that I can help you. – Rahul Chaurasia Oct 01 '15 at 07:01
  • FATAL EXCEPTION: main Could not execute method for android:onClick – Pushpendra Oct 01 '15 at 09:54
  • Paste your full activity code and stack trace. The information that you have given is not sufficient to solve your problem. – Rahul Chaurasia Oct 01 '15 at 10:28
  • Failed to read row 0, column -1 from a CursorWindow which has 6 rows, 3 columns.Shutting down VM beginning of crash FATAL EXCEPTION: main Process: com.simplesqlite, PID: 2216 java.lang.IllegalStateException: Could not execute method for android:onClick at android.view.View$DeclaredOnClickListener.onClick(View.java:4452) at android.view.View.performClick(View.java:5198) – Pushpendra Oct 03 '15 at 04:44
  • at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) – Pushpendra Oct 03 '15 at 04:45
  • at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 9 more Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetString(Native Method) at android.database.CursorWindow.getString(CursorWindow.java:438) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)at com.simplesqlite.MainActivity.Showdata(MainActivity.java:82) 11 more – Pushpendra Oct 03 '15 at 04:45
  • Seems like you have problem with the cursor. Read this [link](http://stackoverflow.com/questions/23178441/error-make-sure-the-cursor-is-initialized-correctly-before-accessing-data-from) – Rahul Chaurasia Oct 05 '15 at 05:47
  • okay i'm trying new example if the problem is remain same then i'll tell you, – Pushpendra Oct 06 '15 at 07:59