数据未显示在自定义 ListView 中
Data is not displaying in custom ListView
我创建了一个自定义列表视图,它从数据库中获取数据。在 toast 的帮助下,我能够看到所有值都正确显示,但这些值未显示在 Listview 中。 ListViewAdapter class 工作正常。
这是我的 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();
}
}
}
}
请告诉我缺少什么,谢谢:)
您在 onCreate()
中用空列表调用了 setAdapter()
。 .
现在,当您加载数据时,您需要在 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();
}
}
你必须通知你的适配器每当你改变列表的数据notifyDataSetChanged()
后改变数据
我创建了一个自定义列表视图,它从数据库中获取数据。在 toast 的帮助下,我能够看到所有值都正确显示,但这些值未显示在 Listview 中。 ListViewAdapter class 工作正常。 这是我的 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();
}
}
}
}
请告诉我缺少什么,谢谢:)
您在 onCreate()
中用空列表调用了 setAdapter()
。 .
现在,当您加载数据时,您需要在 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();
}
}
你必须通知你的适配器每当你改变列表的数据notifyDataSetChanged()
后改变数据