已解决。请参阅下面的singularhum的答案。onCreateView ListView为空。缺少物品
我有一个滑动菜单选项指向一个片段。该片段使用onCreateView
扩大了布局。我更新了onActivityCreated
中的列表值。这是我的来源。
我的列表视图
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
this.dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
this.portfolio = new Portfolio();
this.portfolio.addCompanyNoUpdate("YHOO");
// Specify the fragment layout file for this fragment class
View view = inflater.inflate(R.layout.fragment_stock, container, false);
progressText = (TextView) view.findViewById(R.id.progressTextView);
// Assign our CursorAdapter
adapter = new QuoteAdapter(getActivity(), portfolio.getQuotes());
ListView list = (ListView) view.findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
然后我有onActivityCreated
只是在做更新。
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.update();
}
我的更新类
private void update() {
// If a network connection is available, update the stock information
if (Utils.isNetworkAvailable(getActivity())) {
// Start a custom task that downloads the data on another thread so the UI does not lock up
new UpdateQuotesTask().execute(portfolio);
}
else {
// Otherwise display an error message to the user
this.progressText.setText(this.getResources().getString(R.string.no_internet_connection));
}
}
我的适配器
public class QuoteAdapter extends BaseAdapter {
private Context context;
private List<Quote> quotes;
public QuoteAdapter(Context c, List<Quote> quotes) {
this.context = c;
this.quotes = quotes;
}
public void addQuote(Quote q) {
this.quotes.add(q);
}
@Override
public int getCount() {
return this.quotes.size();
}
@Override
public Object getItem(int position) {
return this.quotes.get(position);
}
public long getItemId(int position) {
// We can just return a default value for this adapter
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MiniQuoteView miniQuoteView = new MiniQuoteView(this.context, this.quotes.get(position));
miniQuoteView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT));
return miniQuoteView;
}
public void removeItem(int position) {
this.quotes.remove(position);
}
}
,最后我的XML作为标准的ListView
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/progressTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/none" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:verticalSpacing="2dp"
android:layout_below="@+id/progressTextView" />
</RelativeLayout>
由于某种原因,我的列表视图变成空的。 查看图片。 正如你所看到的,调试器也具有适配器的一些值。
下面是相关MiniQuoteView类和XML
http://pastebin.com/rRpRA30L http://pastebin.com/YajCCqdJ
可悲的是我的调试器显示的值被取出并准备发球到ListView中 – Sam
张贴MiniQuoteView – Blackbelt
所以portfolio.getQuotes()包含什么? –