首页 新闻 搜索 专区 学院

Android GridView中改变了某一个View的内容,如何立即让其刷新?

0
悬赏园豆:10 [已解决问题] 解决于 2014-04-22 21:41

大家好:

  写了一个GridView,自定义的一个Adapter,设置了OnItemClickListner改变了GridView的Adapter的数据,并调用了GridView的postInvalidate()。这样,再滚动GridView的时候才能将变化显现出来——具体变化是:点击了那个item,那个item的背景会变成黑色(见图)。

  现在的疑问是:用什么方法,在点击了之后,立马就将变化体现出来?

package com.example.regreshgridvdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class MainActivity extends Activity {
    GridView gridV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        gridV = new GridView(this);
        gridV.setAdapter(new cAdapter(this));
        gridV.setOnItemClickListener(l);
        gridV.setVerticalSpacing(10);
        gridV.setHorizontalSpacing(10);
        gridV.setNumColumns(4);
        setContentView(gridV);
    }
    
    OnItemClickListener l = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Adapter adapter = parent.getAdapter();
            if(adapter != null && adapter instanceof cAdapter){
                ((cAdapter)adapter).setCrtId(position);
                parent.postInvalidate();
                Log.i("xxx", "position=" + position + " id=" + id);
            }
        }
    };

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

}


class cAdapter extends BaseAdapter{
    final int CNT = 100;
    int crt = 0;
    
    Context mCtx;
    public cAdapter(Context ctx) {
        mCtx = ctx;
    }
    
    public void setCrtId(int i){
        crt = i;
    }

    @Override
    public int getCount() {
        return CNT;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView txt = null;
        if(convertView == null)
            txt = new TextView(mCtx);
        else if(convertView instanceof TextView)
            txt = (TextView) convertView;
        txt.setTextSize(30);
        txt.setText("ID=" + position);
        txt.setTextColor(Color.RED);
        txt.setBackgroundColor(position == crt ? Color.BLACK : Color.YELLOW);
        
        return txt;
    }
    
}

Charlie123的主页 Charlie123 | 初学一级 | 园豆:142
提问于:2014-04-22 20:23
< >
分享
最佳答案
0

解决的办法是:

在OnItemClickListener里调用adapter的notifyDataSetChanged()方法——注意,Adapter必须继承BaseAdapter才行,因为notifyDataSetChanged()方法是BaseAdapter中的方法。

OnItemClickListener l = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Adapter adapter = parent.getAdapter();
            if(adapter != null && adapter instanceof cAdapter){

                MainActivity.this.adapter.setCrtId(position);
                MainActivity.this.adapter.notifyDataSetChanged();
                Log.i("xxx", "in listener position=" + position + " id=" + id);
            }
        }
    };
Charlie123 | 初学一级 |园豆:142 | 2014-04-22 21:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册