Android 安卓:can';t将onclick侦听器应用于自定义阵列适配器内的按钮<;字符串>;
我正在尝试将单击侦听器动态添加到列表视图的自定义阵列适配器中。我的想法是,我有一个列表对象,上面有文本(例如“组1”、“组2”等),然后当我单击该对象时,会出现一个下拉列表(“可展开”部分),上面有两个按钮(“成员”和“设置”)。单击“成员”按钮时,我希望能够访问原始组对象上的特定文本。例如,单击“组1”选项卡下的“成员”可以访问文本“组1” 以下是自定义适配器对象的XML布局:Android 安卓:can';t将onclick侦听器应用于自定义阵列适配器内的按钮<;字符串>;,android,xml,Android,Xml,我正在尝试将单击侦听器动态添加到列表视图的自定义阵列适配器中。我的想法是,我有一个列表对象,上面有文本(例如“组1”、“组2”等),然后当我单击该对象时,会出现一个下拉列表(“可展开”部分),上面有两个按钮(“成员”和“设置”)。单击“成员”按钮时,我希望能够访问原始组对象上的特定文本。例如,单击“组1”选项卡下的“成员”可以访问文本“组1” 以下是自定义适配器对象的XML布局: <?xml version="1.0" encoding="utf-8"?> <Lin
<?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;
}
}
package com.sapphire.view;
导入java.util.List;
导入com.sapphire.model.Mode;
导入com.sapphire.view.ModeAdapter.ModeHolder;
导入android.R;
导入android.app.Activity;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类GroupAdapter扩展了ArrayAdapter{
私人语境;
私有int资源;
公共GroupAdapter(上下文上下文、int资源、int textViewResourceId、,
列出对象){
超级(上下文、资源、textViewResourceId、对象);
this.context=上下文;
这个资源=资源;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(该资源为空);
}
按钮but=(按钮)v.findViewById(R.id.button1);
如果(但是!=null){
但是,setId(位置);
但是.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
System.out.println(“customgroup clicked!”);
}
});
}
返回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);
}
});
}
}
public void displayGroupsOptions(){
最终列表视图lView=(列表视图)thisView.findViewById(R.id.GroupsView);
最终ArrayList groupNameList=新ArrayList();
lView.setAdapter(null);//清空列表视图
//从数据库表中获取所有组
System.out.println(“customgroup即将从数据库获取所有组”);
List allGroups=db.getAllGroups();
如果(!allGroups.isEmpty()){
对于(g组:所有组){
添加(g.getName());
}
//将组arraylist转换为组数组,以便在列表视图的适配器中使用
字符串[]groupNameArray=(字符串[])groupNameList.toArray(新字符串[0]);
GroupAdapter=新的GroupAdapter(getActivity().getApplicationContext(),R.layout.group\u list\u行,R.id.groupName,groupNameList);
lView.setAdapter(适配器);
//创建项目单击侦听器,打开/关闭每个项目的模式选项工具栏
lView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父视图、最终视图、整型位置、长id){
视图组选项=View.findviewbyd(R.id.expandableSubsection);
//为项目创建展开动画
ExpandAnimation ea=新的ExpandAnimation(组选项、动画延迟);
//在工具栏上启动动画
组选项。startAnimation(ea);
}
});
}
}
任何帮助都将不胜感激!我一辈子都搞不懂如何让正确的文本显示在选项卡上(现在它是空白的),以及如何让onclick侦听器真正连接并工作。谢谢 您可以在getView方法中为按钮设置标记,以传递position参数,并在ClickListener中获取标记
btn.setTag(position);
@Override
public void onClick(View v) {
int position = (Integer)v.getTag();
}
甚至可以将同一适配器项中的其他视图设置为标记,并将其传递给侦听器。在适配器类中使用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;
}
作为备注,您应该使用
groupNameList.toArray(新字符串[groupNameList.size()])
而不是groupNameList.toArray(新字符串[0])
来避免额外的对象创建。谢谢,我将尝试一下--mInflater是从何而来的?在编写R.layout.xyz的地方应该引用哪个布局?所以看起来holder.b实际上并没有被设置为按钮。但是我无法访问我想要在R.id中的按钮的布局项,因此它似乎一开始就无法识别布局。。。