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

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>
4

2 に答える 2

4

同等のものは

button.setBackgroundResource(int resid);
于 2013-06-21T15:46:19.990 に答える