2016-04-22 68 views
0

我的项目中只有服务器操作(如获取和放置数据)中有类。我有类填充列表,然后获取此列表的方法。问题是,我打电话给“getList”方法和后台操作hasent完成,然后我从“getList”方法得到nulldoInBackground方法完成后从AsyncTask类对象检索arraylist

这是我的AsyncTask类,你可以看到“getList”假设给我的列表完成

public class GetRoomatesListActivity extends AsyncTask<String, String, String> { 
    private ArrayList<RoomateModel> tmpList; 
    private ArrayList<RoomateModel> completeList; 
    DBHelper db; 
    Context context; 

    public GetRoomatesListActivity(Context context){ 
     this.context = context; 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     db = DBHelper.getInstance(context); 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     tmpList = new ArrayList<RoomateModel>(); 
     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      InputStream stream = connection.getInputStream(); 

      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 

      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 

      } 
      String finalJson = buffer.toString(); 
      JSONObject parentObject = new JSONObject(finalJson); 
      JSONArray parentArray = parentObject.getJSONArray("result");//creates array of Roomates of the selected apartment 


      for (int i = 0; i < parentArray.length(); i++) { 
       JSONObject finalObject = parentArray.getJSONObject(i);//get the cuttent json object which is representaion of roomate object 

       String Fname = finalObject.getString("firstName"); 
       String Lname = finalObject.getString("lastName"); 
       String phone = finalObject.getString("phone"); 

       RoomateModel item = new RoomateModel(Fname, Lname, phone);//creates roomates model with the current item data from the array 
       tmpList.add(item);//adds the roomate to the list of roomates 

       //add the roomates to local data base 

       db.addRoomate(item,apartment); 
      } 

      return null; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     completeList = tmpList; 

    } 
    public ArrayList<RoomateModel> getList(){ 
     return completeList; 
    } 

} 

,这是我尝试获取列表,以便使用它,但它的检索空

public class roomatesScreen extends Activity { 
    ListView items; 
    ArrayList<RoomateModel> list; //list to compare with the list rerived from GetRoomatesListActivity 
    RoomatesAdapter adapter; 
    DBHelper db; 
    ApartmentModel apartment; 
    SharedPreferences preferences; 
    GetRoomatesListActivity r; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.roomates_list); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    items = (ListView) findViewById(R.id.roomatesList); 
    db = DBHelper.getInstance(this); 

    Bundle bundle = getIntent().getExtras(); 
    int number = bundle.getInt("number"); 
    apartment = new ApartmentModel(number);// creates apartment model with the user's apartment number 
    final String num = Integer.toString(number); 
    r = new GetRoomatesListActivity(this); 
    r.execute("this is the link to my query" + num); 
    list = r.getList(); //here list is null 
    adapter = new RoomatesAdapter(roomatesScreen.this, list); 
    items.setAdapter(adapter);//here application crushes because of nullPointerExpeption 

回答

2

,最好的办法是执行对UI更新类在AsyncTask的PostExecute方法中。 在您访问它时,该控件位于doInBackground方法中。所以那个时候你的列表是空的。 把这个

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 

    adapter = new RoomatesAdapter(roomatesScreen.this, tmpList); 
    items.setAdapter(adapter);/ 

} 

onPostExecute()

第二个解决方案

初始化,当你将它设置为适配器列表。像:

list = new ArrayList(); 

和其他工作(更新列表,并调用notifyDataSetChanged()适配器对象上)在onPostExecute()

+0

适配器和物品清单在“RoomatesScreen”类中定义的,如果我会中做了“getRoomatesListActivity”内部类“ RoomatesScreen“类将会起作用。但我并不 – ATT

+0

在这种情况下,您可以创建您的AsyncTask的构造函数,并传递上下文和其他信息来更新适配器刷新列表。 –

0

变化doInBackground()方法返回类型

public class GetRoomatesListActivity extends AsyncTask<String, String, ArrayList<Object>> { 
private ArrayList<RoomateModel> tmpList; 
private ArrayList<RoomateModel> completeList; 
DBHelper db; 
Context context; 

public GetRoomatesListActivity(Context context){ 
    this.context = context; 
} 

@Override 
protected ArrayList<Object> doInBackground(String... params) { 

    db = DBHelper.getInstance(context); 
    HttpURLConnection connection = null; 
    BufferedReader reader = null; 
    tmpList = new ArrayList<RoomateModel>(); 
    try { 
     URL url = new URL(params[0]); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 

     InputStream stream = connection.getInputStream(); 

     reader = new BufferedReader(new InputStreamReader(stream)); 
     StringBuffer buffer = new StringBuffer(); 

     String line = ""; 

     while ((line = reader.readLine()) != null) { 
      buffer.append(line); 

     } 
     String finalJson = buffer.toString(); 
     JSONObject parentObject = new JSONObject(finalJson); 
     JSONArray parentArray = parentObject.getJSONArray("result");//creates array of Roomates of the selected apartment 


     for (int i = 0; i < parentArray.length(); i++) { 
      JSONObject finalObject = parentArray.getJSONObject(i);//get the cuttent json object which is representaion of roomate object 

      String Fname = finalObject.getString("firstName"); 
      String Lname = finalObject.getString("lastName"); 
      String phone = finalObject.getString("phone"); 

      RoomateModel item = new RoomateModel(Fname, Lname, phone);//creates roomates model with the current item data from the array 
      tmpList.add(item);//adds the roomate to the list of roomates 

      //add the roomates to local data base 

      db.addRoomate(item,apartment); 
     } 

     return null; 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } finally { 
     if (connection != null) { 
      connection.disconnect(); 
     } 
     try { 
      if (reader != null) { 
       reader.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    completeList = tmpList; 

} 
public ArrayList<RoomateModel> getList(){ 
    return completeList; 
} 

}

相关问题