Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android适配器未更新用户界面_Android_Gridview_Android Adapter - Fatal编程技术网

Android适配器未更新用户界面

Android适配器未更新用户界面,android,gridview,android-adapter,Android,Gridview,Android Adapter,我有一个包含一些变量的类…例如 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 yellow = 0xffffff66;
 int green = 0xff00EE76;
 int red = 0xffff4342;
 int blue = 0xff42c3ff;
 int purple = 0xff9932CC;
 int white = 0xffffffff;
 ArrayList<Integer>nextColorArray = new ArrayList<Integer>();
从我的适配器我添加元素到网格视图,以产生5种颜色显示在网格视图用户界面没有问题。。。o、 k

我有一个按钮onclick(不在onCreate中)事件,每次我单击按钮时,它会将total_count增加1…好的,当total_count大于10时,我想向数组中添加一个新元素..好的

if (action == MotionEvent.ACTION_DOWN) {
   if(total_Count > 10){
       nextColorArray.add(0, white);  
}...ok
我的问题是UI gridview没有更新UI以显示新的颜色…关于如何在列表中添加新颜色时更新适配器,有什么想法吗

编辑:因为我不知道我对我的帖子做了多少次编辑。我编辑了问题并在这里添加了答案。这很有效

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
公共类MainActivity扩展活动{
int黄色=0xFFFF66;
int绿色=0xff00EE76;
int red=0xffff4342;
int蓝色=0xff42c3ff;
紫色整数=0xff9932CC;
int白色=0xFFFFFF;
整数总计数=0;
int colorPosition=0;
int colorPickerStart=0;
按钮;
图像视图图像视图;
图像适配器;
GridView GridView 2;
ArrayListNextColorary=新的ArrayList(10);
ArrayListcolorPicker=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextColorary.add(蓝色);
nextColorary.add(绿色);
nextColorary.add(红色);
nextColorary.add(黄色);
添加(紫色);
//设置适配器
GridView GridView=(GridView)findViewById(R.id.GridView);
setAdapter(新按钮适配器(this));
按钮=(按钮)findViewById(R.id.button1);
gridview2=(GridView)findViewById(R.id.gridview2);
ia=新的图像适配器(此,NextColorary);
gridview2.setAdapter(ia);
button.setOnClickListener(新建button.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
nextColorary.add(颜色:黑色);
ia.notifyDataSetChanged();
}
});
//下一个颜色元素
}
公共类ImageAdapter扩展了BaseAdapter{
私有上下文;
ArrayList a;
公共ImageAdapter(上下文c、ArrayList a){
mContext=c;
这个a=a;
}
public int getCount(){
返回a.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回0;
}
/*为适配器引用的每个项目创建新的ImageView*/
公共视图getView(int位置、视图转换视图、视图组父视图){
如果(convertView==null){/*如果它没有被回收,初始化一些属性*/
imageView=新的imageView(mContext);
setLayoutParams(新的GridView.LayoutParams(50,45));
setBackgroundColor(nextcolorray.get(colorPosition));
if(colorPosition<9)colorPosition++;
}否则{
imageView=(imageView)convertView;
}
返回图像视图;
}
}
/*按钮适配器*/
公共类ButtonAdapter扩展BaseAdapter{
私有上下文;
公共按钮适配器(上下文c){
mContext=c;
}
public int getCount(){
返回nextColorArray.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回0;
}
//为适配器引用的每个项目创建一个新按钮
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
按钮=新按钮(mContext);
setLayoutParams(新的GridView.LayoutParams(128128));
总计数++;
按钮.setText(“+”总计数);
}否则{
按钮=(按钮)转换视图;
}
返回按钮;
}//结束获取视图
}//末端按钮适配器
主要活动

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

您必须在适配器上调用
notifyDataSetChanged()
,以刷新其内容

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

通知附加的观察者基础数据已更改,任何反映数据集的视图都应刷新自身

例如:

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>

主要活动

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

        }

     });
}
}
公共类MainActivity扩展活动{
图像适配器;
图像视图图像视图;
GridView gd;
ArrayListNextColorary=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int黄色=0xFFFF66;
int绿色=0xff00EE76;
int red=0xffff4342;
int蓝色=0xff42c3ff;
紫色整数=0xff9932CC;
最终整型白色=0xffffffff;
nextColorary.add(蓝色);
nextColorary.add(绿色);
nextColorary.add(红色);
nextColorary.add(黄色);
添加(紫色);
gd=(GridView)findViewById(R.id.GridView);
im=新的图像适配器(此,NextColorary);
gd.setAdapter(im);
按钮b=(按钮)findViewById(R.id.button1);
b、 setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
nextColorary.add(颜色:黑色);
im.notifyDataSetChanged();
}
});
}
}
图像适配器

   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;

}
 }
公共类ImageAdapter扩展了BaseAdapter{
语境;
ArrayList a;
公共ImageAdapter(上下文,ArrayList a){
mContext=上下文;
这个a=a;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回a.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(最终整数位置,视图arg1,视图组arg2){
图像视图图像视图;
如果(arg1==null){
imageView=新的imageView(mContext);
设置填充(0,20,0,0);
}否则{
imageView=(imageView)arg1;
}
imageView.setBackgr
   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;

}
 }