【问题标题】:Android ListView doesn't get populated in FragmentAndroid ListView 没有在 Fragment 中填充
【发布时间】:2015-07-16 06:47:32
【问题描述】:

我的自定义适配器没有填充片段中的 ListView。它不会抛出任何异常,也不会被填充。

ArrayList 不为空。我通过打印它们进行了检查。

我的片段:

    public class NewsfeedTabActivity extends Fragment {

    View rootView;
    private ArrayList<ActivityTable> activitiesList = new ArrayList<ActivityTable>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.activity_newsfeed_tab, container, false);

            context = container.getContext();

            rootView = inflater.inflate(R.layout.activity_newsfeed_tab, container, false);

            RefreshNewsFeed();
    }

    private void RefreshNewsFeed() {

            Log.e("CHECK", "HEY1");
            ListView newsreel = (ListView) rootView.findViewById(R.id.newsfeed_list); //the listview
            FeedAdapter adapter = new FeedAdapter(getActivity(), activitiesList); //the adapter
            newsreel.setAdapter(adapter);
            Log.e("CHECK", "HEY2");

            //The log successfully logs the HEY1 and HEY2
    }

    public void onActivityCreated(Bundle savedInstanceState)
    {
            super.onActivityCreated(savedInstanceState);
    }
}

在上述示例中,HEY1 和 HEY2 已成功登录

适配器:

public class FeedAdapter extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater = null;
    ArrayList<ActivityTable> actList = new ArrayList<ActivityTable>();

    public FeedAdapter(Activity a, ArrayList<ActivityTable> actList) {

        activity = a;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.actList = actList;

        Log.d("CHECK", "WORKS1");
    }

    public int getCount() {
        return actList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        Log.d("CHECK", "WORKS2");

        View vi = convertView;
        ActivityTable act = actList.get(position);

        Log.d("CHECK", "WORKS3");

        if (convertView == null) {
            vi = inflater.inflate(R.layout.feed_single_text, null);
        }

        TextView caption = (TextView) vi.findViewById(R.id.postcaption); // caption
        // Setting all values in listview
        caption.setText(act.getContent());

        return vi;
    }
}

在适配器中,只有“WORKS1”被记录在构造函数中,而应用程序不记录“WORKS2”和“WORKS3”。我想它会中途停止?我做错了什么?

【问题讨论】:

  • 在 ** Log.d("CHECK", "WORKS1");** 之后打印 actList 大小并检查。我确定它会是 0
  • 它有 16 个项目。我刚刚检查了@CapDroid
  • 我也想知道为什么有人对我的问题投了反对票?
  • 可能是你分享了一半代码,请分享完整代码。我无法找到您将数据填充到 activitiesList 的代码
  • @CapDroid 假设它是完全伴侣。这是一个很长的代码,我检查了列表是否为空:)

标签: android android-layout listview android-fragments android-listview


【解决方案1】:

改变这个:

public Object getItem(int position) {
    return position;
}

收件人:

public ActivityTable getItem(int position) {
    return actList.get(position);
}

我通常将 Object 返回更改为正在返回的类型,在您的情况下为 ActivityTable。这也是一件好事,因为您不必在以后调用getItem 时强制转换它。

【讨论】:

  • 我对此寄予厚望,但没有,伙计。仍然没有到达适配器的末尾:/
【解决方案2】:

您的代码是正确的,而不是让全局根视图对象将ListView 作为全局对象并检查列表是否为空。

public class NewsfeedTabActivity extends Fragment {
    private ListView newsreel;
    private ArrayList<ActivityTable> activitiesList = new ArrayList<ActivityTable>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {           

           View rootView = inflater.inflate(R.layout.activity_newsfeed_tab, container, false);
            newsreel = (ListView) rootView.findViewById(R.id.newsfeed_list); //the listview
           RefreshNewsFeed();
           return rootView;
    }

    private void RefreshNewsFeed() {

            Log.e("CHECK", "HEY1");

            FeedAdapter adapter = new FeedAdapter(getActivity(), activitiesList); //the adapter
            newsreel.setAdapter(adapter);
            Log.e("CHECK", "HEY2");

            //The log successfully logs the HEY1 and HEY2
    }

    public void onActivityCreated(Bundle savedInstanceState)
    {
            super.onActivityCreated(savedInstanceState);
    }
}

【讨论】:

    猜你喜欢
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2020-01-07
    • 2013-06-04
    相关资源
    最近更新 更多