我是Android开发的新手,我现在卡住了,因为我的自定义适配器中的getView方法没有被调用。我试图显示我从API收集的数据,解析它,并将其渲染到android studio的屏幕上。请帮忙!
这是我的遗产:
public class eventsFragment extends Fragment {
View myView;
View rootView;
ArrayList<events> eventList;
private ListView LV;
private ProgressDialog pDialog;
private static String url = "https://kambuku.com/app/tickets";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.events_layout, container, false);
eventList = new ArrayList<>();
LV = (ListView) rootView.findViewById(R.id.List);
//GET CONNECTION INFO
myView = inflater.inflate(R.layout.events_layout, container, false);
ConnectivityManager connectivityManager = (ConnectivityManager)
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new getAllEvents().execute();
} else {
Toast.makeText(getActivity().getApplicationContext(),
"No Active Connection!",
Toast.LENGTH_LONG)
.show();
}
return myView;
}
private class getAllEvents extends AsyncTask<Void, Void, Void> {
String feed = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
//Show Progress Dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait... Fetching your Events");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
String Result;
String DEBUG_TAG = "EVENTS";
HttpHandler handle = new HttpHandler();
String jsonStr = null;
try {
jsonStr = handle.EventsConnection(url, DEBUG_TAG);
} catch (IOException e) {
e.printStackTrace();
}
Result = jsonStr;
Log.e(DEBUG_TAG, "JSON RESPONSE IS" + Result);
if (Result != null) {
try {
JSONObject jsonObject = new JSONObject(Result);
Log.e(DEBUG_TAG, "JSON OBJECT IS" + jsonObject);
//GET JSON ARRAY NODE
JSONArray tickets = jsonObject.getJSONArray("tickets");
Log.e(DEBUG_TAG, "JSON TICKET ARRAY LENGTH IS" + tickets.length());
for (int i = 0; i < tickets.length(); i++) {
JSONObject t = tickets.getJSONObject(i);
Log.e(DEBUG_TAG, "JSON OBJECT " + i + " is " + t);
events event = new events();
event.setEventName(t.getString("eventName"));
event.setId(t.getString("id"));
event.setVenue(t.getString("venue"));
event.setCity(t.getString("city"));
event.setArtwork("https://kambuku.com/"+t.getString("artwork"));
event.setHost(t.getString("host"));
event.setNotes(t.getString("notes"));
event.setPrice(t.getString("price"));
event.setEventDate(t.getString("eventDate"));
eventList.add(event);
}
} catch (final JSONException e) {
Log.e(DEBUG_TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(DEBUG_TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.e("PostExecute", "YOUR LIST ARE "+eventList);
eventsAdapter eventAdapter = new eventsAdapter(getActivity().getApplicationContext(), R.layout.event_item, eventList);
LV.setAdapter(eventAdapter);
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
这是我的适配器:
public class eventsAdapter extends ArrayAdapter {
private ArrayList<events> ArrayListEvents;
private int Resource;
private Context context;
private LayoutInflater inflater;
public eventsAdapter(Context context, int resource, ArrayList<events> objects) {
super(context, resource, objects);
Log.e("ADAPTER", "OBJECTS ARE "+objects);
Log.e("ADAPTER", "RESOURCES ARE "+resource);
ArrayListEvents = objects;
Resource = resource;
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("ADAPTER", "WE ARE HERE");
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(Resource, null);
holder = new ViewHolder();
holder.artwork = (ImageView) convertView.findViewById(R.id.Artwork);
holder.eventName = (TextView) convertView.findViewById(R.id.eventName);
holder.eventNotes = (TextView) convertView.findViewById(R.id.eventNotes);
convertView.setTag(holder);
} else {
holder = (eventsAdapter.ViewHolder)convertView.getTag();
}
new DownloadImageResource(holder.artwork).execute(ArrayListEvents.get(position).getArtwork());
holder.eventName.setText(ArrayListEvents.get(position).getEventName());
holder.eventNotes.setText(ArrayListEvents.get(position).getNotes());
Log.e("Holder", "Holder"+holder.eventName.getText());
return convertView;
}
private static class ViewHolder {
ImageView artwork;
TextView eventName;
TextView eventNotes;
}
private class DownloadImageResource extends AsyncTask<String, Void, Bitmap>
{
ImageView Artwork;
DownloadImageResource(ImageView Artwork)
{
this.Artwork = Artwork;
}
protected Bitmap doInBackground(String... urls)
{
String urlDisplay = urls[0];
Bitmap conn = null;
try
{
InputStream inputStream = new java.net.URL(urlDisplay).openStream();
conn = BitmapFactory.decodeStream(inputStream);
}catch (Exception e){
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return conn;
}
protected void onPostExecute(Bitmap result)
{
Log.e("Image", "Image Download"+result);
Artwork.setImageBitmap(result);
}
}
}
答案 0 :(得分:0)
Override getItemCount method in your adapter.
@Override public int getItemCount() { return ArrayListEvents .size(); }
答案 1 :(得分:0)
Your problem is you are executing nested AsyncTask. I.e. you are running another task for each row in the listview, which is already running a task. This is not good in Android. See this question for more details.