Android的电源适配器没有更新的用户界面 [英] Android adapter not updating ui

查看:288
本文介绍了Android的电源适配器没有更新的用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类女巫拥有一些变量...例如

  INT黄色= 0xffffff66;
 INT绿色= 0xff00EE76;
 INT红色= 0xffff4342;
 INT蓝色= 0xff42c3ff;
 INT紫色= 0xff9932CC;
 INT白色= 0xffffffff的;
 ArrayList的<整数GT; nextColorArray =新的ArrayList<整数GT;();
 

INT TOTAL_COUNT = 0;

在我的onCreate方法:

 的GridView gridview2 =(GridView控件)findViewById(R.id.colorNext);
gridview2.setAdapter(新ImageAdapter(本));
nextColorArray.add(蓝色);
nextColorArray.add(绿色);
nextColorArray.add(红色);
nextColorArray.add(黄色);
nextColorArray.add(紫色);
 

这是我的适配器我添加元素到网格视图产生5种颜色显示在GridView UI没有问题... ... OK

我有一个按钮的onclick(不在的onCreate)事件,到1,每次递增TOTAL_COUNT我按一下按钮... OK时TOTAL_COUNT大于十,我想一个新的元素添加到array..ok

 如果(动作== MotionEvent.ACTION_DOWN){
   如果(TOTAL_COUNT→10){
       nextColorArray.add(0,白色);
}...好
 

我的问题是,用户界面​​的GridView没有更新的用户界面,以显示新的颜色...我如何能当一个新的颜色添加到列表?​​???

更新适配器的任何想法

编辑:因为我不知道我对我的帖子有多少编辑的制作。我编辑的问题,这里增加了答案。这个工程。

 公共类MainActivity延伸活动{
INT黄色= 0xffffff66;
INT绿色= 0xff00EE76;
INT红色= 0xffff4342;
INT蓝色= 0xff42c3ff;
INT紫色= 0xff9932CC;
INT白色= 0xFFFFFFFF的;

INT TOTAL_COUNT = 0;
INT colorPosition = 0;
INT colorPickerStart = 0;
Button按钮;

ImageView的ImageView的;

ImageAdapter IA;
GridView控件gridview2;

的ArrayList&其中;整数> nextColorArray =新的ArrayList&其中;整数>(10);
ArrayList的<整数GT;颜色拾取=新的ArrayList<整数GT;();

@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
nextColorArray.add(蓝色);
nextColorArray.add(绿色);
nextColorArray.add(红色);
nextColorArray.add(黄色);
nextColorArray.add(紫色);
//设置适配器
GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
gridview.setAdapter(新ButtonAdapter(本));
按钮=(按钮)findViewById(R.id.button1);
gridview2 =(GridView控件)findViewById(R.id.gridview2);
IA =新ImageAdapter(这一点,nextColorArray);
gridview2.setAdapter(IA);
button.setOnClickListener(新Button.OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){

                        nextColorArray.add(Color.BLACK);
                        ia.notifyDataSetChanged();

            }
        });
//接下来的色彩元素


}
公共类ImageAdapter扩展了BaseAdapter {
私人语境mContext;
ArrayList的<整数GT;一个;

公共ImageAdapter(上下文C,的ArrayList&其中;整数>一种){
    mContext = C;
    this.a =一个;
}
公众诠释getCount将(){

    返回a.size();
}

公共对象的getItem(INT位置){
    返回的位置;
}

众长getItemId(INT位置){
    返回0;
}

/ *创建一个新的ImageView由适配器引用的每个项目* /
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    如果(convertView == NULL){/ *如果它不回收,初始化一些属性* /
        ImageView的=新ImageView的(mContext);
        imageView.setLayoutParams(新GridView.LayoutParams(50,45));
        imageView.setBackgroundColor(nextColorArray.get(colorPosition));
        如果(colorPosition< 9)colorPosition ++;
    } 其他 {
        ImageView的=(ImageView的)convertView;
    }
    返回ImageView的;
}

}
/ *按钮适配器* /
公共类ButtonAdapter扩展了BaseAdapter {
私人语境mContext;

公共ButtonAdapter(上下文C){
    mContext = C;
}

公众诠释getCount将(){

    返回nextColorArray .size();
}

公共对象的getItem(INT位置){
    返回的位置;
}

众长getItemId(INT位置){
    返回0;
}

//由适配器引用的每个项目创建一个新的按钮
公共查看getView(INT位置,查看convertView,ViewGroup中父){

    如果(convertView == NULL){

        按钮=新按钮(mContext);
        button.setLayoutParams(新GridView.LayoutParams(128,128));

        TOTAL_COUNT ++;
        button.setText(+ TOTAL_COUNT);
    } 其他 {
        按钮=(按钮)convertView;
    }
    返回按钮;
} //到底得的观点
} //结束按钮适配器
 

activity_main

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>

