无法将字符串从Asynctask传递到Fragment

时间:2016-07-29 12:04:21

标签: android android-fragments android-asynctask

第一次使用片段。

尝试将onPostCreate中的字符串值传递给片段。

public class FillCust extends AsyncTask<String, String, List<Map<String, String>>> {
    String z = "" ;

    @Override
    protected void onPostExecute(List<Map<String, String>> r) {
        super.onPostExecute(r);

        custName = String.valueOf(CustData.get(0).get("A"));
        contactPerson = String.valueOf(CustData.get(0).get("B"));
        telNo = String.valueOf(CustData.get(0).get("C"));
        assignedBranch = String.valueOf(CustData.get(0).get("D"));

    }

    @Override
    protected List<Map<String, String>> doInBackground(String... params) {

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server";
            } else {
                String query = "select * from tblCustomer_Master where Customer_ID ='" + CustID + "'";
                PreparedStatement ps = con.prepareStatement(query);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString("Company_Name"));
                    datanum.put("B", rs.getString("Contact_Person"));
                    datanum.put("C", rs.getString("Tel_No"));
                    datanum.put("D", rs.getString("Assigned_Branch"));
                    CustData.add(datanum);

                }
                z = "Success";
            }
        } catch (Exception ex) {
            z = "Error retrieving data from table";

        }
        return CustData;
    }
}

我的界面

public String getCompanyname(String result) {
   return custName ;
}

2 个答案:

答案 0 :(得分:0)

interface中实施fragment,在AsyncTask constructor中通过onPostExecute调用接口方法。

答案 1 :(得分:0)

首先创建这样的界面

Object

在片段上实现此功能

并从onPostExecute()中调用此接口方法。

    public interface AsyncInterface {

            public abstract void companyName(String result);
}