我正在使用一个应用程序,我从我的azure数据库中获取记录并将其插入到数组中,listview
必须显示我的数组。当我尝试调用方法notifyDatasetChanged
时出现错误,这是我的代码:
Button search;
EditText Esearch;
ListView list;
BaseAdapter ADAhere;
Connection connect;
List<Map<String,String>> MyData = new ArrayList();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.srail, container, false);
search = (Button)rootView.findViewById(R.id.btnSearch);
Esearch = (EditText)rootView.findViewById(R.id.srch);
list = (ListView)rootView.findViewById(R.id.view);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
String[] fromwhere = { "NAME","PRICE","RANGE","SUPPLIER","SIZE" };
int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.Range_txtView,R.id.size_txtView,R.id.supplier_txtView};
ADAhere = new SimpleAdapter(getActivity(), MyData,R.layout.list_products, fromwhere, viewswhere);
list.setAdapter(ADAhere);
}
});
return rootView;
}
public class CheckLogin extends AsyncTask<String, String, String> {
String z = "";
Boolean isSuccess = false;
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(getActivity(), "Searching...",
"Listview Loading! Please Wait...", true);
}
@Override
protected void onPostExecute(String r) {
progress.dismiss();
Toast.makeText(getActivity(), r, Toast.LENGTH_SHORT).show();
if (isSuccess) {
Toast.makeText(getActivity(), "Search Successfull", Toast.LENGTH_LONG).show();
//finish();
}
}
@Override
protected String doInBackground(String... strings) {
String Search = search.getText().toString();
try {
ConnectionHelper conStr = new ConnectionHelper();
connect = conStr.connectionclass(); // Connect to database
if (connect == null) {
z = "Check Your Internet Access!";
} else {
// Change below query according to your own database.
String query = "select * from cc_rail where rail_name='" + Search.toString() +"' ";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("NAME", rs.getString("RAIL_NAME"));
datanum.put("PRICE", rs.getString("RAIL_UNIT_PRICE"));
datanum.put("RANGE", rs.getString("RAIL_RANGE"));
datanum.put("SUPPLIER", rs.getString("RAIL_SUPPLIER"));
datanum.put("SIZE", rs.getString("RAIL_SIZE"));
MyData.add(datanum);
}
ADAhere.notifyDatasetChanged();
z = " successful";
isSuccess = true;
connect.close();
}
} catch (Exception ex) {
isSuccess = false;
z = ex.getMessage();
}
return z;
}
}
错误出现在代码的这一部分:
String query = "select * from cc_rail where rail_name='" + Search.toString() +"' ";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("NAME", rs.getString("RAIL_NAME"));
datanum.put("PRICE", rs.getString("RAIL_UNIT_PRICE"));
datanum.put("RANGE", rs.getString("RAIL_RANGE"));
datanum.put("SUPPLIER", rs.getString("RAIL_SUPPLIER"));
datanum.put("SIZE", rs.getString("RAIL_SIZE"));
MyData.add(datanum);
}
ADAhere.notifyDatasetChanged();
答案 0 :(得分:5)
写
ADAhere.notifyDataSetChanged();
而不是
ADAhere.notifyDatasetChanged();
您写了小s
而不是S
更好的做法是在onPostExecute
中编写此代码而不是写doInBackground
答案 1 :(得分:1)
您的问题在于您编写方法名称notifyDataSetChanged()
的方式(您在 set 中使用了一个小s),所以它无法识别它是正常的< / p>
更改此行
ADAhere.notifyDatasetChanged();
带
ADAhere.notifyDataSetChanged();
答案 2 :(得分:1)
您已创建BaseAdapter并尝试通知适配器。创建一个ADAhere对象作为SimpleAdapter,扩展BaseAdapter