メインアクティビティにリストビューがあり、その後にテキストビューが続きます。リスト ビューにはボタンが含まれています。リスト内のボタンをクリックすると、textatrea にテキストが表示されます。しかし、ボタンのクリックをキャプチャできません。
以下は私が使用したコードです。何が悪いのか教えてください。(代替ソリューションは必要ありませんが、コードの何が問題なのか知りたいです)
public class MyFirstAppActivity extends Activity implements OnItemClickListener {
ListView listview ;
String[] array = new String[] {"Click1","Click2"};
ArrayAdapter <String> arrayadapter;
TextView textview;
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.main);
arrayadapter = new ArrayAdapter<String>(this, R.layout.button,array);
listview = (ListView)findViewById(R.id.lv_1);
listview.setAdapter(arrayadapter);
listview.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?>arg1,View v,int Position, long id){
textview = (TextView)findViewById(R.id.textview);
textview.setText("Hello, you just clicked a button"+Position);
}
}
これにより、以下に示すようにアクティビティが main.xml に設定されます。しかし、リストのボタンをクリックすると、クリックがキャプチャされません。これはなぜですか?
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView android:id = "@+id/textview"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_below = "@id/lv_1"
android:text="Message about the button is displayed here">
</TextView>"
</RelativeLayout>
ボタン.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button" >
</Button>