Android 在GridView中面对问题

Android 在GridView中面对问题,android,gridview,uiview,Android,Gridview,Uiview,在这里,我正在开发一个应用程序,在这个应用程序中,我必须根据客户的要求使用GridView来显示一些图像 public class MainActivity extends Activity implements OnClickListener { EditText text; Button button; int number; private static final String NUMBER_OF_IMAGES = "numbers"; @Override protected voi

在这里,我正在开发一个应用程序,在这个应用程序中,我必须根据客户的要求使用GridView来显示一些图像

public class MainActivity extends Activity implements OnClickListener {

EditText text;
Button button;
int number;
private static final String NUMBER_OF_IMAGES = "numbers";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (EditText) findViewById(R.id.text);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    try {
        number = Integer.parseInt(text.getText().toString());

        // if user enters 0 or negative number then the default value is set
        // to 2
        if (number < 1) {
            number = 2;
        }
    } catch (Exception e) {
        e.printStackTrace();
        // if user doesn't enter anything then code will catch 
        // classCastException and assign 2 as default value here
        number = 2;
    }
    // start this intent by putting this number in the bundle
    Intent i = new Intent(getApplicationContext(), SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putInt(NUMBER_OF_IMAGES, number);
    i.putExtras(bundle);
    startActivity(i);
}
}
在这个GridView中,我一次显示2行和2列,这样一次可以看到4个图像。现在,用户希望做一些更改,并希望我创建一个编辑文本,他可以在其中输入数字,根据该数字,GridView应该更改其列和行。

有没有可能,因为我已经浏览了很多在线示例,我得到的只是在运行时更改GridView,但是使用固定的预定义行数和列数。但是我没有发现任何像在运行时从EditText更改GridView这样的例子


这种帮助将是可观的

根据ueser输入的行数和列数,您可以计算要显示的项目数,将该数量的项目传递给适配器,要设置列数,您可以尝试
gridView.setNumColumns(numColumns)。行数将相应调整

正如我在评论中提到的,您可以为gridview使用自定义baseadapter,并将图像数量作为适配器类构造函数中的参数。下面是可以用来实现这一点的代码片段

自定义基本适配器

您的主要活动:在此活动中,您需要要求用户输入他们希望看到的图像数量。当用户未输入值时,我没有添加任何类型的验证或错误消息,而是捕获了可能的错误并使用它来指定默认值。您可以根据自己的需求在这里应用其他类型的逻辑

public class MainActivity extends Activity implements OnClickListener {

EditText text;
Button button;
int number;
private static final String NUMBER_OF_IMAGES = "numbers";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (EditText) findViewById(R.id.text);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    try {
        number = Integer.parseInt(text.getText().toString());

        // if user enters 0 or negative number then the default value is set
        // to 2
        if (number < 1) {
            number = 2;
        }
    } catch (Exception e) {
        e.printStackTrace();
        // if user doesn't enter anything then code will catch 
        // classCastException and assign 2 as default value here
        number = 2;
    }
    // start this intent by putting this number in the bundle
    Intent i = new Intent(getApplicationContext(), SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putInt(NUMBER_OF_IMAGES, number);
    i.putExtras(bundle);
    startActivity(i);
}
}
public类MainActivity扩展活动实现OnClickListener{
编辑文本;
按钮;
整数;
私有静态最终字符串编号\u OF_IMAGES=“numbers”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(EditText)findViewById(R.id.text);
按钮=(按钮)findViewById(R.id.button);
setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
试一试{
number=Integer.parseInt(text.getText().toString());
//如果用户输入0或负数,则设置默认值
//到2
如果(数字<1){
数量=2;
}
}捕获(例外e){
e、 printStackTrace();
//若用户并没有输入任何内容,那个么代码将捕获
//classCastException并在此处指定2作为默认值
数量=2;
}
//通过将此号码放入捆绑包开始此目的
Intent i=新的Intent(getApplicationContext(),SecondActivity.class);
Bundle=新Bundle();
bundle.putInt(图像的个数,个数);
i、 putExtras(束);
星触觉(i);
}
}
第二个活动:这是定义和初始化gridview和适配器的活动。确保从以前的活动中检索捆绑包,并正确检索用户输入的号码

public class SecondActivity extends Activity {
GridView grid;
Adapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grids);

    Bundle bundle = getIntent().getExtras();
    int numbers = bundle.getInt(MainActivity.NUMBER_OF_IMAGES);

    grid = (GridView) findViewById(R.id.gridview);
    // depending upon your requirement you can also add logic here to set
    // the number of columns based on the number entered by the user
    setColumns(numbers);
    adapter = new Adapter(getApplicationContext(), numbers);
    grid.setAdapter(adapter);
}

@Override
protected void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}

/**
 * Method to set the number of columns depending upon user input for desired
 * grids. other attributes for the grid view can also be set her such as
 * width, height, background etc.
 * 
 * @param number
 */
public void setColumns(int number) {
    if (number > 0 && number < 10) {
        grid.setNumColumns(2);
    } else if (number >= 10 && number < 20) {
        grid.setNumColumns(4);
    } else {
        grid.setNumColumns(5);
    }
}
}
公共类SecondActivity扩展活动{
网格视图网格;
适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.grids);
Bundle Bundle=getIntent().getExtras();
int numbers=bundle.getInt(MainActivity.NUMBER\u的图像);
grid=(GridView)findviewbyd(R.id.GridView);
//根据您的需求,您还可以在此处添加逻辑来设置
//基于用户输入的数字的列数
设置列(编号);
适配器=新适配器(getApplicationContext(),数字);
设置适配器(适配器);
}
@凌驾
受保护的void onResume(){
super.onResume();
adapter.notifyDataSetChanged();
}
/**
*方法,根据所需的用户输入设置列数
*网格视图的其他属性也可以设置为
*宽度、高度、背景等。
* 
*@param编号
*/
公共无效集合列(整数){
如果(编号>0&&编号<10){
网格。setNumColumns(2);
}否则,如果(编号>=10&&number<20){
网格。setNumColumns(4);
}否则{
网格。setNumColumns(5);
}
}
}

注意:这不是针对性能的高度优化代码,只是提供了如何实现所需功能的示例

使用自定义适配器并在构造函数中从用户传递图像数的参数。将它用于n个您想要显示的图像。实际上,有一种方法可以处理这个问题。您可以在初始化时获取最大列/行数。然后,您可以为不需要的列/行应用android:visibility=“gone”属性。谢谢你宝贵的回复,我会检查代码并让你知道是什么happening@Devraj这个答案对你有帮助吗?如果是的话,请考虑接受和投票。