【问题标题】:How to make the TextView invisible when there are items in listView?当listView中有项目时如何使TextView不可见?
【发布时间】:2012-12-07 05:00:10
【问题描述】:

我喜欢在 listView 中没有项目时显示 textView 而当 listView 中有项目时 textView 不会显示。我的问题是即使listView中有项目,textView仍然会在短时间内显示,然后将项目加载到listView中。那么,当listView中有item时,如何让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>

【问题讨论】:

标签: android listview textview


【解决方案1】:

我的问题是即使listView中有item,textView还是会在短时间内显示出来,然后将item加载到listView中。

不要在onCreate() 中使用setEmptyView(),而是将android:visibility="gone" 添加到XML 中的TextView 并在调用setAdapter() 之后使用setEmptyView()

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

【讨论】:

    【解决方案2】:

    您可以使用类似if 的语句来简单地完成此操作

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

    你需要在将adapter 添加到ListView 后调用它

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        如果有一个有用方法的列表项,你可以隐藏 textView: AdapterView.setEmptyView(查看emptyView)

        这是一个例子: 列表视图列表视图; 文本视图文本视图;

        listView.setEmptyView(textView); 这将在列表中有项目时自动隐藏textView,并在listView为空时显示textView!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-28
          • 1970-01-01
          • 2014-09-25
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 1970-01-01
          相关资源
          最近更新 更多