2014-04-29 29 views
0

我試圖將點擊監聽器動態添加到列表視圖的自定義陣列適配器。這個想法是,我有他們的文本列表對象(例如「組1」,「組2」等),然後當我點擊對象下拉出現(「expandable_section」)上有兩個按鈕( 「成員」和「設置」)。當我點擊成員按鈕時,我希望能夠訪問原始組對象上的特定文本。例如,點擊「第1組」標籤下的「成員」可以訪問文本「第1組」。Android:不能將onclick監聽器應用於自定義陣列適配器內的按鈕<String>

下面是定製適配器對象我的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" > 

    <!-- The list element for GROUP SETTINGS tab sub-section --> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:paddingBottom="10dp" 
     android:paddingRight="10dp" 
     android:paddingTop="10dp" > 

     <ImageView 
      android:id="@+id/activeIcon" 
      android:layout_width="34dp" 
      android:layout_height="34dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="-7dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center_vertical" /> 

     <ImageView 
      android:id="@+id/modeIcon" 
      android:layout_width="34dp" 
      android:layout_height="34dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginBottom="5dp" 
      android:layout_marginRight="15dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center_vertical" /> 

     <TextView 
      android:id="@+id/groupName" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginBottom="5dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center_vertical" 
      android:textColor="#000000" 
      android:textSize="22dp" /> 
    </LinearLayout> 


    <!-- SPECIAL HIDDEN TOOLBAR FOR DEMO LIBRARY LIST MODES TODO: Change to whitelist/blacklist GROUP TAB! --> 
    <!-- EXPANDABLEFOR GROUP-SETTINGS FRAGMENT --> 

    <LinearLayout 
     android:id="@+id/expandableSubsection" 
     android:layout_width="fill_parent" 
     android:layout_height="90dp" 
     android:layout_marginBottom="-100dp" 
     android:background="#dddddd" 
     android:gravity="center" 
     android:visibility="gone" > 

     <Button 
      android:id="@+id/groupMembersButton" 
      android:layout_width="150dp" 
      android:layout_height="40dp" 
      android:layout_marginRight="20dp" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Members" /> 
     <Button 
      android:id="@+id/groupSettingsButton" 
      android:layout_width="150dp" 
      android:layout_height="40dp" 
      android:layout_marginRight="20dp" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Settings" /> 
    </LinearLayout> 

</LinearLayout> 

這裏是定製適配器代碼:

package com.sapphire.view; 

import java.util.List; 

import com.sapphire.model.Mode; 
import com.sapphire.view.ModeAdapter.ModeHolder; 

import android.R; 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class GroupAdapter extends ArrayAdapter<String>{ 
private Context context; 
private int resource; 

public GroupAdapter(Context context, int resource, int textViewResourceId, 
     List<String> objects) { 
    super(context, resource, textViewResourceId, objects); 
    this.context = context; 
    this.resource = resource; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(this.resource, null); 
    } 

      Button but= (Button) v.findViewById(R.id.button1); 

      if(but!=null){ 
       but.setId(position); 
       but.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         System.out.println("customgroup clicked !"); 
        }       
        }); 

      } 
    return v; 

} 
} 

,這裏是在自定義適配器實際上是用來顯示列表的組:

public void displayGroupsOptions(){ 
    final ListView lView = (ListView) thisView.findViewById(R.id.GroupsView); 
final ArrayList<String> groupNameList = new ArrayList<String>(); 
lView.setAdapter(null); //empty the list view 
//Get all groups from database table 
System.out.println("customgroup about to get all groups from database"); 
    List<Group> allGroups = db.getAllGroups(); 
    if(!allGroups.isEmpty()){ 
     for (Group g: allGroups){ 
      groupNameList.add(g.getName()); 
     } 
     //Convert group arraylist to group array for use in adapter for list view 
     String[] groupNameArray = (String[]) groupNameList.toArray(new String[0]); 

     GroupAdapter adapter = new GroupAdapter(getActivity().getApplicationContext(), R.layout.group_list_row, R.id.groupName, groupNameList); 
     lView.setAdapter(adapter); 
     // Creating an item click listener, to open/close the mode options toolbar for each item 
     lView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 

       View groupOptions = view.findViewById(R.id.expandableSubsection); 
       // Creating the expand animation for the item 
       ExpandAnimation ea = new ExpandAnimation(groupOptions, ANIMATION_DELAY); 
       // Start the animation on the toolbar 
       groupOptions.startAnimation(ea); 

      } 
     }); 

    } 
    } 

任何幫助,將不勝感激!我只是無法弄清楚我的生活如何獲得適當的文本顯示在選項卡上(現在它是空白的)以及如何使onclick監聽器實際連接和工作。謝謝!

+0

正如此言,你應該使用'groupNameList.toArray(新的String [groupNameList。 size()])'而不是'groupNameList.toArray(new String [0])'來避免額外的對象創建。 – Joffrey

回答

0

您可以在getView方法中爲您的Button設置標記以傳遞位置參數,並獲取ClickListener中的標記。

btn.setTag(position); 

@Override 
public void onClick(View v) { 
    int position = (Integer)v.getTag(); 
} 

即使你可以在同一個適配器項目爲標籤設置其他視圖,並把它傳遞給聽衆。

0

使用這段代碼,您使用的getView適配器類

public class ViewHolder { 
    Button b; 
    } 

而且這個

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
final ViewHolder holder; 
if (convertView == null) { 
    holder = new ViewHolder(); 

convertView = mInflater.inflate(R.layout.xyz, 
       parent, false); 

holder.b = (Button) convertView.findViewById(R.id.button1); 
     convertView.setTag(holder); 
} 
else{ 
holder = (ViewHolder) convertView.getTag(); 
} 
Item it = item.get(position); 
holder.b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


        System.out.println("customgroup clicked !"); 
       }       
       }); 


return convertView; 
} 
+0

謝謝我會試試這個 - 來自和項目的mInflater是什麼?在你寫R.layout.xyz的地方應該引用哪個佈局? – ekofman

+0

因此,似乎holder.b實際上並未設置爲按鈕。但是我不能在R.id中訪問我想要的按鈕的佈局項目,所以它似乎並沒有首先識別佈局。 – ekofman