从数据库获取数据并在列表视图中显示

时间:2018-07-21 10:15:56

标签: android arrays listview azure-sql-database

我试图显示来自云中的sql表的数据并以listview形式显示。从getdata.java类访问数据时,我遇到了不兼容的类型。我创建了一个StorageClass.java来将数据添加到静态数组中,但是我发现很难显示它。

我的Getdata班:

public class GetValence extends AsyncTask<String, Integer, List<Map<String, String>>> {
Connection connect;
String ConnectionResult = "";
Boolean isSuccess = false;

public List<Map<String,String>> doInBackground(String... params) {

    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)
        {
            ConnectionResult = "Check Your Internet Access!";
        }
        else
        {
            // Change below query according to your own database.
            String query = "select * from cc_valence";
            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("VALENCE_NAME"));

                datanum.put("PRICE",rs.getString("VALENCE_UNIT_PRICE"));

                datanum.put("RANGE",rs.getString("VALENCE_RANGE"));

                datanum.put("SUPPLIER",rs.getString("VALENCE_SUPPLIER"));

                datanum.put("SIZE",rs.getString("VALENCE_SIZE"));
                data.add(datanum);
            }


            ConnectionResult = " successful";
            isSuccess=true;
            connect.close();
        }
    }
    catch (Exception ex)
    {
        isSuccess = false;
        ConnectionResult = ex.getMessage();
    }

    return data;
}
public void onPostExecute(Map<String, String> result) {
    StorageClass.setArrayData(result);
}

这是我的Storageclass

public class StorageClass {
private static Map<String,String> arrayData = new HashMap<>();

public static void setArrayData(Map<String,String> results){
    arrayData = results;
}

public static Map<String, String> getArrayData(){
    return arrayData;
}

这是我遇到错误的活动:

          List<Map<String,String>> MyData = null;
             StorageClass mydata =new StorageClass();
            MyData = getArrayData();
            String[] fromwhere = { "NAME","PRICE","SIZE" ,"RANGE","SUPPLIER"};

            int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.size_txtView};

            ADAhere = new SimpleAdapter(getActivity(), MyData,R.layout.list_valence, fromwhere, viewswhere);

            list.setAdapter(ADAhere);

我收到Mydata的错误,并显示以下错误: Required java.util.List Found java.util.map

2 个答案:

答案 0 :(得分:1)

我认为您的StorageClass类变量arrayData是错误的类型。它应该是List<Map<String,String>>类型。

public class StorageClass {
  private static List<Map<String,String>> arrayData = new ArrayList();

  public static void setArrayData(List<Map<String,String>> results){
      arrayData = results;
  }

  public static List<Map<String,String>> getArrayData(){
     return arrayData;
  }
}

让我知道它可以解决您的问题。

已添加

获得结果后,再次将Adapter设置为ListView

public void onPostExecute(List<Map<String,String>> result) {

     if( result != null){

       String[] fromwhere = { "NAME","PRICE","SIZE" ,"RANGE","SUPPLIER"};

       int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.size_txtView};

       adapter = new SimpleAdapter(getActivity(), result,R.layout.list_valence, fromwhere, viewswhere);

       list.setAdapter(adapter);
    }
}

答案 1 :(得分:0)

在活动中将 SELECT d.Name,AVG(e.Salary) AS 'Avg Sal > 1000000' FROM #Employee e JOIN #Department d ON e.DeptId=d.DeptId GROUP BY d.Name HAVING AVG(e.Salary)>1000000 设为内部类,并将您在问题中提到的GetValence添加到Activity code的{​​{1}}中。将onPostExecute直接设置到GetValence中的适配器,如下所示:

result map

编辑

onPostExecute