自定义ListView不显示

时间:2014-06-09 00:58:14

标签: android android-fragments android-listview

我正在尝试使用ListView内的自定义Fragment同时使用自定义CursorAdapter,但到目前为止,我无法显示ListView

我做错了什么?

我的Fragment

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class MyListFragment extends Fragment {

    private CustomcursorAdapter mCustomcursorAdapter;
    private ListView mListView;

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

        //CursorAdapter
        mCustomcursorAdapter = new CustomcursorAdapter(view.getContext(), null, 0);

        //ListView
        mListView = (ListView) view.findViewById(R.id.itemslist);
        mListView.setAdapter(mCustomcursorAdapter);

        return view;
    }

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

我的主要活动

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getFragmentManager();
        if (fragmentManager.findFragmentById(android.R.id.content) == null) {
            MyListFragment myListFragment = new MyListFragment();
            fragmentManager.beginTransaction().add(android.R.id.content, myListFragment).commit();
        }
    }
}

CustomcursorAdapter

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class CustomcursorAdapter extends CursorAdapter {

    private LayoutInflater mLayoutInflater;

    public CustomcursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView textview = (TextView) view.findViewById(R.id.itemTextView);
        textview.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME_ITEM)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mLayoutInflater.inflate(R.layout.itemslist, parent, false);
    }
}

1 个答案:

答案 0 :(得分:0)

这就是我正在做的事情。

在片段中,我正在onActivityCreated()中执行所有操作:

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

        View campainview = inflater.inflate(R.layout.campaignview, container, false);

        return campainview;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);

            String[] s = {"a","b","c","d","b","c","d","b","c","d","b","c","d","b","c","d","b","c","d"};

            ArrayList<String> aList = new ArrayList<String>();

            for(String s1:s){

                aList.add(s1);

            }

            ListView lv1= (ListView)getActivity().findViewById(R.id.campaignListView);
            lv1.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,aList));

        }

这项工作对我而言,基本上你可以做的是,你可以使用游标适配器而不是使用这个适配器。如果它仍然无法正常工作,那么问题在于光标适配器,或者可能是来自数据库的值为null。

让我知道这是否有效..祝你好运.. :)