2016-12-05 52 views
-3
public class MainActivity extends AppCompatActivity { 

ListView lv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     lv=(ListView)findViewById(R.id.list_id); 
     new JsonTask().execute("https://www.dropbox.com/s/o8yqrfm08wfdh48/Hotels.txt?dl=0"); 
    } 

class JsonTask extends AsyncTask<String,String ,List<MovieModels>>{ 
    @Override 
    protected List<MovieModels> doInBackground(String...Params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     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("Hotels"); 
      List<MovieModels> modelsList=new ArrayList<>(); 

      for (int i=0; i<parentArray.length();i++) { 
       JSONObject finalobject = parentArray.getJSONObject(i); 

       MovieModels models= new MovieModels(); 
       models.setImage(finalobject.getString("Image")); 
       models.setHotel_Name(finalobject.getString("Hotel_Name")); 

       modelsList.add(models); 

      } 

       return modelsList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     return null; 
    } 

    @Override 
    protected void onPostExecute(List<MovieModels> result) { 
     super.onPostExecute(result); 

     Adapter adapter=new Adapter(getApplicationContext(),R.layout.row,result); 
     lv.setAdapter(adapter); 

     //to do set the data into list 


    } 
} 

public class Adapter extends ArrayAdapter{ 

private List<MovieModels> modelsList; 
    private int resource; 
    private LayoutInflater inflater; 
    public Adapter(Context context, int resource, List<MovieModels> objects) { 
     super(context, resource, objects); 
     modelsList= objects; 
     this.resource=resource; 
     inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (convertView==null){ 
      convertView=inflater.inflate(R.layout.row,null); 

     } 
     ImageView imageView; 
     TextView textView; 
     imageView=(ImageView)convertView.findViewById(R.id.image_id); 
     textView=(TextView)convertView.findViewById(R.id.text_id); 

     ImageLoader.getInstance().displayImage(modelsList.get(position).getImage(),imageView); 


     textView.setText("Hotel_Name-"+modelsList.get(position).getHotel_Name()); 

     return convertView; 
    } 
}} 

我在设置适配器类中有错误,有谁能提醒吗?java.lang.NullPointerException:尝试调用接口方法'int java.util.List.size()

+0

你的问题是什么? – Carcigenicate

+0

张贴您的MovieModels.java代码请 –

+1

通过在json数组中的前3项是酒店名称,而不是Hotel_Name缺少_所以修复它,如果你想获得所有结果 –

回答

0

创建一个类为您MovieModels这样的:

public class MovieModels(){ 
    private String imagePath, hotelName; 

    public MovieModels(String imagePath, String hotelName){ 
     this.imagePath = imagePath; 
     this.hotelName = hotelName; 
    } 

    public String getImagePath(){ 
     return imagePath; 
    } 

    public String getHotelName(){ 
     return hotelName; 
    } 

    public void setImagePath(String imagePath){ 
     this.imagePath = imagePath; 
    } 

    public void setHotelName(String hotelName){ 
     this.hotelName = hotelName; 
    } 


} 

,然后修改您的doInBackground()方法是这样的:

@Override 
    protected List<MovieModels> doInBackground(String...Params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     List<MovieModels> modelsList=new ArrayList<>(); 
     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("Hotels"); 
      for (int i=0; i<parentArray.length();i++) { 
       JSONObject finalobject = parentArray.getJSONObject(i); 
       String image = finalobject.getString("Image"); 
       String hotelName = finalobject.getString("Hotel_Name"); 
       modelsList.add(new MovieModels(image, hotelName)); 
      } 

      return modelsList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     return null; 
    } 

和你postExecute()这样的:

@Override 
protected void onPostExecute(List<MovieModels> result) { 
    super.onPostExecute(result); 
    if (result == null) { 
     Log.e("MY APPLICATION", "No result to display"); 
    } 
    Adapter adapter=new Adapter(getApplicationContext(),R.layout.row,result); 
    lv.setAdapter(adapter); 

    //to do set the data into list 
} 

希望它有助于!!!

+0

适配器中的theist计数它可能仍然有NullPointerException异常提到我的回答 – fbwnd

+0

忘记onPost –

+0

我有一个MovieModels类..Whatever你暗示我做了那个但代码正在执行但不显示任何内容。只是空白的屏幕。并且我还纠正了你提出错误的json文件。 PLZ ..帮助 – Amol

0

您在doInBackground中返回null,然后将其设置为适配器。

您有2个选项来解决这个问题:

  1. 返回e.g的空数组return new ArrayList<>();代替return null 或 检查不设置的情况下,该适配器的零。例如:

    @Override 
    protected void onPostExecute(List<MovieModels> result) { 
        super.onPostExecute(result); 
        if (result == null) { 
         //todo handle no result 
         return; 
        } 
        Adapter adapter=new Adapter(getApplicationContext(),R.layout.row,result); 
        lv.setAdapter(adapter); 
    
        //to do set the data into list 
    } 
    

    }

编辑:

此外,作为评论说,你JSON的关键,你试图让( “HOTEL_NAME”)不喜欢的人JSON本身(JSON中的“酒店名称”)。在修复之后,您会看到列表,尽管它首先解决异常仍然很重要。

+0

Thanxx ......它的工作,但不显示ListView .... – Amol

+0

你仍然需要修复JSON。关键是错的,所以它不解析它 – fbwnd

+0

我做了正确的关键。但之后也不会显示ListView。 – Amol

相关问题