我正在尝试使用适配器将JSON数据传递给listview。现在我在logcat中遇到错误:
package ir.homa;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by SMQ on 9/17/2016.
*/
public class RoomTypeAdapter extends ArrayAdapter<RoomType> {
public RoomTypeAdapter(Context context, ArrayList<RoomType> roomType) {
super(context,0, roomType);
}
@Override
public View getView(int position , View ConvertView, ViewGroup parent ){
RoomType roomType = getItem(position);
if (ConvertView == null) {
ConvertView = LayoutInflater.from(getContext()).inflate(R.layout.item_type, parent, false);
}
TextView typeItem = (TextView) ConvertView.findViewById(R.id.itemRoomType);
typeItem.setText(roomType.RoomType);
return ConvertView;
}
}
这是我的适配器:
package ir.homa;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by SMQ on 9/17/2016.
*/
public class RoomType {
String RoomType;
String id;
public void setRoomType(JSONObject object) {
try {
this.RoomType = object.getString("description");
this.id = object.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我的类,其中包含变量:
package ir.homa;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by SMQ on 9/4/2016.
*/
public class HotelDetail extends Activity {
// json array response url
private String urlJsonArry = "http://homa.qadamgahi.ir/bandar_result.json";
private static String TAG = HotelDetail.class.getSimpleName();
ArrayList<RoomType> typeList;
RoomTypeAdapter adapter;
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
typeList = new ArrayList<>();
adapter = new RoomTypeAdapter(this, typeList);
listView = (ListView) findViewById(R.id.listv);
listView.setAdapter(adapter);
makeJsonObjectRequest();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
/*
ATTENTION: This was auto-generated to implement the App Indexing API.
See https://g.co/AppIndexing/AndroidStudio for more information.
*/
GoogleApiClient client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
/**
* Method to make json object request where json response starts wtih {
* */
private void makeJsonObjectRequest() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
urlJsonArry, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
// Parsing json object response
// response will be a json object
RoomType o = new RoomType();
try {
for (int i = 0 ; i < typeList.size() ; i++)
{
RoomType add = new RoomType();
add.id = response.getString("id");
add.RoomType = response.getString("description");
typeList.add(add);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
}
这是我的MainActivity:
{{1}}
请告诉我如何使用listview填充json数据,这是什么问题?!
答案 0 :(得分:1)
您忘了拨打- (void)addBlurView
{
CGRect viewBounds = [[UIScreen mainScreen]applicationFrame];
self.myModelView = [[MyModalView alloc] initWithFrame:CGRectMake(viewBounds.origin.x, -16, viewBounds.size.width, viewBounds.size.height+36)];
if(![self toolbar]) {
_toolbar = [[UIToolbar alloc] initWithFrame:[self.myModelView bounds]];
[_toolbar setBarStyle:UIBarStyleBlack];
_toolbar.alpha = 0.97;
[self.myModelView.layer insertSublayer:_toolbar.layer atIndex:0];
}
[self.view addSubview:self.myModelView];
}
方法。
setContentView()