1

我有一個包含自定義列表視圖的活動。將自定義列表視圖添加到片段

它有一個自定義數組適配器(作爲另一個java文件)和一個setter和getter類(作爲另一個java文件)。 我想把這個自定義列表視圖放入一個片段,但我總是得到錯誤。 這是我之前主要活動代碼: 公共類MainActivity擴展活動實現OnClickListener {

private ListView listView; 
private List<NameBean> items; 
private NamesAdapter objAdapter = null; 
private Button btnGetSelected; 
public static final String[] titles = new String[] { "Strawberry", 
    "Banana", "Orange", "Mixed" }; 
String CheckedItems; 

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

    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, 
      R.array.spinner_array, android.R.layout.simple_spinner_item); 
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinnerAdapter.setNotifyOnChange(true); 

    listView = (ListView) findViewById(R.id.listview); 
    btnGetSelected = (Button) findViewById(R.id.btnget); 
    btnGetSelected.setOnClickListener(this); 

    items = new ArrayList<NameBean>(); 
    for (int i = 0; i < titles.length; i++) { 
     NameBean nb = new NameBean(); 
     nb.setName(titles [i]); 
     nb.setSpinnerAdapter(spinnerAdapter); 
     items.add(nb); 
    } 
    objAdapter = new NamesAdapter(MainActivity.this, items); 
    listView.setAdapter(objAdapter); 
    listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      ... 
     } 
    }); 
} 

@Override 
public void onClick(View v) { 

    ... 
     } 
    } 
} 

}

這是我要如何使用它裏面的片段:

公共類的內容延伸片段{

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

} 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_content, container, false); 

    return view; 
} 
public void setThings(ListAdapter adapter) { 
    ListView listView = (ListView) getView().findViewById(R.id.listview); 
    listView.setAdapter(adapter); 
    } 

}

我定義的另一個片段中,這些參數(在同一個活動):

public class MyListFragment extends Fragment { 

private ListView listView; 
private List<NameBean> items; 
private NamesAdapter objAdapter = null; 
public static final String[] titles = new String[] { "Strawberry", 
    "Banana", "Orange", "Mixed" }; 
String CheckedItems; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_items, 
      container, false); 
    Button button = (Button) view.findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      updateDetail(); 
     } 
    }); 
    return view; 
} 

// May also be triggered from the Activity 
public void updateDetail() { 
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), 
      R.array.spinner_array, android.R.layout.simple_spinner_item); 
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinnerAdapter.setNotifyOnChange(true); 
    items = new ArrayList<NameBean>(); 
    for (int i = 0; i < titles.length; i++) { 
     NameBean nb = new NameBean(); 
     nb.setName(titles [i]); 
     nb.setSpinnerAdapter(spinnerAdapter); 
     items.add(nb); 
    } 
    objAdapter = new NamesAdapter(getActivity(), items); 
    Content fragment = (Content) getFragmentManager() 
      .findFragmentById(R.id.detailFragment); 
    if (fragment != null && fragment.isInLayout()) { 
     fragment.setThings (objAdapter); 
    } 
} 

}

我不知道是什麼問題。請幫忙。

+0

如果你甚至沒有說出你的錯誤是什麼,你不可能得到幫助。我們也不知道問題是什麼。 – rds

回答

0

我不知道這是否是足夠的,但是:

  1. Content類應該擴展ListFragment,不只是片段。默認情況下,ListFragment有一個包含ListView的視圖層次結構,沒有別的;如果您想提供自己的佈局,那很好,但您在該佈局中的ListView必須具有ID @id/android:list。查看ListFragment javadoc。

  2. 在你setThings(ListAdapter)方法,你應該通過ListAdapter到片段的setListAdapter()方法,而不是直接向ListView,就像這樣:

    public void setThings(ListAdapter adapter) { 
        setListAdapter(adapter); 
    } 
    

希望這有助於。

相關問題