我正在尝试显示一个列表,当我在该位置按下它时,我想显示一个已经消失的图像并更改该行的字体。所以首先我有一个带有简单列表的xml文件,然后是每行的另一个xml,这个:
<ImageView
android:id="@+id/icono"
android:layout_gravity="center"
android:layout_marginRight="5dip"/>
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:text="TextView" />
<ImageView
android:id="@+id/image_tick_ingred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:layout_alignRight="@id/textView1"
android:layout_centerVertical="true"
android:src="@android:drawable/presence_online" />
</RelativeLayout>
这就是每行的代码,包含左边的图像,然后是文本,最后是行右边的另一个图像。和java代码:
public class TabIngred extends ListActivity {
ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rellenas_ingr); //the list xml layout
Bundle extras = getIntent().getExtras(); //get data
if (extras != null) {
String ingredientesLista[] = extras.getStringArray("ingredientesLista");
int[] ingredientesImagen=extras.getIntArray("ingredientesImagen");
setListAdapter (new IngredienteAdapter(this, R.layout.rellenas_ingr_fila, ingredientesLista, ingredientesImagen));
getListView().setChoiceMode(2);
}
}
private class IngredienteAdapter extends ArrayAdapter<String>{
private String listaIngred[];
private int[] listaImagenes;
public IngredienteAdapter(Context context, int textViewResourceId, String ingredientesLista[], int[] ingredientesImagen) {
super(context, textViewResourceId, ingredientesLista);
this.listaIngred = ingredientesLista;
this.listaImagenes=ingredientesImagen;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// XML de la vista de la fila
v = vi.inflate(R.layout.rellenas_ingr_fila, null); //the row for the listview
}
String ingred = listaIngred[position];
int imagen=listaImagenes[position];
int tickImagen = R.drawable.tick_ingred;//set the image to a tick
if (ingred != null) {
TextView ttitulo = (TextView) v.findViewById(R.id.textView1);
if (ttitulo != null) {
ttitulo.setText(ingred);
}
ImageView timagen = (ImageView) v.findViewById(R.id.icono);
if (timagen != null) {
timagen.setImageResource(imagen);
}
ImageView ttick = (ImageView) v.findViewById(R.id.image_tick_ingred);
if (ttick != null) {
ttick.setImageResource(tickImagen);
ttick.setVisibility(View.GONE);
}
}
return v;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
img=(ImageView)v.findViewById(R.id.image_tick_ingred);
TextView t= (TextView)v.findViewById(R.id.textView1);
if(img.getVisibility()==View.GONE){
t.setTypeface(null, Typeface.BOLD);
img.setVisibility(View.VISIBLE);
}else {
t.setTypeface(null, Typeface.NORMAL);
img.setVisibility(View.GONE);
}
}
}
所以,我定义了getview(),我把图像放在了(这是有效的),但是OnlistItemClick()不起作用。首先,当我选择一个项目时,图像不可见,并且为了不同而分开我按下第一行的位置是改变字体,而不是我按下的项目。
谢谢:)
答案 0 :(得分:0)
您onListItemClick
中需要使用View
例如:
img=(ImageView)findViewById(R.id.image_tick_ingred);
TextView t= (TextView)findViewById(R.id.textView1);
应该是
img=(ImageView)v.findViewById(R.id.image_tick_ingred);
TextView t= (TextView)v.findViewById(R.id.textView1);
注意v
,即点击行的视图
答案 1 :(得分:0)
问题是我定义了getListView()。setChoiceMode(2);那是因为它不起作用,我删除了这条线并且工作得很好!!
毕竟谢谢!