Возможно ли изменить изображение ImageViews, хранящееся внутри GridView, нажав на одну ячейку? Это часть основной деятельности, которая меня беспокоит.Изменение нескольких ячеек GridView, щелкнув один из них
tablicaBrodova.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView img = (ImageView) view;
//SOME OTHER CODE
case 1:
if ((provjeriStupac(temp) <= 6)) {
pozicije1[temp + 1] = 1;
pozicije1[temp + 2] = 1;
pozicije1[temp + 3] = 1;
for (int k=0;k<=3;k++)
{
position=temp+k;
img.setImageResource(R.drawable.postavljen);
}
Это часть, которая беспокоит меня (цикл for). Я попытался изменить положение, а затем изменить ImageView, но он, похоже, не работает. Возможно ли это? Любой другой способ сделать это? Я кое-что прочитал о performItemClick, но ничего не знаю об этом.
Temp - это предыдущая позиция, нажатая сохраненная, а pozicije1 - массив, сохраняющий мои данные.
Это мой Image Adapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] celije = new Integer[100];
public ImageAdapter (Context c){
mContext = c;
for (int i=0; i<100; i+=1){
celije[i] = R.drawable.kockica;
}
}
public int getCount(){
return celije.length;
}
public Object getItem(int position){
return celije[position];
}
public long getItemId(int position){
return 0;
}
public View getView (int position, View convertView, ViewGroup parent){
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(android.R.color.darker_gray);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50,50));
return imageView;
}
}
И мой файл макета:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context="com.marijannovak.potapanjebrodovazavrsni.Ploca1">
<ListView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/listaBrodova"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<GridView
android:layout_width="340dp"
android:layout_height="340dp"
android:id="@+id/tablicaBrodova"
android:numColumns="10"
android:columnWidth="30dp"
android:background="@android:color/white"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/listaBrodova"
android:layout_toEndOf="@+id/listaBrodova"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:id="@+id/reset"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="reset"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:id="@+id/dalje"
android:layout_below="@+id/reset"
android:layout_alignLeft="@+id/reset"
android:layout_alignStart="@+id/reset"
android:onClick="dalje"/>
</RelativeLayout>