< GridView控件
    机器人:ID =@ + ID / GridView控件
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =150dp
    机器人:columnWidth中=0dp
    机器人:重力=中心
    机器人:为numColumns =2
    机器人:horizo​​ntalSpacing =20dp
    机器人:填充=40dp
    机器人:verticalSpacing =20dp/>
     < GridView控件
    机器人:ID =@ + ID / gridview2
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =150dp
    机器人:layout_above =@ + ID /按钮1
    机器人:layout_alignParentRight =真
    机器人:layout_marginBottom =74dp
    机器人:columnWidth中=0dp
    机器人:重力=中心
    机器人:horizo​​ntalSpacing =20dp
    机器人:为numColumns =2
    机器人:填充=40dp
    机器人:verticalSpacing =20dp>
< / GridView的>


<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:文本=按钮/>


< / RelativeLayout的>
 

解决方案

  ImageAdapter IA;
 GridView控件gridview2 =(GridView控件)findViewById(R.id.colorNext);
 IA =新ImageAdapter(本);
 gridview2.setAdapter(IA);
 ....
 如果(动作== MotionEvent.ACTION_DOWN){
 如果(TOTAL_COUNT→10){
   nextColorArray.add(0,白色);
   ia.notifyDataSetChanged(); //调用notifyDataSetChanged来更新你的GridView
 }
 

公共无效notifyDataSetChanged()

通知所附的观察员的基础数据已被更改,任何视图反映了数据集应自动刷新。

例如:

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>

< GridView控件
    机器人:ID =@ + ID / GridView控件
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:columnWidth中=0dp
    机器人:重力=中心
    机器人:为numColumns =2
    机器人:horizo​​ntalSpacing =20dp
    机器人:填充=40dp
    机器人:verticalSpacing =20dp/>

<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:文本=按钮/>

    < / RelativeLayout的>
 

MainActivity

 公共类MainActivity延伸活动{

ImageAdapter IM;
ImageView的ImageView的;
GridView的GD;
 ArrayList的<整数GT; nextColorArray =新的ArrayList<整数GT;();
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

     INT黄色= 0xffffff66;
     INT绿色= 0xff00EE76;
     INT红色= 0xffff4342;
     INT蓝色= 0xff42c3ff;
     INT紫色= 0xff9932CC;
     最终诠释白色= 0xffffffff的;
     nextColorArray.add(蓝色);
     nextColorArray.add(绿色);
     nextColorArray.add(红色);
     nextColorArray.add(黄色);
     nextColorArray.add(紫色);
        GD =(GridView控件)findViewById(R.id.gridview);
        IM =新ImageAdapter(这一点,nextColorArray);
        gd.setAdapter(IM);
     按钮B =(按钮)findViewById(R.id.button1);
     b.setOnClickListener(新OnClickListener()
     {

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            nextColorArray.add(Color.BLACK);
            im.notifyDataSetChanged();

        }

     });
}
}
 

ImageAdapter

 公共类ImageAdapter扩展了BaseAdapter {

语境mContext;
ArrayList的<整数GT;一个;

公共ImageAdapter(语境上下文,的ArrayList&其中;整数>一种){
    mContext =背景;
    this.a =一个;
}

@覆盖
公众诠释getCount将(){
    // TODO自动生成方法存根
    返回a.size();
}

@覆盖
公共对象的getItem(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}

@覆盖
众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}

@覆盖
公共查看getView(最终诠释的立场,观点ARG1,ARG2的ViewGroup){
     ImageView的ImageView的;
        如果(ARG1 == NULL){
            ImageView的=新ImageView的(mContext);

            imageView.setPadding(0,20,0,0);
        } 其他 {
            ImageView的=(ImageView的)ARG1;
        }

        imageView.setBackgroundColor(a.get(位置));

        返回ImageView的;

}
 }
 

,当你点击黑色出现在屏幕上。

快照

I have a class witch holds some variables...eg

 int yellow = 0xffffff66;
 int green = 0xff00EE76;
 int red = 0xffff4342;
 int blue = 0xff42c3ff;
 int purple = 0xff9932CC;
 int white = 0xffffffff;
 ArrayList<Integer>nextColorArray = new ArrayList<Integer>();

int total_count = 0;

in my onCreate method:

GridView gridview2 = (GridView) findViewById(R.id.colorNext);
gridview2.setAdapter(new ImageAdapter(this));
nextColorArray.add(blue);
nextColorArray.add(green);
nextColorArray.add(red);
nextColorArray.add(yellow);
nextColorArray.add(purple);

from my adapter i add elements to the grid view to produce 5 colors the show in the gridview UI no problem... o.k

i have a button onclick(not in onCreate) event that increments total_count by 1 each time i click the button...o.k when the total_count is greater than ten i want to add a new element to the array..ok

