Android 适配器未更新 ui

标签 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 总计数 = 0;

在我的 onCreate 方法中:

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

从我的适配器中,我将元素添加到 GridView 中以产生 5 种颜色,在 GridView UI 中显示没有问题...o.k

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

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

Activity 主

<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() 才能刷新其内容。

关于Android 适配器未更新 ui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16652812/

相关文章:

android - 如何正确设置应用程序的名称?

android - requestLocationUpdates() 在单独的线程中

android - ExoPlayer2 - 如何使 HTTP 301 重定向工作?

c# - 单击gridview外部的按钮获取gridview的rowindex

android - 我可以在另一个类/Activity 中使用 ItemCount() 方法值吗?

android - 设置 Android 键盘上的返回键

javascript - 将 POST 数据作为隐藏的输入名称值数组发送

android - GridView 中的动态填充不通过 XML

android - 使用自定义适配器时刷卡不显示

android - 如何从 Listview, Android 中删除适配器