-2

我是新来的碎片在android和我创建一个简单的状态列表的片段,但它会给出一个错误。在片段错误中的自定义列表视图

这是我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); 
     } 
    } 

} 

以上是例如在网站上给出。当我想使用FragmentManager来调用MyFragmentList时,它给了我错误的参数类型错误。

这里,我怎么叫

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

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

+1

,请复制粘贴你的错误也 –

+0

每当我称之为使用FragmentManager它给我的错误,那错误的参数类型通 – Manish

+0

分享您的logcat日志以及 – Anjali

回答

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,你必须,如果你使用android.app.FragmentManager;必须使用import android.app.Fragment;的片段使用getSupportFragmentManager()用于段管理,反之亦然。

+0

其实我在一个片段活动里面调用.... 。和我使用getSupportFragmentManager但错误仍然相同,也...谢谢 – Manish

+0

检查更新的答案 – Anjali

+0

我使用** FragmentManager fragmentManager = getActivity()。getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.containerView,myListFragment).commit(); ** – Manish