if (action == MotionEvent.ACTION_DOWN) {
   if(total_Count > 10){
       nextColorArray.add(0, white);  
}...ok

my problem is that the UI gridview is not updating the ui to show the new color...any ideas on how i can update the adapter when a new color is added to the list????

Edit: Since i am not sure how many edit's i made to my post. i edited the question and added the answer here. This works .

public class MainActivity extends Activity {
int yellow = 0xffffff66;
int green = 0xff00EE76;
int red = 0xffff4342;
int blue = 0xff42c3ff;
int purple = 0xff9932CC;
int white = 0xFFFFFFFF;

int total_Count = 0;
int colorPosition = 0;
int colorPickerStart = 0;
Button button;

ImageView imageView;

ImageAdapter ia;
GridView gridview2;

ArrayList<Integer>nextColorArray = new ArrayList<Integer>(10);
ArrayList<Integer>colorPicker = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextColorArray.add(blue);
nextColorArray.add(green);
nextColorArray.add(red);
nextColorArray.add(yellow);
nextColorArray.add(purple);
//set adapters
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ButtonAdapter(this));
button = (Button) findViewById(R.id.button1); 
gridview2 = (GridView) findViewById(R.id.gridview2);
ia = new ImageAdapter(this, nextColorArray);
gridview2.setAdapter(ia);
button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                        nextColorArray.add(Color.BLACK);
                        ia.notifyDataSetChanged();

            }
        });
//next color elements


}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<Integer> a;

public ImageAdapter(Context c, ArrayList<Integer> a) {
    mContext = c;
    this.a = a;
}
public int getCount() {

    return a.size();
}

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

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) {
    if (convertView == null) {/*if it's not recycled, initialize some attributes*/
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(50, 45));
        imageView.setBackgroundColor(nextColorArray.get(colorPosition));
        if(colorPosition < 9) colorPosition++;
    } else {
        imageView = (ImageView) convertView;
    }
    return imageView;
}

}
/*button adapter*/
public class ButtonAdapter extends BaseAdapter {
private Context mContext;

public ButtonAdapter(Context c) {
    mContext = c;
}

public int getCount() {

    return nextColorArray .size();
}

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

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

// create a new button for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        button = new Button(mContext);
        button.setLayoutParams(new GridView.LayoutParams(128, 128));

        total_Count++;
        button.setText(""+total_Count); 
    } else {
        button = (Button) convertView;
    }
    return button;
}// end get view
}// end button adapter

activity_main

<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="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:columnWidth="0dp"
    android:gravity="center"
    android:numColumns="2"
    android:horizontalSpacing="20dp"
    android:padding="40dp"
    android:verticalSpacing="20dp" />
     <GridView
    android:id="@+id/gridview2"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_above="@+id/button1"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="74dp"
    android:columnWidth="0dp"
    android:gravity="center"
    android:horizontalSpacing="20dp"
    android:numColumns="2"
    android:padding="40dp"
    android:verticalSpacing="20dp" >
</GridView>


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />


</RelativeLayout>

解决方案

 ImageAdapter ia;
 GridView gridview2 = (GridView) findViewById(R.id.colorNext);
 ia = new ImageAdapter(this);
 gridview2.setAdapter(ia);  
 .... 
 if (action == MotionEvent.ACTION_DOWN) {
 if(total_Count > 10){
   nextColorArray.add(0, white); 
   ia.notifyDataSetChanged(); // call notifyDataSetChanged  to update your gridview
 }

public void notifyDataSetChanged ()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

Example:

activity_main.xml

<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="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:columnWidth="0dp"
    android:gravity="center"
    android:numColumns="2"
    android:horizontalSpacing="20dp"
    android:padding="40dp"
    android:verticalSpacing="20dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

    </RelativeLayout>

MainActivity

  public class MainActivity extends Activity {

ImageAdapter im;
ImageView imageView;
GridView gd;
 ArrayList<Integer>nextColorArray = new ArrayList<Integer>(); 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     int yellow = 0xffffff66;
     int green = 0xff00EE76;
     int red = 0xffff4342;
     int blue = 0xff42c3ff;
     int purple = 0xff9932CC;
     final int white = 0xffffffff;
     nextColorArray.add(blue);
     nextColorArray.add(green);
     nextColorArray.add(red);
     nextColorArray.add(yellow);
     nextColorArray.add(purple);
        gd= (GridView) findViewById(R.id.gridview);
        im = new ImageAdapter(this,nextColorArray);
        gd.setAdapter(im);
     Button b= (Button) findViewById(R.id.button1);
     b.setOnClickListener(new OnClickListener()
     {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            nextColorArray.add(Color.BLACK);
            im.notifyDataSetChanged();

        }

     });
}
}

ImageAdapter

   public class ImageAdapter extends BaseAdapter{

Context mContext;
ArrayList<Integer> a;

public ImageAdapter(Context context,ArrayList<Integer> a)   {
    mContext=context;
    this.a= a;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return a.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View arg1, ViewGroup arg2) {
     ImageView imageView;
        if (arg1 == null) {  
            imageView = new ImageView(mContext);

            imageView.setPadding(0, 20, 0, 0);
        } else {
            imageView = (ImageView) arg1;
        }

        imageView.setBackgroundColor(a.get(position));

        return imageView;

}
 }

As and when you click black color appears on the screen.

SnapShot

这篇关于Android的电源适配器没有更新的用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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