0

我正在创建一个自定义适配器,以便可以在 listView 中放置一个按钮。我把它放进去,就像我放一张图片一样,然后我在教程中找到了这行代码:

button.setImageResource(current_item.getButton_id());

我尝试将其更改为setButtonResource,但没有方法。谢谢你的帮助。我希望它看起来像这样,其中有一个带有文本视图和按钮的列表视图。我把文本放得很好,但我不知道如何在列表视图中获得一个按钮。

在此处输入图像描述

这是我的适配器:

private class MyListAdapter extends ArrayAdapter<SchoolsListClass>{
public MyListAdapter() {
    super(searchResults.this, R.layout.da_item, mySchools);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Make sure we have a view to work with (may have been given null
    View itemView = convertView;
        if (itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.da_item, parent, false);
        }
    //Find school to work with
        SchoolsListClass currentSchoolsListClass = mySchools.get(position);
    //Fill the view
    //school name
    TextView schoolText = (TextView) itemView.findViewById(R.id.item_schoolName);
    schoolText.setText(currentSchoolsListClass.getSchool_name());

    //subscribe/unsubscribe button
    Button button = (Button)itemView.findViewById(R.id.item_subscribeButton);
    button..setImageResource(currentSchoolsListClass.getButton_id());//This is the line of code I am having trouble with. I want it to do exactly what it is doing for images, but use buttons instead of images.
    return itemView;
}

学校列表类:

package com.parse.starter;

public class SchoolsListClass {
private String school_name;
private int button_id;


public SchoolsListClass(String school, int button){
super();
this.school_name = school;
this.button_id = button;



}


public String getSchool_name() {
    return school_name;
    }


    public int getButton_id() {
    return button_id;
        }
}

school_results.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/schoolListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" >
    </ListView>

</RelativeLayout>

da_item.xml:(我的列表视图中的项目示例)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/itemLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="4dp" >

    <TextView
        android:id="@+id/item_schoolName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/item_subscribeButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/item_subscribeButton"
        android:ellipsize="end"
        android:text="Central Wisconsin Christian School"
        android:textSize="@dimen/school_name_size" />

    <Button
        android:id="@+id/item_subscribeButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>
4

2 回答 2

4

相当于是

button.setBackgroundResource(int resid);
于 2013-06-21T15:46:19.990 回答
0

你可以使用,但它在新的 API 中被破坏了

button.setBackgroundDrawable(R.drawable.ic_launcher);
于 2013-06-21T15:53:42.767 回答