自定义ListView不显示任何项目

时间:2014-11-13 11:39:55

标签: android listview tablelayout

我正在尝试显示自定义列表视图,但没有显示任何内容。 我的活动:

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.example.elnoorgeh.ServerAPI;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BlogFragment extends Fragment {
    JSONArray jArray;
    TextView title;
    RelativeLayout layout;
    int previousID = 0;
    int currentID = 0;
    ArrayList<String> titles;
    ArrayList<String> contents;
    ListView list;

    public BlogFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_blog, container,
                false);
        new AsyncFetch().execute();
        return rootView;
    }

    private class AsyncFetch extends AsyncTask<Object, Object, Object> {

        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            jArray = new JSONArray();
            jArray = ServerAPI.getData();
            titles = new ArrayList<String>();
            contents = new ArrayList<String>();
            for (int i = 0; i < jArray.length(); i++) {
                String blogTitle = null;
                String content = null;
                try {
                    blogTitle = jArray.getJSONObject(i).getString("title");
                    content = jArray.getJSONObject(i).getString("content");
                    titles.add(blogTitle);
                    contents.add(content);

                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                System.out.println(blogTitle);
                System.out.println(content);
            }
            // display(titles, contents);
            return null;
        }

        protected void onPostExecute(Object result) {

            layout = (RelativeLayout) getView().findViewById(R.id.blogPage);
            layout.removeAllViews();

            CustomList adapter = new CustomList(getActivity(), titles);
            list = new ListView(getActivity());
            System.out.println("list done");
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(getActivity(),
                            "You Clicked at " + titles.get(position),
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public void display(ArrayList<String> t, ArrayList<String> c) {

    }
}

自定义ListView类:

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String> {
    private final Activity context;
    private final ArrayList<String> web;
//  private final Integer[] imageId;

    public CustomList(Activity context, ArrayList<String>web) {
        super(context, R.layout.fragment_listview);
        this.context = context;
        this.web = web;
//      this.imageId = imageId;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.fragment_listview, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText((CharSequence) web.get(position));
//      imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

fragment_blog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/blogPage"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <ListView
       android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

    <TextView
        android:id="@+id/txtLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:maxLines="20"
        android:singleLine="false"
        android:textSize="16dp" />


</RelativeLayout>

fragment_listview:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow>

        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
    </TableRow>

</TableLayout>

我无法发现任何错误或发现不正常的情况,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

你应该扩展BaseAdapter并实现抽象方法

@Override
public int getCount() {
    return web.size();
}

@Override
public Object getItem(int position) {
    return web.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

你也可以改变

txtTitle.setText((CharSequence) web.get(position));

txtTitle.setText((CharSequence) getItem(position));

现在你的适配器不知道网络数组的大小

编辑:

你可以在构造函数中获得一个inflater并保持在课堂上,不需要每次都充满热量(对于性能而言稍微好一些)

  LayoutInflater inflater = context.getLayoutInflater();

编辑2:

localhost放置正确的注释 - 您将从RelativeLayout,ListView中删除所有视图,并在不添加到Relative的情况下创建新的ListView。保持参考不会自动添加视图

protected void onPostExecute(Object result) {

        layout = (RelativeLayout) getView().findViewById(R.id.blogPage);
        layout.removeView(getView().findViewById(R.id.txtLabel);
        //assuming you need to remove only txtLabel

        CustomList adapter = new CustomList(getActivity(), titles);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(getActivity(),
                        "You Clicked at " + titles.get(position),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }