我想顯示一個列表,當我在該位置按下它時,我想顯示一個不存在的圖像並更改該行的字體。所以首先我有一個簡單的列表中的XML文件,然後另一個XML的每一行,這一個:OnListIemClick爲每一行設置可見性
<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()doesn't工作。首先,當我選擇一個項目時,圖像不可見,除此之外,我按下第一行的位置是改變字體的位置,而不是我按下的項目。
謝謝:)
圖像初始化消失了(我想要的),但是當我按下它並不可見 – Txispas 2012-08-19 18:50:46