我已经尝试了一段时间,但我似乎仍然无法做到正确。我可以向列表视图添加自定义标题,但无法向列表添加自定义行布局。我希望列表中的每一行都具有文件“listitemrow.xml”的布局,但尽管阅读了多个教程,但我似乎不太明白如何做到这一点。
这是我的代码
public class PortfolioFragMent extends android.app.ListFragment{
private String[] ShareholdingNames;
private ListView mainListView ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ListView listView = new ListView(getActivity());
super.onCreate(savedInstanceState);
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, listView, false);
listView.addHeaderView(header, null, false);
ViewGroup listrow = (ViewGroup)inflater.inflate(R.layout.listitemrow, listView, false);
//listView.add(listitemrow); I want to do something like this
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1);
setListAdapter(array);
ShareholdingNames= ShareholdingNames();
for (String str : ShareholdingNames)
array.add(str);
listView.setAdapter(array);
return listView;
}
private String[] ShareholdingNames(){
ShareholdingNames = new String[Portfolio.getPortfolio().count()];
for(int i=0; i < Portfolio.getPortfolio().count(); i++){
ShareholdingNames[i]= Portfolio.getPortfolio().getShareHolding(i).getName();
}
return ShareholdingNames;
}
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(),
DetailShareHoldingActivity.class);
intent.putExtra("new_variable_name","value");
intent.putExtra("bookPositionInList",position);
startActivity(intent);
}
}