Android:无法将onclick侦听器应用于自定义arrayadapter <string> </string>中的按钮

时间:2014-04-29 07:24:11

标签: android xml

我尝试将点击侦听器动态添加到自定义数组适配器以获取列表视图。我的想法是我有列表对象,上面有文字(例如&#34;第1组&#34;,&#34;第2组&#34;等等),然后当我点击对象时出现一个下拉菜单( &#34; expandable_section&#34;)上面有两个按钮(&#34;会员&#34;&#34;设置&#34;)。当我单击成员按钮时,我希望能够访问原始组对象上的特定文本。例如,点击&#34;会员&#34;根据&#34;第1组&#34;选项卡可以让我访问文本&#34;第1组。&#34;

以下是自定义适配器对象的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监听器实际附加和工作。谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在getView方法中为Button设置标记以传递position参数,并在ClickListener中获取标记。

btn.setTag(position);

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

即使您可以在同一个适配器项目中将其他视图设置为标记,也可以将其传递给侦听器。

答案 1 :(得分: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;
}