我无法访问gridview中的按钮。我的gridview使用customAdpater放置在对话框中。请帮我!!! 以下是我的代码
1)在onCreate()方法中:
dialog = new Dialog(this);
dialog.setContentView(R.layout.question_pallet);
dialog.setTitle("Question Pallet...");
gridView = (GridView) dialog.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(QuestionBankActivity.this,allQuestions));
Button b = (Button) gridView.findViewWithTag("Hi"); //returning null
Button b = (Button) gridView.getChildAt(0); //returning null
2)question_pallet.xml就像这样
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="70dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
3)我的grdview的ImageAdapter.java是
public class ImageAdapter extends BaseAdapter {
private Activity mContext;
List<GATEQuesAnswer> allQuestions;
static Button b[] = new Button[65] ;
int next_counter = 1;
private int pos;
// Constructor
public ImageAdapter(Activity c, List<GATEQuesAnswer> allgeteQuestions){
mContext = c;
allQuestions = allgeteQuestions;
}
public ImageAdapter(QuestionBankActivity questionBankActivity) {
mContext = questionBankActivity;
}
@Override
public int getCount() {
return 65;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Button button;
//if (convertView == null) {
button = new Button(mContext);
button.setText(Integer.toString(pos++));
button.setId(position);
button.setTag("Hi");
//}
//else
//{
//button = (Button) convertView;
//Log.i("mytag", b.length+"");
//}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
((QuestionBankActivity)mContext).navigate_to_ques(position+1);
}
});
button.setLayoutParams(new GridView.LayoutParams(70, 70));
return button;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
}
我已尝试过所有可能性,例如为每个按钮提供唯一ID并使用gridview.findViewById()或getChildAt(index)获取它,但没有任何工作:( 任何帮助将不胜感激!!! 提前谢谢!
答案 0 :(得分:0)
你的getView方法都错了,试试类似......
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.button = (Button) convertView
.findViewById(R.id.ID_OF_YOUR_BUTTON);
holder.button.setTag("HI");
convertView.setTag(holder);
holder.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//DO YOUR CODE
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
private class ViewHolder {
Button button;
}
//编辑
此外,您可以使用getChildAt()迭代 GridView (或任何其他ViewGroup)的所有子项:
for(int i=0; i<gridView.getChildCount(); i++) {
Button child = (Button) gridView.getChildAt(i);
// do stuff with child view
}
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
</LinearLayout>