如何在GridView中更改图像? [英] How to change image in GridView?

查看:90
本文介绍了如何在GridView中更改图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with GridView. I want to change image through code in GridView (not OnClickListener etc.) but I have lost any hope...here's my code:

ImageAdapter:

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:

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中的图像.我希望有人能帮助我.

The most important thing here is public void backpack(). Inside it I want to write code to change image in GridView. I hope someone will help me.

我知道Backpack()中有一个例外,但我想向您展示我如何做到这一点.

I know that there is an exception in backpack() but I wanted to show you how I want to be it done.

推荐答案

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

You can add a new method to your ImageAdapter, something like this:

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);

此外,您应该考虑将 GridView ImageAdapter 实例设置为类变量,这样就不必在每次使用它们时都创建它们.

Also, you should consider setting the GridView and ImageAdapter instances as class variables so you won't have to create them every time you need to use them.

这篇关于如何在GridView中更改图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