是否有与按钮的setImageResource相同的东西?

时间:2013-06-21 15:44:00

标签: android button android-listview android-adapter custom-adapter

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

button.setImageResource(current_item.getButton_id());

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

enter image description here

这是我的适配器:

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;
}

SchoolsListClass:

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>

2 个答案:

答案 0 :(得分:4)

相当于

button.setBackgroundResource(int resid);

答案 1 :(得分:0)

您可以使用,但它在新API

中被清除
button.setBackgroundDrawable(R.drawable.ic_launcher);