Search code examples
androidlistviewandroid-asynctaskcustom-lists

Data is not displaying in custom ListView


I have created a custom listview which gets data from the database. With the help of toast i am able to see that all values are coming properly but these values are not shown in Listview. ListViewAdapter class is working properly. Here is my mainActivity code

    public class FamilyInfo extends AppCompatActivity {

    ProgressDialog pDialog;
    ListView lv_fam_memb;
    ArrayList<String>namelist,agelist,edulist,rellist;
    ListViewAdapter listViewAdapter;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_family_info);
        Bundle bundle=getIntent().getExtras();
        id=bundle.getString("headid");
        namelist=new ArrayList<>();
        agelist=new ArrayList<>();
        edulist=new ArrayList<>();
        rellist=new ArrayList<>();
        lv_fam_memb=findViewById(R.id.lv_fam_member);
        new GetMydata().execute();
        listViewAdapter=new ListViewAdapter(FamilyInfo.this,namelist,agelist,edulist,rellist);
        lv_fam_memb.setAdapter(listViewAdapter);

    } public class GetMydata extends AsyncTask{
       MyConnection myConnection;
       Connection connection;
       Statement statement;
       ResultSet resultSet;

       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           pDialog = new ProgressDialog(FamilyInfo.this);
           pDialog.setMessage("Please wait...");
           pDialog.setIndeterminate(false);
           pDialog.setCancelable(false);
           pDialog.show();
       }

       @Override
       protected Object doInBackground(Object[] objects) {
           myConnection=new MyConnection();
           connection=myConnection.DatabaseConncetion1();
           try {
               statement=connection.createStatement();
               resultSet=statement.executeQuery("select MEM_NAME,MEM_AGE,MEM_EDU,MEM_REl from FAMILY_NO where HEAD_ID="+id);

           } catch (SQLException e) {
               e.printStackTrace();
           }
           return null;
       }

       @Override
       protected void onPostExecute(Object o) {
           super.onPostExecute(o);
           try {
               while (resultSet.next())
               {
                   namelist.add(resultSet.getString(1));
                   agelist.add(resultSet.getString(2));
                   edulist.add(resultSet.getString(3));
                   rellist.add(resultSet.getString(4));
                  }
               pDialog.dismiss();



           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
   }

}

Please tell me what is missing, thank you :)


Solution

  • You have called setAdapter() in onCreate() with empty List. . Now when you load data you need to call notifydatasetChanged() in Adapter . See the code below.

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        try {
            while (resultSet.next())
            {
                namelist.add(resultSet.getString(1));
                agelist.add(resultSet.getString(2));
                edulist.add(resultSet.getString(3));
                rellist.add(resultSet.getString(4));
            }
            pDialog.dismiss();
            listViewAdapter.notifyDataSetChanged();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }