更新GridView

时间:2018-08-08 15:50:51

标签: java android gridview

单击某个项目时,我需要更新自己的gridView。我已经做了很多尝试,但是没有任何效果。 我的活动包含以下代码:

public class GameActivity extends AppCompatActivity implements IGameView{

public GridGame _gridGame = new GridGame();
public GamePresenter _presenter = new GamePresenter(this, _gridGame);
public ImageAdapter _imageAdapter = new ImageAdapter(this, _gridGame);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_help) {
        Intent intent = new Intent(this, HelpActivity.class);
        startActivity(intent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    final Context context = this;

    final GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(_imageAdapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            _presenter.addPawn(position);
            _imageAdapter.update(position, _presenter.actualPlayer);
            Toast.makeText(context, "" + position,Toast.LENGTH_SHORT).show();
        }
    });
}

以及imageAdapter的代码:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private GridGame _gridGame;
public ImageAdapter(Context c, GridGame grid) {
    mContext = c;
    _gridGame = grid;
}

public int getCount() {
    return grid.length;
}

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

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    setGrid();

    imageView.setImageResource(grid[position]);
    return imageView;
}

private Integer[] grid = new Integer[42];

public void setGrid() {
    for(int i = 0; i < 42; i++){
        grid[i] = R.drawable.square;
    }
}

public void update(int id, int player){
    grid[id] = R.drawable.ic_notifications_black_24dp;
}

当我单击一个正方形(gridview上的项目)时,我希望该视图更新一个显示其他图像而不是正方形。当我在ImageAdapter上调用update方法但视图不更新时,更新就完成了。

3 个答案:

答案 0 :(得分:2)

首先,您需要修复为适配器返回所需数据的方法,以将项目对象标识为

public class CognitoClientCredentials {

    private String poolId = "xxxxxxxxx";
    private String clientId = "xxxxxxxxxxxxxxxx";

    public String getPoolId(){
        return this.poolId;
    }

    public String getClientId(){
        return this.clientId;
    }
}

答案 1 :(得分:1)

您需要通知适配器数据元素已更改。尝试以下update()方法:

public void update(int id, int player){
    grid[id] = R.drawable.ic_notifications_black_24dp;
    notifyDataSetChanged();
}

答案 2 :(得分:1)

好的,我发现了问题,我在getView(int position, View convertView, ViewGroup parent);中设置了网格,但是应该在构造函数中设置它。 问题是每次我单击一个项目时它都会重置网格中的值,因此该值没有更改。

相关问题