【问题标题】:Custom ListView not showing any items自定义 ListView 不显示任何项目
【发布时间】:2015-01-10 13:13:05
【问题描述】:

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

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;
    }
}

片段博客:

<?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>

我找不到任何错误或发现异常情况,为什么会这样?

【问题讨论】:

  • layout.removeAllViews();删除 blogPage 中的所有视图,包括您的 listView
  • @localhost 仍然不是问题:/
  • 覆盖 getCount 并返回 ArraySize nad 为什么要layout.removeAllViews(); ??
  • 问题依旧,然后切换到BaseAdapter

标签: android listview tablelayout


【解决方案1】:

你应该扩展 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));

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

编辑:

您可以在构造函数中获得一个充气机并保持在课堂上,无需每次都获得充气机(性能稍微好一点)

  LayoutInflater inflater = context.getLayoutInflater();

编辑 2:

localhost 给出了正确的注释 - 您正在从 RelativeLayout 和 ListView 中删除所有视图,并创建新的 ListView 而不添加到 Relative。保留引用不会自动添加视图

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();
            }
        });
    }

【讨论】:

  • 我完全按照你说的做了,但是什么也没发生
  • 签出edit2,您正在删除所有视图,还有ListView,但之后的addView(list)在哪里...
  • 到目前为止,列表边框出现了,所以我点击了 toast 正确显示的任何地方。问题是该项目本身在列表中不可见
  • inflater.inflate(R.layout.fragment_listview, parent, false);将 textColor 更改为粉红色或其他,也许都是白色/黑色(但我在您的代码中没有看到类似的东西......)。另外:字体在 sp,而不是 dp(16sp ?),并为 ListView 搜索 ViewHolder 模式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 2013-08-03
  • 2016-03-11
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多