ListView不再显示在Tab内

时间:2013-12-05 06:48:32

标签: android android-listview android-tabhost android-support-library

我在几个月内没有碰过这个代码库。既然我已经再次提起它,我认为我没有改变的一些代码已停止工作:由simplecursoradapter填充的应用程序中的所有listfragment列表视图已停止工作并且只是空白。起初我以为光标或数据库出错了或者说适配器没有正确设置,所以我给onLoadFinished添加了一些调试:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    Log.d("IndexListFragment", "Rows returned: " + cursor.getCount());
    Log.d("IndexListFragment", cursor.getColumnName(0));
    Log.d("IndexListFragment", mAdapter.toString());
    Log.d("IndexListFragment", this.getListView().getAdapter().toString());

    mAdapter.swapCursor(cursor);
}

这会返回我期望的内容:

IndexListFragment: Rows returned: 2324
IndexListFragment: _id
IndexListFragment: android.support.v4.widget.SimpleCursorAdapter@41d06a18
IndexListFragment: android.support.v4.widget.SimpleCursorAdapter@41d06a18

据我所知,光标在那里并且充满了数据,但是交换它并没有填充listview。有什么想法吗?

为了完整性,这里是创建和设置适配器的onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] from = new String[] { GuidebookProvider.COL_NAME,
            GuidebookProvider.COL_GRADE };
    int[] to = new int[] { android.R.id.text1, android.R.id.text2 };

    mAdapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_list_item_2, null, from, to, 0);
    setListAdapter(mAdapter);

    getLoaderManager().initLoader(0, null, this);
}

onCreateLoader:

@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle loaderArgs) {
    String orderBy = GuidebookProvider.COL_NAME;
    String[] projection = new String[] { GuidebookProvider.COL_ID, GuidebookProvider.COL_NAME, GuidebookProvider.COL_GRADE };


    return new CursorLoader(getActivity(),
            GuidebookProvider.CONTENT_URI_CLIMB, projection, null, null,
            orderBy);
}

编辑:我已经做了一些调试这个问题的工作。如果我直接从这样的活动运行它,该片段工作正常:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

public class TestActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout frame = new FrameLayout(this);
        frame.setId(android.R.id.content);

        if (savedInstanceState == null) {
            Bundle args = new Bundle();
            args.putInt(IndexListFragment.ARGUMENT_INDEX_TYPE, IndexListFragment.IndexType.ALPHA.value);

            Fragment indexListFragment = new IndexListFragment();
            indexListFragment.setArguments(args);

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(android.R.id.content, indexListFragment).commit();
        }
    }
}

但是,将其作为标签的一部分调用最终会显示一个空白标签:

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

public class TestActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tab_host);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Bundle args = new Bundle();
        args.putInt(IndexListFragment.ARGUMENT_INDEX_TYPE,
                IndexListFragment.IndexType.ALPHA.value);
        Drawable drawable = null;
        mTabHost.addTab(mTabHost.newTabSpec("A-Z")
                .setIndicator("A-Z", drawable), IndexListFragment.class, args);

    }
}

这是tabhost的我的xml:

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

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />
    </android.support.v4.app.FragmentTabHost>
 </LinearLayout>

直接调用时片段正确填充但在从选项卡中调用时没有正确填充这一事实似乎表明问题根本不存在游标或适配器,而是与listfragment在选项卡内运行的方式。

任何人都可以指出我做错了什么(或者在SDK的最后几次更新中有什么改变)来打破这段代码?过去工作得很好。

2 个答案:

答案 0 :(得分:0)

本准则可以帮助您尝试

ViewAllLesson.java:

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

import com.xyz.R;
import com.xyz.db.LessonDatabaseConnector;
import com.xyz.pojo.AssignmentPojo;
import com.xyz.pojo.LessonPojo;

public class ViewAllLessons extends Fragment {
    public LessonDatabaseConnector dao;
    private View rootView;
    List<LessonPojo> lessons;
    ArrayList<String> con = new ArrayList<String>();
    LayoutInflater mInflater;

