如何在GridView中更改图像?

时间:2016-05-08 20:57:35

标签: java android gridview

我的GridView有问题。我想通过GridView中的代码更改图像(而不是OnClickListener等),但我已经失去了任何希望......这是我的代码:

ImageAdapter:

package com.example.rafal.gra;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class ImageAdapter extends BaseAdapter{

private Context CTX;
public Integer image_id[] = {R.mipmap.right_head,R.mipmap.right_head,R.mipmap.right_head,
        R.mipmap.right_head, R.mipmap.right_head, R.mipmap.right_head, R.mipmap.right_head,
R.mipmap.right_head,R.mipmap.right_head,R.mipmap.right_head,R.mipmap.right_head,
        R.mipmap.right_head};

public ImageAdapter(Context CTX){
    this.CTX = CTX;
}

@Override
public int getCount() {
    return image_id.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView img;
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
            CTX.getResources().getDisplayMetrics());
    if(convertView == null){
        img = new ImageView(CTX);
        img.setLayoutParams(new GridView.LayoutParams(height,height));
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        img.setPadding(1,1,1,1);
    }else{
        img = (ImageView) convertView;
    }
    img.setImageResource(image_id[position]);
    return img;
}
}

EquipmentActivity:

package com.example.rafal.gra;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class EquipmentActivity extends AppCompatActivity {

String postacWeapon, postacShield;
int postacHpotion, postacMpotion;
Backpack backpack;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_equipment);
    final GridView grid = (GridView) findViewById(R.id.gridView);
    grid.setAdapter(new ImageAdapter(this));
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(), "You clicked on image " + position, Toast.LENGTH_LONG).show();
        }
    });
    odczyt();
    eq();


    backpack = new Backpack("sword_of_faith","","","","","","","","","","","");
    backpack();
}

@Override
public void onBackPressed() {
    AlertDialog.Builder exit = new AlertDialog.Builder(this);
    exit.setMessage("Are you sure you want to exit the application?")
            .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //zapis();
                    finish();
                    System.exit(0);
                }
            })
            .setPositiveButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .create();
    exit.show();
}

public void powrot(View view) {

    //zapis();
    startActivity(new Intent(getApplicationContext(), MainActivity.class));
    finish();

}

public void odczyt() {

    SharedPreferences loadE = getSharedPreferences("Save", MODE_PRIVATE);
    SharedPreferences.Editor editor4 = loadE.edit();
    postacWeapon = loadE.getString("sword", "");
    postacShield = loadE.getString("shield", "");
    postacHpotion = loadE.getInt("hpotion", 0);
    postacMpotion = loadE.getInt("mpotion",0);

}

public void eq() {

    TextView hp = (TextView) findViewById(R.id.textViewHp);
    TextView mp = (TextView) findViewById(R.id.textViewMp);
    hp.setText(Integer.toString(postacHpotion)+"   ");
    mp.setText(Integer.toString(postacMpotion) + "   ");
    ImageView weapon = (ImageView) findViewById(R.id.imageViewWeapon);
    ImageView shield = (ImageView) findViewById(R.id.imageViewShield);

    switch (postacWeapon) {
        case "short_sword":
            weapon.setImageResource(R.mipmap.short_sword);
            break;
        default:
            weapon.setImageResource(R.mipmap.weapon_empty);
    }

    switch (postacShield) {
        case "shield_zero":
            shield.setImageResource(R.mipmap.shield_zero);
            break;
        default:
            shield.setImageResource(R.mipmap.shield_empty);
    }


}

public void backpack(){

    final GridView grid = (GridView) findViewById(R.id.gridView);
    grid.setAdapter(new ImageAdapter(this));
    ImageView slot = (ImageView)     findViewById((int)grid.getItemIdAtPosition(0));
    slot.setImageResource(R.mipmap.sword_of_faith);

}
}

这里最重要的是public void backpack()。在里面我想写代码来改变GridView中的图像。我希望有人能帮助我。

我知道背包()中有一个例外,但我想告诉你我是怎么做的。

1 个答案:

答案 0 :(得分:1)

您可以向ImageAdapter添加新方法,如下所示:

public void changeImage(int position, int image){
    if(position >= 0 && position < images_id.length){
        image_id[position] = image;
        notifyDataSetChanged();
    }
}

以下是您可以使用它的方法:

GridView grid = (GridView) findViewById(R.id.gridView);
((ImageAdapter)grid.getAdapter()).changeImage(0, R.mipmap.sword_of_faith);

此外,您应该考虑将GridViewImageAdapter实例设置为类变量,这样您就不必在每次需要时都创建它们。