2015-04-21 57 views
0

我正在使用我正在使用列表片段的Android應用程序。我在佈局的頂部添加了一個按鈕。我的問題是它在每個列表選項中重複。我只希望它在INDIA選項列表的頂部只顯示一次。Android中的Listfragment按鈕顯示

我與花絮代碼給出如下:

public class CountryList extends ListFragment { 


    Button btn1; 
    // Array of strings storing country names 
    String[] countries = new String[] { 
      "India", 
      "Pakistan", 
      "Sri Lanka", 
      "China", 
      "Bangladesh", 
      "Nepal", 
      "Afghanistan", 
      "North Korea", 
      "South Korea", 
      "Japan" 
    };  

    // Array of integers points to images stored in /res/drawable/ 
    int[] flags = new int[]{ 
      R.drawable.india, 
      R.drawable.pakistan, 
      R.drawable.srilanka, 
      R.drawable.china, 
      R.drawable.bangladesh, 
      R.drawable.nepal, 
      R.drawable.afghanistan, 
      R.drawable.nkorea, 
      R.drawable.skorea, 
      R.drawable.japan  
    }; 

    // Array of strings to store currencies 
    String[] currency = new String[]{ 
     "Indian Rupee", 
     "Pakistani Rupee", 
     "Sri Lankan Rupee", 
     "Renminbi", 
     "Bangladeshi Taka", 
     "Nepalese Rupee", 
     "Afghani", 
     "North Korean Won", 
     "South Korean Won", 
     "Japanese Yen" 
    }; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 


     // Each row in the list stores country name, currency and flag 
     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();   

     for(int i=0;i<10;i++){ 
      if(countries[i]=="India"){ 

      } 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", "Country : " + countries[i]); 
      hm.put("cur","Currency : " + currency[i]); 
      hm.put("flag", Integer.toString(flags[i]));    
      aList.add(hm);   
     } 

     // Keys used in Hashmap 
     String[] from = { "flag","txt","cur" }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag,R.id.txt,R.id.cur};   

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);  

     setListAdapter(adapter); 

     return super.onCreateView(inflater, container, savedInstanceState);  
    } 

} 

我的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" 
    android:orientation="vertical"  
    > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Button" /> 

    <ImageView 
      android:id="@+id/flag" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:contentDescription="HELLO WORLD" 
      android:layout_gravity="center" 
      /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" >  

      <TextView 
       android:id="@+id/txt" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15dp" />   

      <TextView 
       android:id="@+id/cur" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="10dp" /> 

     </LinearLayout> 

</LinearLayout> 

enter image description here

+1

你必須創建爲 –

回答

2

我想你已經爲你的清單做了一個自定義佈局。

您正在使用SimpleAdapter。但是對於您的要求,您需要製作一個自定義適配器,您可以將其實施爲SimpleAdapterBaseAdapter。在該課程中,您將找到一種getView()方法,在該方法中,您可以膨脹自定義視圖並檢查以下情況。

if(countries[position].eqauls("India")) { 
     yourButton.setVisibility(View.VISIBLE); 
    }else{ 
     yourButton.setVisibility(View.GONE); 
    } 
+0

可以請你按照我的條件提供我要在這裏自定義適配器代碼段。我是開發新手,這就是爲什麼我沒有太多的自定義適配器的經驗。 –

+1

請參閱這些鏈接http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/,http://www.pcsalt.com/android/listview -using-baseadapter-android/ – Piyush

+0

它的工作原理,我用這個鏈接,並製作一個自定義適配器,它的工作原理。我只想知道多一個想我想讓我的按鈕來點擊偵聽器來打開一個活動,我想通過startActivity來做到這一點,但在這一點上它給出了一個錯誤:startActivity不在BaseAdapter的範圍內..你能否向我解釋這件事,如果我按下這個按鈕,我想做任何事情。 –

0

代替row_layout添加按鈕..與該按鈕建立新的XML只有..將此佈局添加爲listView的headerView。 這將在列表視圖的頂部添加按鈕。 讓我知道如果問題出現,任何! 歡呼!

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 


     // Each row in the list stores country name, currency and flag 
     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();   

     for(int i=0;i<10;i++){ 
      if(countries[i]=="India"){ 

      } 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", "Country : " + countries[i]); 
      hm.put("cur","Currency : " + currency[i]); 
      hm.put("flag", Integer.toString(flags[i]));    
      aList.add(hm);   
     } 

     // Keys used in Hashmap 
     String[] from = { "flag","txt","cur" }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag,R.id.txt,R.id.cur};   

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);  
View headerView; 
headerView = inflater.inflate(R.layout.list_header, null,false); 
getListView().addHeaderView(mHeaderView); 
     setListAdapter(adapter); 

     return super.onCreateView(inflater, container, savedInstanceState);  
    } 
+0

自定義適配器和list_header與按鈕有佈局.. –