2011-05-02 115 views
3

这是我的自定义列表视图布局。Android - 自定义列表视图中的空列表

我添加了ID为“机器人:空”的“空列表”文本视图,但是当列表为空时不显示“空文”视图。

任何建议,请。

更新 - 更新了布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" 
    android:paddingLeft="5dip" android:paddingRight="5dip" 
    android:paddingTop="15dip" android:paddingBottom="15dip" 
    android:cacheColorHint="@color/match_bg2" android:background="@color/match_bg"> 
    <ImageView android:id="@+id/flag_left" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:background="@drawable/flag" 
     android:src="@drawable/flag_default" /> 
    <LinearLayout android:orientation="vertical" 
     android:padding="10dip" android:layout_width="0dip" android:gravity="center" 
     android:layout_weight="1" android:layout_height="fill_parent"> 
     <TextView android:id="@+id/item_match_label" 
      android:layout_width="wrap_content" android:layout_height="0dip" 
      android:layout_weight="1" android:gravity="center" 
      android:textColor="@color/match_fg" android:textStyle="bold" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
      android:focusableInTouchMode="true" 
      /> 
    </LinearLayout> 
    <ImageView android:id="@+id/flag_right" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:background="@drawable/flag" 
     android:src="@drawable/flag_default" /> 

    <TextView android:id="@id/android:empty" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#FF0000" 
       android:text="No data"/> 
</LinearLayout> 

这里是我的列表视图活动

public class LatestMatchesListActivity extends ListActivity { 
... 

private void createMatchList() { 

    /* 
    Log.i(MY_DEBUG_TAG, "Checking Match store size before passing it to custom adapter: "+mStore.size()); 
    if(mStore.size() == 0){ 
     Log.i(MY_DEBUG_TAG, "Got Empty match store, so checking connectivity.."); 
     Match m = new Match(); 
     if(isOnline()){ 
      Log.i(MY_DEBUG_TAG, "Internet is accessible, so adding no matches object: "); 
      m.setId(-1); 
     } else{ 
      Log.i(MY_DEBUG_TAG, "No Internet accessible, so adding mo caonnectivity match object: "); 
      m.setId(-2); 
     } 
     mStore.add(m); 
    } else { 
     Log.e(MY_DEBUG_TAG, "Match store is not empty "); 
    } 
    */ 
    //mStore.clear(); 
    Log.i(MY_DEBUG_TAG, "Passing match list to custom adapter: "+mStore.toString()); 
    this.mAdapter = new MatchAdapter(this, R.layout.list_matches, mStore); 
    this.setListAdapter(this.mAdapter); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setBackgroundDrawable(null); 
    lv.setBackgroundResource(0); 

} 

和自定义ArrayAdapter

public class MatchAdapter extends ArrayAdapter<Match> { 
    private ArrayList<Match> items; 
    private final String MY_DEBUG_TAG = "MatchAdapter"; 

    public MatchAdapter(Context context, int textViewResourceId, ArrayList<Match> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.list_matches, null); 
     } 
     Match m = items.get(position); 
     if (m != null) { 
      TextView itemLabel = (TextView) v.findViewById(R.id.item_match_label); 
      ImageView imageFlagLeft = (ImageView) v.findViewById(R.id.flag_left); 
      ImageView imageFlagRight = (ImageView) v.findViewById(R.id.flag_right); 

      if(m.getId() == -1){ 
       itemLabel.setText("No Live Matches."); 
       imageFlagLeft.setVisibility(View.INVISIBLE); 
       imageFlagRight.setVisibility(View.INVISIBLE); 
      } else if(m.getId() == -2){ 
       itemLabel.setText("No Intenet connectivity."); 
       imageFlagLeft.setVisibility(View.INVISIBLE); 
       imageFlagRight.setVisibility(View.INVISIBLE); 
      } else { 
       itemLabel.setText(m.getTeam1() + " v/s " + m.getTeam2()); 
       imageFlagLeft.setImageResource(getFlagResource(m.getTeam1())); 
       imageFlagRight.setImageResource(getFlagResource(m.getTeam2())); 
      } 

     } 
     return v; 
    } 

    private int getFlagResource(String teamName) { 
     Log.i(MY_DEBUG_TAG, "Getting the flag of " + teamName); 
     String flagString = "flag_" + teamName.replace(" ", "_").toLowerCase(); 
     int resId = getContext().getResources().getIdentifier(flagString, "drawable", "com.itflux.DroidChirp"); 
     if (resId != 0) { 
      return resId; 
     } 
     return R.drawable.flag_default; 
    } 
} 
+0

textview没有显示在您的布局? – vnshetty 2011-05-02 05:28:09

+0

是@vnshetty,列表为空时不显示文本视图。 – 2011-05-02 05:30:30

+0

你可以在图形布局中看到你的textview吗? – vnshetty 2011-05-02 08:35:31

回答

3

你有你的ListView适合屏幕,并在LinearLayout您的TextView显示在下那ListView所以它是不可见的(这将是,如果你使用ScrollView)。

例如,您需要使用作为布局的根元素RelativeLayout使TextView位于ListView之上。

+0

我试着用'RelativeLayout'包装我的布局,并且像你提到的那样移动了'TextView',当前代码可以在http://pastebin.com/xFBqErgq查看,但是空的文本视图仍然没有显示。 – 2011-05-02 06:10:48

+0

你怎么触发'TextView'出现?你可以分享相关的代码作为你的问题的扩展吗?谢谢! – rekaszeru 2011-05-02 06:47:18

+0

请看到更新的问题 – 2011-05-02 07:00:47

1

使用@ + ID空和@id列表:机器人:ID = “@ + ID /安卓:空”

+0

@Sandy添加+的ID,它并没有帮助 – 2011-05-02 06:03:24

+0

它应该工作,你在哪里使用列表您可以发布您的Java代码? – Sandy 2011-05-02 06:33:44

+0

@Sandy请参阅更新后的问题 – 2011-05-02 07:00:25

0

你的问题是与ListView height属性。将其设置为wrap_content

+0

请参阅最新的问题 – 2011-05-02 07:01:14

2

好像这个问题仍然没有解决。所以这是我如何解决它。我触发的功能,这将基本刷新列表布局每当数据如下变化:

ListView lv = getListView(); 
    lv.requestLayout(); 
    if(aa.arrayValue.size() > 0) { 
     lv.setVisibility(ListView.VISIBLE); 
     setListAdapter(new Adaptor(this, R.layout.main_list_item, aa.arrayValue)); 
    } 
    else { 
     lv.setVisibility(ListView.INVISIBLE); 
     TextView tv = (TextView) findViewById(android.R.id.empty); 
     tv.requestLayout(); 
     tv.setVisibility(TextView.VISIBLE); 
    } 

希望这可以帮助你!