2
我的自定義適配器不會打印出ListView。在我的代碼中,我從URL中獲取JSON輸入並解析它以顯示在我的自定義ListView中。不顯示列表視圖的自定義適配器
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ProgressDialog pDialog;
ArrayList<String> workList;
String title;
String imagepathview;
String description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
workList = new ArrayList<>();
new GetWork().execute();
}
private class GetWork extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Work...");
pDialog.setCancelable(false);
pDialog.show();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected Void doInBackground(Void... arg0) {
Context context;
// URL to get JSON data
String url = "http:xyz.json";
FirstAccessInDay f = new FirstAccessInDay(); //Makes HTTPHandler call and returns the JSON.
String jsonStr = f.firstaccess(url);
workList = null;
JSONArray a;
String key;
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
workList = new ArrayList<>();
Iterator iterator = jsonObj.keys();
while (iterator.hasNext()) {
key = (String) iterator.next();
// Getting JSON Array node
a = jsonObj.getJSONArray(key);
int j = 0;
JSONObject c = a.getJSONObject(j);
JSONObject d = a.getJSONObject(j + 1);
JSONObject e = a.getJSONObject(j + 2);
title = c.getString("title");
imagepathview = d.getString("image");
description = e.getString("description");
workList.add(title);
workList.add(imagepathview);
workList.add(description);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter adapter = new CustomListAdapter(MainActivity.this,R.layout.mylist,workList);
lv.setAdapter(adapter);
}
}
}
CustomListAdapter.java:
class CustomListAdapter extends ArrayAdapter<String> implements Adapter {
private final Activity context;
private ArrayList worklist;
static class ViewHolder{
TextView txtTitle;
ImageView imageView;
TextView extratxt;
}
CustomListAdapter(Activity context, int listViewId, ArrayList worklist) {
super(context, listViewId);
// TODO Auto-generated constructor stub
this.context = context;
this.worklist = worklist;
}
@NonNull
public View getView(int position, View view, @NonNull ViewGroup parent) {
ViewHolder holder;
View rowView= view;
ImageLoader imageLoader = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater();
rowView = inflater.inflate(R.layout.mylist, parent, false);
holder = new ViewHolder();
imageLoader = ImageLoader.getInstance();
holder.txtTitle = (TextView) rowView.findViewById(R.id.title);
holder.imageView = (ImageView) rowView.findViewById(R.id.image);
holder.extratxt = (TextView) rowView.findViewById(R.id.description);
rowView.setTag(holder);
}
else {
holder = (ViewHolder) rowView.getTag();
}
String title = (String)worklist.get(position);
holder.txtTitle.setText(title);
String imagepathview = (String)worklist.get(position);
imageLoader.displayImage(imagepathview, holder.imageView);
String description = (String) worklist.get(position);
holder.extratxt.setText(description);
return rowView;
}
}
這是我的logcat:
Process pipe failed
[ 03-01 14:36:02.192 4772: 4806 D/ ]
HostConnection::get() New Host Connection established 0x7f04c6b3e020, tid 4806
03-01 14:36:02.203 4772-4806/com.example.customlistview I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 14:36:02.265 4772-4806/com.example.customlistview E/EGL_emulation: tid 4806: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
03-01 14:36:02.266 4772-4806/com.example.customlistview W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f04bde0b2c0, error=EGL_BAD_MATCH
03-01 14:36:02.347 4772-4806/com.example.customlistview E/EGL_emulation: tid 4806: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
03-01 14:36:02.347 4772-4806/com.example.customlistview W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f04bde0b300, error=EGL_BAD_MATCH
03-01 14:36:03.937 4772-4806/com.example.customlistview E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f04bf6d0a80
03-01 14:36:03.959 4772-4772/com.example.customlistview D/AndroidRuntime: Shutting down VM
03-01 14:36:03.968 4772-4772/com.example.customlistview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.customlistview, PID: 4772
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.customlistview.ModelClass
at com.example.customlistview.CustomListAdapter.getView(CustomListAdapter.java:73)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:676)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:479)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-01 14:36:06.429 4772-4772/com.example.customlistview I/Process: Sending signal. PID: 4772 SIG: 9
評論不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/137138/discussion-on-answer-by-intellij-amiya-custom-adapter-not-displaying-listview)。 –