数据未显示在自定义ListView中

时间:2018-01-19 05:23:52

标签: android listview android-asynctask custom-lists

我创建了一个自定义列表视图,用于从数据库中获取数据。在toast的帮助下,我能够看到所有值都正常,但这些值未在Listview中显示。 ListViewAdapter类正常工作。 这是我的mainActivity代码

    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();
           }
       }
   }

}

请告诉我缺少什么,谢谢:)

2 个答案:

答案 0 :(得分:2)

您已使用空列表在setAdapter()中呼叫onCreate()。 。 现在,当您加载数据时,需要在Adapter中调用notifydatasetChanged()。请参阅下面的代码。

@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();
    }
}

答案 1 :(得分:-1)

更改数据后,每当您更改notifyDataSetChanged()列表数据时,您必须通知适配器