Gridview OnItemClickListener 不工作,但为什么?
Gridview OnItemClickListener Not Working But Why?
我有一个基本适配器和 gridview。我正在使用适配器 gridview 填充项目。当用户触摸任何项目时,我想祝酒。但它从未显示过。
@petey 我更新了代码并为您添加了 AdapterB class。请检查。
代码:
@Override
protected void onPostExecute(final Boolean success)
{
final AdapterB ada = new AdapterB(context, WallPaperList);
gridView.setAdapter(ada);
if (progressDialog.isShowing())
{
progressDialog.dismiss();
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long identity) {
Toast.makeText(context,ada.getItem(position).get(POST_ID), LENGTH_LONG).show();
}
});
}
适配器 B Class:
public class AdapterB extends BaseAdapter {
private Context mContext;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public AdapterB(Context c,ArrayList<HashMap<String,String>> d) {
// TODO Auto-generated constructor stub
mContext = c;
data = d;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(mContext,true);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public HashMap<String, String> getItem(int position) {
HashMap<String, String> tar;
tar = data.get(position);
return tar;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if (convertView == null) {
grid = new View(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.mygrid, parent, false);
} else {
grid = (View) convertView;
}
ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart);
HashMap<String,String> tar;
tar = data.get(position);
imageLoader.DisplayImage(tar.get(FragmentB.FOTOYOL),imageView);
/*
grid.setId(Integer.parseInt(tar.get(FragmentB.POST_ID)));
grid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"Clicked "+position,Toast.LENGTH_LONG).show();
}
});*/
return grid;
}
}
布局Xml:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:descendantFocusability="blocksDescendants" />
我找到了解决办法。
我的填充项有一个复选框。我给它一个 属性 这样的;
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
成功了!但是我现在不知道怎么才能选中这些框。如果我找到了,我会在这里更新。
我有一个基本适配器和 gridview。我正在使用适配器 gridview 填充项目。当用户触摸任何项目时,我想祝酒。但它从未显示过。
@petey 我更新了代码并为您添加了 AdapterB class。请检查。
代码:
@Override
protected void onPostExecute(final Boolean success)
{
final AdapterB ada = new AdapterB(context, WallPaperList);
gridView.setAdapter(ada);
if (progressDialog.isShowing())
{
progressDialog.dismiss();
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long identity) {
Toast.makeText(context,ada.getItem(position).get(POST_ID), LENGTH_LONG).show();
}
});
}
适配器 B Class:
public class AdapterB extends BaseAdapter {
private Context mContext;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public AdapterB(Context c,ArrayList<HashMap<String,String>> d) {
// TODO Auto-generated constructor stub
mContext = c;
data = d;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(mContext,true);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public HashMap<String, String> getItem(int position) {
HashMap<String, String> tar;
tar = data.get(position);
return tar;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if (convertView == null) {
grid = new View(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.mygrid, parent, false);
} else {
grid = (View) convertView;
}
ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart);
HashMap<String,String> tar;
tar = data.get(position);
imageLoader.DisplayImage(tar.get(FragmentB.FOTOYOL),imageView);
/*
grid.setId(Integer.parseInt(tar.get(FragmentB.POST_ID)));
grid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"Clicked "+position,Toast.LENGTH_LONG).show();
}
});*/
return grid;
}
}
布局Xml:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:descendantFocusability="blocksDescendants" />
我找到了解决办法。
我的填充项有一个复选框。我给它一个 属性 这样的;
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
成功了!但是我现在不知道怎么才能选中这些框。如果我找到了,我会在这里更新。