    LessonCustomAdapter lessonCustomAdapter;
    private ListView listView;
    public static CheckBox selectAll;
    private Button deleteBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.add_exams);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.lesson_view_all_list, container,
                false);

        // dao = new AssignmentDatabaseConnector(getActivity());

        listView = (ListView) rootView.findViewById(R.id.Lesson_ListAll);
        selectAll = (CheckBox) rootView.findViewById(R.id.SelectAll);
        deleteBtn = (Button) rootView.findViewById(R.id.LessonDeleteBtn);
        Button addNew = (Button) rootView.findViewById(R.id.LessonAddNewBtn);
        addNew.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Toast.makeText(getActivity(), "hi",
                // Toast.LENGTH_LONG).show();

                Lesson lesson = new Lesson();
                getActivity().getSupportFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, lesson).commit();
            }
        });

        // viewgr.setScrollContainer(false);
        dao = new LessonDatabaseConnector(getActivity());
        lessons = dao.getAllLessonList();
        lessonCustomAdapter = new LessonCustomAdapter(getActivity(), lessons,
                this);
        listView.setAdapter(lessonCustomAdapter);

        /* To Check List View is empty */
        if (lessons.size() <= 0) {

            View empty = rootView.findViewById(R.id.empty);

            ListView list = (ListView) rootView
                    .findViewById(R.id.Lesson_ListAll);
            list.setEmptyView(empty);
            listView.setVisibility(0);
            empty.setVisibility(1);

        }

        selectAll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Toast.makeText(getActivity(), "hi",
                // Toast.LENGTH_LONG).show();

                if (((CheckBox) v).isChecked()) {
                    LessonCustomAdapter.flag = true;
                    lessonCustomAdapter.notifyDataSetChanged();
                } else {
                    LessonCustomAdapter.flag = false;
                    LessonCustomAdapter.checkedvalue.clear();
                    lessonCustomAdapter.notifyDataSetChanged();
                }
            }
        });

        // Set the list adapter and get TODOs list via DAO

        deleteBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LessonDatabaseConnector dao = new LessonDatabaseConnector(
                        getActivity());

                for (int i = 0; i < LessonCustomAdapter.checkedvalue.size(); i++) {
                    //
                    System.out
                            .println("===========LessonCustomAdapter.checkedvalue.size()==========SIZE======>>>>"
                                    + LessonCustomAdapter.checkedvalue.size());
                    System.out
                            .println("===========DELETED LESSON=================>>>>"
                                    + LessonCustomAdapter.checkedvalue.get(i));
                    dao.DeleteLesson(LessonCustomAdapter.checkedvalue.get(i));
                }
                LessonCustomAdapter.flag = false;

                ViewAllLessons viewAllLessons = new ViewAllLessons();
                getFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, viewAllLessons)
                        .commit();
                // finish();

            }

        });

        return rootView;

    }

    private List<AssignmentPojo> getAllAssignmentsList() {
        // TODO Auto-generated method stub
        return null;
    }

}

LessonCustomAdapter:

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.xyz.R;
import com.xyz.pojo.LessonPojo;

public class LessonCustomAdapter extends BaseAdapter {
    protected static boolean flag = false;
    private TextView mLessonTitle, mLessonDate;
    private final Context context;
    List<LessonPojo> Lessons;
    private ViewAllLessons viewAllLessons;
    private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
    private CheckBox SingleChk;
    public static ArrayList<Long> checkedvalue = new ArrayList<Long>();

