在片段中创建自定义列表视图不起作用

时间:2016-08-09 05:48:15

标签: java android android-fragments

我在片段中创建了一个自定义列表视图但是当我运行时,它在下面没有显示任何内容是完整代码请帮帮我

这是我的PlanetFragment类



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;

	}

}
&#13;
&#13;
&#13;

这是我的ActorsAdapter类

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

这里是fragment_planet.xml

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

这里是row.xml

&#13;
&#13;
"t".charCodeAt(0)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要在适配器上调用notifyDataSetChanged()以便更新ListView

来自notifyDataSetChanged()的文档:

  

通知附加的观察者基础数据已经存在   已更改,任何反映数据集的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);
                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();
    }
}