我有一个列表视图,并使用包含 1 个 ImageView、2 个 TextView 和 1 个按钮的自定义布局对其进行了扩展。我想在单击 listview item 时更改按钮背景,但我无法执行它。有人能帮助我吗?非常感谢你。
row_segnalibro.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="50dp" >
<ImageView android:id="@+id/ImageView01" android:src="@drawable/star"
android:layout_height="40dp"
android:layout_width="40dp"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout02"
android:orientation="vertical"
android:layout_width="220dp" android:layout_height="match_parent">
<TextView
android:id="@+id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tvRow2"
android:layout_below="@id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
</RelativeLayout>
<Button
android:id="@+id/butt_segnalib"
android:background="@drawable/freccia_o"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center" />
Segnalibro.java
public class Segnalibro extends ListActivity{
super.onCreate(savedInstanceState);
setContentView(R.layout.segnalibro);
lv = (ListView) getListView();
.....some code
MySimpleCursorAdap myadap = new MySimpleCursorAdap(getApplicationContext(),R.layout.rowlayout_segnalibro,curr,campi, to);
lv.setAdapter(myadap);
lv.setClickable(true);
lv.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,int position, long arg3) {
Log.i("","I have clicked on item "+position);
}
});
MySimpleCursorAdap.java
public class MySimpleCursorAdap extends SimpleCursorAdapter{
public MySimpleCursorAdap(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mLayout = layout;
this.curr = c;
this.cont = context;
}
public void bindView(View view, Context context, final Cursor cursor) {
....some code
/* Button butt = (Button) view.findViewById(R.id.butt_segnalib);
butt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
.....code to change the background
}
}); */
}
在 MySimpleCursorAdap 中,我可以使用 findViewById 方法获取 Button,我如何在 Segnalibro 类中做到这一点?