2011-04-04 139 views
1

我想要一個不可見的列表視圖 - 我需要撥打list.setVisibility(View.INVISIBLE),但我已經使用了ListActivity,並且在我的代碼中沒有ListView隱藏ListView

如何調用此函數或獲取此屬性? 請幫我實現這個目標。

public class list extends ListActivity { 
ListView lst; 
    private LayoutInflater mInflater; 
    private Vector<RowData> data; 
    RowData rd; 
    static final String[] title = new String[] { 
      "*New*Apple iPad Wi-Fi (16GB)", "7 Touch Tablet -2GB Google Android", 
    "Apple iPad Wi-Fi (16GB) Rarely Used ","Apple iPad Wi-Fi (16GB) AppleCase"  }; 
    static final String[] detail = new String[] { 
      "1h 37m Shipping: $10.00","1h 39m Shipping: Free","58m 6s Shipping: $10.00","59m 30s Shipping: $10.95" }; 
    private Integer[] imgid = { 
     R.drawable.icon,R.drawable.icon,R.drawable.icon, 
     R.drawable.icon 
    }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    lst=(ListView)findViewById(R.id.list); 
    mInflater = (LayoutInflater) getSystemService(
    Activity.LAYOUT_INFLATER_SERVICE); 
    data = new Vector<RowData>(); 
    for(int i=0;i<title.length;i++){ 
    try { 
     rd = new RowData(i,title[i],detail[i]); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     data.add(rd); 
    } 
     CustomAdapter adapter = new CustomAdapter(this, R.layout.list, 
             R.id.title, data); 
     setListAdapter(adapter); 
     //getListView().setTextFilterEnabled(true); 
    } 
     public void onListItemClick(ListView parent, View v, int position, 
                    long id) {   
     Toast.makeText(getApplicationContext(), "You have selected " 
         +(position+1)+"th item", Toast.LENGTH_SHORT).show(); 
    } 
      private class RowData { 
      protected int mId; 
      protected String mTitle; 
      protected String mDetail; 
      RowData(int id,String title,String detail){ 
      mId=id; 
      mTitle = title; 
      mDetail=detail; 
     } 
      @Override 
      public String toString() { 
        return mId+" "+mTitle+" "+mDetail; 
      } 
    } 
     private class CustomAdapter extends ArrayAdapter<RowData> { 
     public CustomAdapter(Context context, int resource, 
          int textViewResourceId, List<RowData> objects) {    
    super(context, resource, textViewResourceId, objects); 
    } 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder = null; 
      TextView title = null; 
      TextView detail = null; 
      ImageView i11=null; 
      RowData rowData= getItem(position); 
      if(null == convertView){ 
       convertView = mInflater.inflate(R.layout.list, null); 
       holder = new ViewHolder(convertView); 
       convertView.setTag(holder); 
    } 
       holder = (ViewHolder) convertView.getTag(); 
       title = holder.gettitle(); 
       title.setText(rowData.mTitle); 
       detail = holder.getdetail(); 
       detail.setText(rowData.mDetail);              
       i11=holder.getImage(); 
       i11.setImageResource(imgid[rowData.mId]); 
       return convertView; 
    } 
       private class ViewHolder { 
       private View mRow; 
       private TextView title = null; 
       private TextView detail = null; 
       private ImageView i11=null; 
       public ViewHolder(View row) { 
       mRow = row; 
    } 
      public TextView gettitle() { 
       if(null == title){ 
        title = (TextView) mRow.findViewById(R.id.title); 
        } 
       return title; 
      }  
      public TextView getdetail() { 
       if(null == detail){ 
         detail = (TextView) mRow.findViewById(R.id.detail); 
         } 
       return detail; 
      } 
      public ImageView getImage() { 
       if(null == i11){ 
         i11 = (ImageView) mRow.findViewById(R.id.img); 
              } 
        return i11; 
      } 
     } 
     } } 
+0

請在添加您的代碼之前按編輯器上的ctrl + k。那麼它將被很好地看到,在這裏沒有什麼是明確的。 – 2011-04-04 21:38:36

+0

我編輯了這個問題,使其更清晰。該修訂版一經同行評審後即可見。 – 2011-04-04 21:39:59

回答

2

裏面一個ListActivity你可以打電話getListView(),這樣你就可以基本上做到:

getListView().setVisibility(View.INVISIBLE); 
+0

感謝reply.its正常工作 – Ritesh 2011-04-05 04:46:19

2

ListActivity隱藏你的ListView,但你可以通過調用getListView()得到它。 所以你可以有add方法您ListActivity

void setListVisible (boolean isVisible) { 
    getListView().setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE); 
} 

呼叫setListVisible(false)以隱藏表格,並setListVisible(true)顯示列表。

isVisible ? View.VISIBLE : View.INVISIBLE行將可見性設置爲View.VISIBLE如果isVisible == true,否則將其設置爲View.INVISIBLE

+0

嗨,感謝它現在的工作感到抱歉,對於最近的回覆 – Ritesh 2011-04-24 04:47:12

-1
Listview list=(ListView) findviewbyid(R.id.list); 
list.setVisibilty(View.Gone); 
+0

請問你能解釋一下嗎?謝謝。 – rapvelopment 2014-12-20 19:30:34

+0

第一個列表視圖id從xml文件中獲取它,然後列表對象設置可見性,如View.Gone或view.visible。已經用於隱藏,可見用於隱藏它的任何事物。 – sandhu 2014-12-24 09:10:39

+0

list.setVisibility(View.INVISIBLE); //用於隱藏 – sandhu 2014-12-25 20:53:01