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;
}
}}
我在设置适配器类中有错误,有人可以建议吗?
答案 0 :(得分:0)
您在null
中返回doInBackground
,然后将其设置为适配器。
您有2个选项可以解决它:
返回一个空数组,例如return new ArrayList<>();
而不是return null
要么
检查不要将适配器设置为null。 e.g:
@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密钥(&#34; Hotel_Name&#34;)与JSON本身不一样(其&#34;酒店名称&#34;在JSON中)。修复之后,您将看到列表,尽管它仍然很重要,可以先解决异常。
答案 1 :(得分: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
}
希望它有所帮助!!!