0

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 :)

2
  • post your adapter code here Commented Jan 19, 2018 at 5:25
  • You are not notifying the change made to the list to the adapter just call adapter_name.notifyDataSetChanged(); after the while completion it will notify the changes and it will display data Commented Jan 19, 2018 at 5:31

2 Answers 2

2

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();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

but you mistake here ADM
list is not empty NewBee executed asynktask then set adapter to view
can you explain i m not getting why notifysetchnaged worked
First of all all 4 lists are empty see the onCreate(). And second thing there is no sense of using 4 lists for single adapter. The OP should use a POJO class for it .
@VineshChauhan The Async part of AsyncTask means asynchronous. Execution in onCreate() proceeds immediately after the new GetMydata().execute() call; i.e., it doesn't wait for the AsyncTask to complete.
-1

you have to notify your adapter whenever you change the data of the list by notifyDataSetChanged() after changing the data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.