2012-12-07 80 views
0

我喜歡在listView中沒有項目時顯示textView,而當listView中有項目時textView不會顯示。我的問題是即使listView中有項目,textView仍然會在短時間內顯示,然後將項目加載到listView中。那麼,如何在ListView中有項目時使TextView不可見?如何在ListView中有項目時使TextView不可見?

這裏是代碼:

public void onCreate(Bundle savedInstancesState){ 
    super.onCreate(savedInstancesState); 
    setContentView(R.layout.list_screen); 
    user = getIntent().getExtras().getString("user"); 
    Log.d("dg",user); 
    getList(); 
    lv = (ListView) findViewById(android.R.id.list); 
    emptyText = (TextView)findViewById(android.R.id.empty); 
    lv.setEmptyView(emptyText);  
} 

public void getList(){ 
    new Thread(){ 
     public void run(){ 
      try{ 
       httpclient = new DefaultHttpClient(); 
       httppost = new HttpPost("http://www.kryptoquest.com/tracker/friendlist.php"); 
       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
       nameValuePairs.add(new BasicNameValuePair("Username", user)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       response = httpclient.execute(httppost); 
       is = response.getEntity().getContent(); 
      }catch(Exception e){ 
       Log.e("log_tag", "Error:"+e.toString()); 
      } 

      //convert response to string 
      try{ 
        reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
        sb = new StringBuilder(); 
        line = null; 
        while ((line = reader.readLine()) != null) { 

          sb.append(line + "\n"); 

        } 
        Log.d("test",sb.toString()); 
        is.close(); 

        result = sb.toString(); 

        result = result.substring(0, result.length()-1);      
       // Log.d("result",result); 
        friend = new ArrayList<String>(Arrays.asList(result.split("[*]"))); 
        Log.d("size",String.valueOf(friend.size())); 
        runOnUiThread(new Runnable() 
        { 
         public void run(){ 
          adapter = new ArrayAdapter<String>(ThirdActivity.this,android.R.layout.simple_list_item_1,friend); 
          setListAdapter(adapter); 
         } 
        }); 


      }catch(Exception e){ 
        Log.e("log_tag", "Error converting result "+e.toString()); 
      } 
     } 
    }.start(); 
} 

list_screen.xml

<?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="fill_parent" 
    > 

<ListView android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

<TextView android:id="@+id/android:empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="@string/no_friend" 
      android:gravity="center_vertical|center_horizontal"/> 

</LinearLayout> 
+0

什麼的GetList();?它返回任何價值。 –

+2

你可以通過把條件if(getList.size()> 0)來檢查,然後顯示listview否則顯示textView –

+0

我在這裏回答了類似的問題: http://stackoverflow.com/questions/8212137/change-color -of-的項功能於列表視圖與 - simpleadapter/17124508#17124508 這裏: http://stackoverflow.com/questions/12374226/setting-tag-for-listview-item - 當-使用-simpleadapter/17124755#17124755 – Gene

回答

0

我的問題是,即使有在ListView項目,TextView的依然會顯示在很短的時間,然後將這些項目加載到listView中。

不要在onCreate()使用setEmptyView(),但在你的XML添加android:visibility="gone"到的TextView和調用setAdapter()後使用setEmptyView()

adapter = new ArrayAdapter<String>(ThirdActivity.this,android.R.layout.simple_list_item_1,friend); 
setListAdapter(adapter); 
lv.setEmptyView(emptyText);  
0

你可以做到這一點簡單的用if聲明類似

if (listView.getSize() > 0){ 
    textView.setVisibility(View.GONE); 
    //or use 
    textView.setVisibility(View.INVISIBLE); 
} else { 
    //your list is empty 
    textView.setVisibility(View.VISIBLE); 
} 
詛咒

你需要添加adapterListView

相關問題