【发布时间】:2015-02-24 07:00:44
【问题描述】:
在 Android Studio 中编写一个 Android 应用时,我有一个 GridView 来显示一组图像/文本,但我无法确定如何访问列表中的特定项目。
如果我点击一个项目,我可以对它做一些事情(在下面的代码中,我更改了显示的图像),但我不知道如何以编程方式访问特定项目(例如更改显示的图像) .
最初我使用了一个示例中的 ImageAdapter,但后来我发现如果我将其更改为使用 TextView,我可以为每个文本以及一个图像分配文本,并且它可以很好地与 Talkback(Android 屏幕阅读器)一起使用.
我最初也将 OnTouchListener 添加到活动中,但我认为我现在不需要它,因为我只需要在适配器中实现它。
最后,我对究竟需要在这里访问什么感到有点困惑——我是想从 GridView 中取出项目,还是从 ImageAdapter 中取出项目?我认为 GridView 设置布局并定义在集合中放置每个项目的位置,而 ImageAdapter 是用于定义集合中每个项目的属性的项目数组的容器,对吗?
亲切的问候
昆汀。
public class multitap extends Activity implements View.OnTouchListener {
private GridView myGridView;
public int numRows;
public int numCols;
public int currentChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multitap);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
numRows = 1;
numCols = 1;
} else {
numRows = extras.getInt("NUMBER_OF_GAME_ROWS");
numCols = extras.getInt("NUMBER_OF_GAME_COLS");
}
} else {
numRows = (int) savedInstanceState.getSerializable("NUMBER_OF_GAME_ROWS");
numCols = (int) savedInstanceState.getSerializable("NUMBER_OF_GAME_COLS");
}
myGridView = (GridView)
findViewById(R.id.myGridView);
myGridView.setNumColumns(numCols);
myGridView.setAdapter(new ImageAdapter(this));
// now that everything is initialised, choose a random image tile.
Random r = new Random();
choice = r.nextInt(numCols*numRows);
// This is the main thing I can't figure out - what do I need to replace "DoSomethingToFindItem" with?
chosenItem = DoSomethingToFindItem(choice);
// change the image of the chosen tile.
chosenItem.setBackgroundResource(R.drawable.newImage);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// turns out I don't need this as onTouch in adapter collects touch event
return false;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
//not really sure what mConext is?
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() { return numCols*numRows; }
public Object getItem(int position) {
// Is this right? All of the examples on the net use "return null" claiming they it's not needed (for their example)
return this;
}
public long getItemId(int position) {
// I know I should return something other than position here, but don't know what?
// again every example doesn't feel the need for this function.
return position;
}
// create a new TextView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
} else {
textView = (TextView) convertView;
}
textView.setBackgroundResource(R.drawable.blankcell);
textView.setText("" + (position + 1));
textView.setTextColor(getResources().getColor(android.R.color.transparent));
textView.setId(position);
textView.setMaxHeight(parent.getHeight() / numRows);
textView.setMinimumHeight(parent.getHeight() / numRows);
textView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int position;
position = v.getId();
// looking to get the first touch of any finger - this part works.
if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)) {
TextView textView;
if (v == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
} else {
textView = (TextView) v;
}
// I can set a specific image by clicking on it.
textView.setBackgroundResource(R.drawable.clickedImage);
return true;
} else return false;
}
;});
return textView;
}
}
}
【问题讨论】:
-
谢谢,我试过了,得到一个空指针异常。我尝试添加一条 toast 消息,指示 numVisibleChildren 和 firstVisiblePosition 的值,它们都返回 0,这是我没有预料到的 - 我已经初始化了所有内容,如果我不包含此代码块,除了视图之外什么都不会调用然后在屏幕上可见 - 在之前的初始化完成并绘制所有内容之前运行这段代码是否存在某种问题?
-
解决了您的问题?
-
抱歉 - 我发现我在写回复时无法按 ENTER - 请参阅我的回复中的编辑 :)
-
GridView 对象列表存储在哪里?