[英]created a custom listview in fragment not working
I created a custom listview in fragment but when i run, it's not showing anything here below is full code please help me 我在片段中创建了一个自定义列表视图,但运行时未在此处显示任何内容,以下为完整代码,请帮助我
here is my PlanetFragment class 这是我的PlanetFragment类
public class PlanetFragment extends ListFragment { public static final String ARG_PLANET_NUMBER = "planet_number"; ListView listView; ArrayList<Actors> actorList; ActorsAdapter adapter; private View view; public PlanetFragment() { // Empty constructor required for fragment subclasses } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view == null) { view = inflater.inflate(R.layout.fragment_planet, container, false); }else { ViewGroup parent = (ViewGroup) view.getParent(); parent.removeView(view); } listView = (ListView) view.findViewById(android.R.id.list); actorList = new ArrayList<Actors>(); adapter = new ActorsAdapter(getActivity(),R.layout.row, actorList); listView.setAdapter(adapter); new JSONAsyncTask().execute("/UpcomingEvents/GetEvents/1"); return view; } public class JSONAsyncTask extends AsyncTask<String, Void, Boolean>{ @Override protected Boolean doInBackground(String... params) { HttpGet httpGs[0]); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = httpClient.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if(status == 200){ HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); // JSONObject jsonObject = new JSONObject(data); JSONArray jsonArray = new JSONArray(data); for(int i=0;i<jsonArray.length();i++){ JSONObject object = jsonArray.getJSONObject(i); Actors actor = new Actors(); actor.setName(object.getString("eventTitle")); actorList.add(actor); } return true; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } } }
here is my ActorsAdapter class 这是我的ActorsAdapter类
public class ActorsAdapter extends ArrayAdapter<Actors> { ArrayList<Actors> arraylistObject; int resource; Context context; ViewHolder holder; LayoutInflater li; private View view; public ActorsAdapter(Context context, int resource, ArrayList<Actors> objects) { super(context, resource, objects); // TODO Auto-generated constructor stub this.resource = resource; arraylistObject = objects; this.context = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub li = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = li.inflate(resource, parent, false); if (view == null) { holder = new ViewHolder(); view = li.inflate(resource, parent, false); holder.tvName = (TextView) view.findViewById(R.id.tvName); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.tvName.setText(arraylistObject.get(position).getName()); return view; } static class ViewHolder { public TextView tvName; } }
and here is fragment_planet.xml 这是fragment_planet.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.demoasynctask.MainActivity" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" tools:listitem="@layout/row"></ListView> </RelativeLayout>
here is row.xml 这是row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="4dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tom Cruise" android:textColor="#166CED" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tvDateOfBirth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#D64530" android:text="Date of Birth: July 3, 1962" /> <TextView android:id="@+id/tvHeight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Height: 1.80 m" android:textColor="#D64530" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/tvCountry" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#D64530" android:text="United States" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/tvDescriptionn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#009A57" android:text="Description" /> <TextView android:id="@+id/tvSpouse" android:layout_width="wrap_content" android:textColor="#166CED" android:layout_height="wrap_content" android:text="Spouse: Katie Holmes" /> <TextView android:id="@+id/tvChildren" android:layout_width="wrap_content" android:textColor="#166CED" android:layout_height="wrap_content" android:text="Children: Suri Cruise, Isabella Jane Cruise, Connor Cruise" /> </LinearLayout>
You need to call notifyDataSetChanged()
on your adapter for the ListView
to update. 您需要在适配器上调用notifyDataSetChanged()
来更新ListView
。
From the documentation of notifyDataSetChanged()
: 从notifyDataSetChanged()
的文档中:
Notifies the attached observers that the underlying data has been changed and any
View
reflecting the data set should refresh itself. 通知附加的观察者基础数据已更改,任何反映该数据集的View
都应刷新自身。
Something like this should work: 这样的事情应该起作用:
public class JSONAsyncTask extends AsyncTask<String, Void, Boolean>{
@Override
protected Boolean doInBackground(String... params) {
HttpGet httpGs[0]);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray jsonArray = new JSONArray(data);
for(int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
Actors actor = new Actors();
actor.setName(object.getString("eventTitle"));
actorList.add(actor);
}
return true;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
// tell the adapter that its data changed
adapter.notifyDataSetChanged();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.