0

我正在创建一个自定义适配器,以便我可以在ListView中放置一个按钮。我把它就像我会的图像,然后我得到了在本教程这行代码:有什么等价于setImageResource的按钮?

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> 
+0

你是说的按钮在你的列表视图显示在所有,使用此代码? – Vyrx

回答

4

相当于是

button.setBackgroundResource(int resid); 
+0

该方法仅适用于可绘制资源,但我想为按钮设置一个。这是我想要做的事情的想法,只是为了按钮而不是图像。 –

+0

否则,如果你有一个适配器与按钮一起工作,那也可以工作 –

+0

对不起,我不明白你...你想设置一个图像作为你的按钮的背景吗? – Blackbelt

0

可以使用,但它是在新的API debricated

button.setBackgroundDrawable(R.drawable.ic_launcher); 
+0

我正在开发api级别8-17,所以我想使用未被depricated的方法。 –

+0

嗨sunil, 不setBackgroundDrawable只是改变按钮的图片? 如果是这样,我仍然有一个问题,因为我需要列表中的每个按钮都具有不同的功能。你有任何推荐如何做到这一点? – Tomer

相关问题