如何根据用户的选择在GridView中分配项目数

时间:2018-07-20 08:48:36

标签: java android android-gridview

我正在Android Studio中制作一个项目。

我有一个gridview,想根据用户在AlertDialog中输入的内容分配其中的项目数。

我尝试在ImageAdapter中创建一个方法,该方法包含一个for循环,以根据用户选择的数字将图像填充到数组 mThumbIds 中,但这不起作用。

我该怎么做?

这是ImageAdapter:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.pic_1, R.drawable.pic_1,
            R.drawable.pic_1,R.drawable.pic_1,
            R.drawable.pic_1, R.drawable.pic_1,

    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {

            //Calculation of ImageView Size - density independent.
            //maybe you should do this calculation not exactly in this method but put it somewhere else.
            Resources r = Resources.getSystem();
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());


            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

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

}

这是MainActivity:

public class MainActivity extends Activity {

    EditText input;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("How many tables do you have ?");

        input = new EditText(this);
        builder.setView(input);

        builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String txt = input.getText().toString();
                Toast.makeText(getApplicationContext(),txt, Toast.LENGTH_LONG).show();
            }
        });

        AlertDialog ad = builder.create();
        ad.show();
    }

}

1 个答案:

答案 0 :(得分:0)

在适配器中添加以下内容:

private int count;

public void setCount(int count){
this.count = count;
notifyDataSetChanged();
}

您的getCount()方法将是

    @Override
        public int getCount() {
            if(count == 0) return mThumbIds.length;
            else return count;
        }

在您的按钮onClick呼叫中,以下内容:

imageAdapter.setCount(count);