2014-04-06 31 views
0

已解决。请参阅下面的singularhum的答案。onCreateView ListView为空。缺少物品


我有一个滑动菜单选项指向一个片段。该片段使用onCreateView扩大了布局。我更新了onActivityCreated中的列表值。这是我的来源。

我的列表视图 Pic

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> 

由于某种原因,我的列表视图变成空的。 查看图片。 正如你所看到的,调试器也具有适配器的一些值。 enter image description here

下面是相关MiniQuoteView类和XML

http://pastebin.com/rRpRA30L http://pastebin.com/YajCCqdJ

+0

可悲的是我的调试器显示的值被取出并准备发球到ListView中 – Sam

+0

张贴MiniQuoteView – Blackbelt

+0

所以portfolio.getQuotes()包含什么? –

回答

1

我相信你的问题是在你MiniQuoteView类的fillData方法。

你的第一个if的说法是:

if (this.quote == null) 

但当quote对象不是null你不必为elseelse if声明。

然后,如果你看一下你的第二个if语句(读取quote数据集来查看)你的第一个里面,它是:

if (this.quote.name != null) 

因为quote是内空,这将永远是正确的那if。这就是为什么你没有看到你的列表视图中的任何内容,因为没有设置显示。

因此,它应该是这样的:

if (this.quote == null) { 
    // displaying your not available message 
} else if (this.quote.name != null){ 
    // your code to display the data 
}