gridview 删除 android,android – 从GridView中删除项目
我对Android开发很新,我觉得这很简单,但是,我没有设法在谷歌上发现同样问题的任何人.我有一个Gridview,其中填充了TextView(顶部有一个图像)和一个ImageButton(删除当前项目).我想要做的是删除我单击ImageButton的项目.这是我的主要:public class ActivityMain extends Activity{GridView gridview;pub
我对Android开发很新,我觉得这很简单,但是,我没有设法在谷歌上发现同样问题的任何人.
我有一个Gridview,其中填充了TextView(顶部有一个图像)和一个ImageButton(删除当前项目).我想要做的是删除我单击ImageButton的项目.
这是我的主要:
public class ActivityMain extends Activity
{
GridView gridview;
public GridAdapter mainActivityAdapter;
public ArrayList listService = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listService.add("Market");
listService.add("Recherche");
listService.add("Quiz");
gridview = (GridView) findViewById(R.id.mainActivity_grid);
mainActivityAdapter = new GridAdapter(this.getApplicationContext(), listService);
gridview.setAdapter(mainActivityAdapter);
}
}
这是我的适配器:
public class GridAdapter extends BaseAdapter
{
Context context;
ArrayList list = new ArrayList();
GridAdapter adapter = this;
public GridAdapter(Context context, ArrayList list)
{
this.context = context;
this.list = list;
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int arg0)
{
return null;
}
@Override
public long getItemId(int arg0)
{
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_gridmain, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.gridMain_text);
holder.close = (ImageButton) convertView.findViewById(R.id.mainActivity_delete);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
// Doing stuff on views
holder.close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg1)
{
// list.remove(position);
list.remove(position);
adapter.notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder
{
TextView textView;
ImageButton close;
}
}
问题是,当我点击一个ImageButton时,它总是被删除的最后一个项目被删除,我无法弄清楚为什么或如何解决这个问题.
谢谢.
==================
编辑:
这是我的activity_main.xml:
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DDDDDD"
android:orientation="vertical" >
android:id="@+id/mainActivity_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:clickable="false"
android:gravity="center"
android:horizontalSpacing="15dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
还有我的item_gridmain.xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/gridMain_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_dark"
/>
android:id="@+id/mainActivity_delete"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/deleteFav"
android:src="@drawable/boutoncroixfermer" />
解决方法:
每个项目都有ImageButton吗?你可以设置
holder.close.setTag(Integer.valueOf(position));
然后,您将所有位置隐藏在右侧按钮中.更改OnClickListener,如下所示:
close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
list.remove((Integer) v.getTag());
adapter.notifyDataSetChanged();
}
});
标签:android,adapter,gridview,android-gridview
来源: https://codeday.me/bug/20190517/1121872.html
更多推荐
所有评论(0)