片段错误

时间:2016-09-16 06:32:28

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

我是android中的Fragment的新手,我在片段中创建了一个简单的状态列表,但它给出了错误。

这是我的list_item.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mytitle"/>

</RelativeLayout>

这是我的list_fragment.xml

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/empty"/>

</LinearLayout>

这是我的Getter和setter

public class RowItem {
    private String title;

    public RowItem(String title)
    {
        this.title=title;
    }


    public String getTitle()
    {
        return title;
    }

    public void setTitle()
    {
        this.title=title;
    }
}

这是我的CustomAdapter:

public class CustomAdapter extends BaseAdapter {

    Context context;
    List<RowItem> rowItems;

    CustomAdapter(Context context,List<RowItem> rowItems)
    {
        this.context=context;
        this.rowItems=rowItems;
    }
    @Override
    public int getCount() {
        return rowItems.size();
    }

    @Override
    public Object getItem(int i) {
        return rowItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return rowItems.indexOf(getItem(i));
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view==null)
        {
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.list_item,null);
        }

        TextView txtTitle=(TextView) view.findViewById(R.id.mytitle);
        RowItem row_pos=rowItems.get(i);
        txtTitle.setText(row_pos.getTitle());
        return view;
    }
}

最后是MyListFragment类

public class MyListFragment extends ListFragment
{

    CustomAdapter customAdapter;
    private List<RowItem> rowItems;
    public MyListFragment()
    {

    }

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

        return inflater.inflate(R.layout.list_fragment,container,false);
    }

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

        //data
        getStateData();

        customAdapter=new CustomAdapter(getActivity(),rowItems);
        setListAdapter(customAdapter);

    }

    //state web service data
    private void getStateData()
    {
        final ProgressDialog pd=ProgressDialog.show(getActivity(),"Loading State","Please wait",false);

        StringRequest stateRequest=new StringRequest(Request.Method.POST, "http://resalerental.com/webservice/findstate.php"
                , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                pd.dismiss();
                //Toast.makeText(getActivity(), ""+response, Toast.LENGTH_SHORT).show();
                getStateSpinner(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pd.dismiss();
                Toast.makeText(getActivity(), ""+error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue stateQueue= Volley.newRequestQueue(getActivity());
        stateQueue.add(stateRequest);
    }

    private void getStateSpinner(String state)
    {
        ParseJson parseJson=new ParseJson(state);
        parseJson.parseState();

        rowItems=new ArrayList<RowItem>();

       for (int i=0;i<ParseJson.state_name.length;i++)
       {
           RowItem Item=new RowItem(ParseJson.state_name[i]);

           rowItems.add(Item);
       }
    }

}

以上是网站上给出的示例。当我想使用MyFragmentList调用此FragmentManager时,它会给出错误的参数类型传递错误。

我在这里打电话

MyListFragment myListFragment=new MyListFragment();
                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView,myListFragment).commit();

请建议我做什么。 谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

getFragmentManager()替换为代码中的getSupportFragmentManager()

                MyListFragment myListFragment=new MyListFragment();
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView,myListFragment).commit();

如果您使用import android.support.v4.app.Fragment,则必须使用getSupportFragmentManager()表示片段管理器,反之亦然如果您使用android.app.FragmentManager;,则必须使用import android.app.Fragment;表示片段。