gpt4 book ai didi

android - 在 GridView 中面临问题

转载 作者:行者123 更新时间:2023-11-29 21:05:08 25 4
gpt4 key购买 nike

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

在那个 GridView 中,我一次显示 2 行和 2 列,因此一次可以看到 4 个图像。现在用户想要做一些更改,并希望我创建一个 EditText,他可以在其中输入数字,根据该数字,GridView 应该更改其列和行。

是否有可能,因为我已经浏览了很多在线示例,而我所得到的只是在运行时更改 GridView 但具有固定的预定义行数和列数。但我没有找到任何示例,例如在运行时从 EditText 更改 GridView。

和类型的帮助将是可观的。

最佳答案

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

自定义 BaseAdapter

public class Adapter extends BaseAdapter {

private final Context context;
private final LayoutInflater inflater;
private final int numbers;

public Adapter(Context context, int numberOfImages) {
this.context = context;
this.numbers = numberOfImages;
this.inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
//You need to return the numberOfImages here for
//the adapter to determine how many view you need to create.
return numbers;
}

private class viewHolder {
ImageView image;
TextView image_desc;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

viewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item, null);
holder = new viewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.image_desc = (TextView) convertView
.findViewById(R.id.img_desc);
convertView.setTag(holder);
}
holder = (viewHolder) convertView.getTag();
if (position % 2 == 0) {
holder.image.setImageResource(R.drawable.ic_action_add_person);
holder.image_desc.setText("change");
}

return convertView;
}

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

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

}

您的主要 Activity : 在此 Activity 中,您需要要求用户输入他们想要查看的图像数量。当用户未输入值时,我没有添加任何类型的验证或错误消息,而是我捕获了可能的错误并使用它来分配默认值。您可以根据需要在此处应用其他类型的逻辑。

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

第二个 Activity :这是您定义和初始化 gridview 和适配器的 Activity 。确保您从之前的 Activity 中检索包并正确检索用户输入的数字。

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

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

关于android - 在 GridView 中面临问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24823806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com