    LessonCustomAdapter(Context context, List<LessonPojo> Lessons2,
            ViewAllLessons viewAllLessons) {
        this.context = context;
        this.Lessons = Lessons2;
        this.viewAllLessons = viewAllLessons;
        for (int i = 0; i < Lessons2.size(); i++) {
            itemChecked.add(i, false); // initializes all items value with false
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Lessons.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = convertView;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            rowView = inflater.inflate(R.layout.lesson_view_row_list, parent,
                    false);
        }

        mLessonDate = (TextView) rowView.findViewById(R.id.LessonDate);
        mLessonDate.setText(Lessons.get(position).getLdate());

        mLessonTitle = (TextView) rowView.findViewById(R.id.LessonTitle);
        mLessonTitle.setText(Lessons.get(position).getLtitle());

        SingleChk = (CheckBox) rowView.findViewById(R.id.singleChk);

        /*
         * if (flag) { System.out.println("66666666666666666666666666666666" +
         * flag); for (int i = 0; i < Lessons.size(); i++) {
         * SingleChk.setChecked(flag); SingleChk.setEnabled(!flag); }
         * 
         * } else { SingleChk.setChecked(itemChecked.get(position)); }
         */
        checkedvalue.clear();
        if (ViewAllLessons.selectAll.isChecked()) {
            SingleChk.setChecked(true);
            Boolean val = false;
            for (int i = 0; i < Lessons.size(); i++) {

                checkedvalue.add(Long.parseLong(Lessons.get(i).getId()));
                System.out
                        .println("ADD SUCCEFULLYYYYYYYY  ======================  "
                                + Long.parseLong(Lessons.get(position).getId()));

            }

        } else {
            SingleChk.setChecked(false);
            checkedvalue.clear();
        }

        rowView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();
                ViewSingleLesson viewSingleLesson = new ViewSingleLesson();

                bundle.putString("ID", Lessons.get(position).getId());
                bundle.putString("TITLE", Lessons.get(position).getLtitle());
                bundle.putString("COURSE", Lessons.get(position).getLcourse());
                bundle.putString("LEVEL", Lessons.get(position).getLlevel());
                bundle.putString("CLASS", Lessons.get(position).getLclass());
                bundle.putString("DATE", Lessons.get(position).getLdate());
                bundle.putString("DESC", Lessons.get(position).getLdesc());
                bundle.putString("ATTACH", Lessons.get(position).getLfilepath());

                viewSingleLesson.setArguments(bundle);
                viewAllLessons.getFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, viewSingleLesson)
                        .commit();
            }
        });

        SingleChk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                Boolean val = false;
                Toast.makeText(context, "hi " + Lessons.get(position).getId(),
                        Toast.LENGTH_LONG).show();
                if (((CheckBox) v).isChecked()) {

                    for (int i = 0; i < checkedvalue.size(); i++) {
                        if (checkedvalue.get(i) == Long.parseLong(Lessons.get(
                                position).getId())) {
                            val = true;
                            System.out
                                    .println("DUPLICATE  ======================  "
                                            + Long.parseLong(Lessons.get(
                                                    position).getId()));
                        } else {
                            val = false;
                        }
                    }
                    if (val == false) {
                        checkedvalue.add(Long.parseLong(Lessons.get(position)
                                .getId()));
                        System.out
                                .println("ADD SUCCEFULLYYYYYYYY  ======================  "
                                        + Long.parseLong(Lessons.get(position)
                                                .getId()));
                    }
                } else {
                    ViewAllLessons.selectAll.setChecked(false);
                    checkedvalue.remove(Long.parseLong(Lessons.get(position)
                            .getId()));
                    System.out
                            .println("Removed SUCCEFULLYYYYYYYY  ======================  "
                                    + Integer.parseInt(Lessons.get(position)
                                            .getId()));
                    // itemChecked.set(position, false);
                }

            }
        });

        return rowView;
    }

}

答案 1 :(得分:0)

今天有机会看看这个,最后整理出来。问题出在我的xml布局设置中。奇怪的是,根据我的git存储库,这个文件在9个月内没有改变,所以我想我做的事情只是偶然起作用。我找到了一个教程FragmentTabHosts,它有不同的布局,并将我的布局更新为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

</LinearLayout>

以某种方式解决了这个问题。更多的搜索在TabActivity文档中显示了FragmentTabHost的示例布局xml:http://developer.android.com/reference/android/app/TabActivity.html

我更新了使用文档中的版本,它也有效:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

鉴于我在Android的文档中找到了它,我会坚持使用这个版本。