我需要在网格视图上创建ItemClickListener。我在gridvie上动态添加按钮。使用Base适配器。我需要在我的活动中使用onItemClickListener,而不是在baseadapter类上。
我的xml代码:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
/>
DirectOrder类:
public class DIrectOrder extends Activity {
DbHelper db;
ArrayList<HashMap<String, String>> category = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
BaseAdapter baseAdapter;
LinearLayout categroyLayout;
String strCategory = "";
GridView grdView;
static String[] itemName = new String[10];;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.cash_directorder);
db = new DbHelper(DIrectOrder.this);
grdView = (GridView) findViewById(R.id.gridview);
categroyLayout = (LinearLayout) findViewById(R.id.lay_category);
category = db.retrieve_category_name();
strCategory = category.get(0).get("name");
items = db.retrieve_item_details(strCategory);
for (int i = 0; i < category.size(); i++) {
Button butCategory = new Button(this);
butCategory.setText(category.get(i).get("name"));
butCategory.setId(Integer.parseInt(items.get(i).get("Id")));
categroyLayout.addView(butCategory);
}
Log.i("Item name ::::", "" + items.get(0).get("Item_Name"));
for (int i = 0; i < items.size(); i++) {
itemName[i] = items.get(i).get("Item_Name");
}
grdView.setAdapter(new categoryAdapter(this, itemName));
// I need on click of the gridview on here //
grdView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.i("Clciking", "Click");
}
});
}
}
我的基础适配器类:
public class categoryAdapter extends BaseAdapter {
private Context mContext;
String[] itemname;
public categoryAdapter(Context c, String[] itemname) {
mContext = c;
this.itemname = itemname;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemname.length;
}
@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;
}
@SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Button butCatrgory = new Button(mContext);
butCatrgory.setId(position);
butCatrgory.setHeight(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
butCatrgory.setText(itemname[position]);
return butCatrgory;
}
}
答案 0 :(得分:1)
在但是Catrogory.setId(position);
之后,在MyBaseAdapter类的'getview方法中添加以下行butCatrgory.setEnabled(false);
butCatrgory.setClickable(false);
答案 1 :(得分:0)
Button
默认是可聚焦的,应该阻止调用OnItemClickListener。添加
butCatrgory.setFocusable(false);
在getView