Android ListView没有填充在Fragment中

时间:2015-07-16 06:47:33

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

我的自定义适配器不会填充片段中的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;
    }
}

在适配器中,只有&#34; WORKS1&#34;登录构造函数,应用程序没有记录&#34; WORKS2&#34;和&#34; WORKS3&#34;。我假设它被中途停止了?我做错了什么人?

2 个答案:

答案 0 :(得分:1)

改变这个:

doDelete

要:

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

我通常会将public ActivityTable getItem(int position) { return actList.get(position); } 返回更改为您返回的类型Object。这也是一件好事,因为你打电话给ActivityTable时不必再投出它。

答案 1 :(得分:0)

您的代码是正确的,而不是使全局根视图对象将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);
    }
}