我試圖將點擊監聽器動態添加到列表視圖的自定義陣列適配器。這個想法是,我有他們的文本列表對象(例如「組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監聽器實際連接和工作。謝謝!
正如此言,你應該使用'groupNameList.toArray(新的String [groupNameList。 size()])'而不是'groupNameList.toArray(new String [0])'來避免額外的對象創建。 – Joffrey