我正在创建一个listview,我从azure数据库通过带有where语句的SQL查询获取我的项目。我的代码运行但是当我尝试使用toast
在屏幕底部显示[]
显示的内容时,这是我的代码:
@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.raill);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
List<Map<String,String>> MyData = new ArrayList();
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);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String,Object> obj=(HashMap<String,Object>)ADAhere.getItem(position);
String ID=(String)obj.get("A");
Toast.makeText(getActivity(), ID, Toast.LENGTH_SHORT).show();
}
});
}
});
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();
if (Search.trim().equals(""))
z = "Please Search something";
else {
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
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() + "' OR RAIL_UNIT_PRICE= '" + Search.toString() + "' OR RAIL_RANGE= '" + Search.toString() + "' OR RAIL_SUPPLIER='" + Search.toString() + "' OR RAIL_SIZE='" + 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"));
data.add(datanum);
}
z = " successful";
isSuccess = true;
connect.close();
}
} catch (Exception ex) {
isSuccess = false;
z = ex.getMessage();
}
return String.valueOf(data);
}
return z;
}
}
}
答案 0 :(得分:1)
您正在使用此MyData
变量作为适配器的数据源:
List<Map<String,String>> MyData = new ArrayList();
...
ADAhere = new SimpleAdapter(getActivity(), MyData,R.layout.list_products, fromwhere, viewswhere);
...
list.setAdapter(ADAhere);
然而,您并未向直接数据源MyData
或适配器ADAhere
添加任何内容。
修改强>
你可以做些什么来解决这个问题,让你的数据变量全局化(在onCreateView()之前移动它:
List<Map<String,String>> MyData = new ArrayList();
然后在你的asynctask中你调用add来向它添加数据:
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()
然后,当你完成将数据加载到MyData中时,只需调用ADAhere.notifyDatasetChanged()。