I using custom adapter for displaying a list of items in my project,
Right now it is working fine.but now i got another requirement that i need to put a button on each item.
If put the button like that then the onItemClickListenter() for that item is not working ,instead onClickListener() for that button is working.
But according to my context both onClickListenter() for button and onItemClickListener() for that item should work.Can somebody please help me if know the technique.
Thanks in advance.
As Button is a focusable view thats why onItemClick isn't work. In ur_row.xml (where you placed that Button) add these attributes inside Button tag
android:focusable="false"
android:focusableInTouchMode="false"
Inside your CustomAdapter's getView(...) set onClickListener for that Button this will fire both onItemClick for ListView and onClick for Button.
You need a custom adapter:
This project contains 4 parts:
1.MainActivity
public class MainActivity extends Activity {
private ListView listView;
private CustomAdapter customAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
init();
}
/**
* Init Data
*/
public void init(){
List<String> listItems = new ArrayList<String>();
for(int i=0;i<10;i++) {
listItems.add("Test" + i);
}
listView = (ListView)findViewById(R.id.listView);
customAdapter = new CustomAdapter(MainActivity.this,R.layout.row_item,listItems);
listView.setAdapter(customAdapter);
}
}
2.MainActivity layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_activity">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
3.CustomAdapter
public class CustomAdapter extends ArrayAdapter<String> {
private Context context;
private TextView itemListText;
private Button itemButton;
private List<String> listValues;
public CustomAdapter(Context context, int resource, List<String> listValues) {
super(context, resource,listValues);
this.context = context;
this.listValues = listValues;
}
/**
* getView method is called for each item of ListView
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String currentValue = listValues.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_item, null);
itemListText = (TextView)convertView.findViewById(R.id.itemListText);
itemListText.setText(currentValue);
itemListText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"Text Working",Toast.LENGTH_SHORT).show();
}
});
itemButton = (Button)convertView.findViewById(R.id.itemButton);
//To lazy to implement interface
itemButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"Button Working",Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
4.Row Item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/itemListText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Select Text"
android:gravity="center_vertical"
android:layout_weight="0.7"/>
<Button
android:id="#+id/itemButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push me"
/>
</LinearLayout>
Output:
TextView touch action
Button touch action
rather than putting buttons on row use image views as many required and set on click listener to them, Since imageViews are not focusable
Related
When I have a simple ListView and I want to change the color of the TextBox showing the string of the item by clicking on it I use the following code:
public class ListViewActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ArrayList<String> list=new ArrayList<String>();
for (int i = 0; i < 100; i++) {
list.add(String.valueOf(i));
}
listView=(ListView) findViewById(R.id.listView1);
CustomArrayAdapter adapter=new CustomArrayAdapter(this,R.layout.listview_row,R.id.listviewRow,list);
listView.setAdapter(adapter);
}
class CustomArrayAdapter extends ArrayAdapter<String>{
public CustomArrayAdapter(#NonNull Context context, int resource, int textViewResourceId, #NonNull List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=super.getView(position,convertView,parent);
ViewGroup vg=(ViewGroup) view;
TextView txtView=(TextView) vg.getChildAt(0);
if (!txtView.hasOnClickListeners()){
txtView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t=(TextView) v;
t.setTextColor(0xffbababa);
t.invalidate();
}
});
}
return view;
}
}
}
and the layout files:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="gr.spunitrade.development.spda.ListViewActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1">
</ListView>
and
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/listviewRow"
/>
</LinearLayout>
When clicking on the items one can see that by scrolling down the ListView more items have been grayed out that where not clicked. The same behavior happens even if the graying out is performed on listView.setOnItemClickListener alternative implementation.
I cannot understand why this happens. The breakpoints on the OnClick event are hit only once.
I would approach this way.
Assuming you want to toggle color based on click.
Keep an array of booleans with size of your data source.
Initialize to false.
When clicked set to true and set color. Then in on create view set the color based on the boolean.
I am new to android programming and this task is really need for my school project. Please kindly help me.
I've string array List - (retrieved from csv)
list = new ArrayList<>(Arrays.asList("111,222,333,444,555,666".split(",")));
myList.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.cell,list));
The result is showing only line by line text of arrayList. I want to add button to each generated line by line to delete clicked row.
Please how can I do this. Thank you for understanding my problem.
You have to create a custom layout xml which having a single item then you will add your button to this layout along with any other items.
CustomLayout.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />
</RelativeLayout>
Now after creating custom item layout you need listview which holds all items.
MainActivity.xml
.
.
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
.
.
Now in java file just set adapter with our custom layout xml
.
.
list = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));
listview.setAdapter(new MyCustomAdapter(list, context) );
.
.
Custom adapter Class
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.CustomLayout, null);
}
//Handle TextView and display string from your list
TextView tvContact= (TextView)view.findViewById(R.id.tvContact);
tvContact.setText(list.get(position));
//Handle buttons and add onClickListeners
Button callbtn= (Button)view.findViewById(R.id.btn);
callbtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
.
}
});
return view;
}
}
We have need ListviewActivity for listing your data
SchoolAdapter which is custom adapter to inflate each individual row
activity_listview which is layout for ListviewActivity
view_listview_row which is required for each individual row
Now create all file as below
For ListviewActivity,
public class ListviewActivity extends AppCompatActivity {
private ListView mListview;
private ArrayList<String> mArrData;
private SchoolAdapter mAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mListview = (ListView) findViewById(R.id.listSchool);
// Set some data to array list
mArrData = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));
// Initialize adapter and set adapter to list view
mAdapter = new SchoolAdapter(ListviewActivity.this, mArrData);
mListview.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
For SchoolAdapter,
public class SchoolAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mArrSchoolData;
public SchoolAdapter(Context context, ArrayList arrSchoolData) {
super();
mContext = context;
mArrSchoolData = arrSchoolData;
}
public int getCount() {
// return the number of records
return mArrSchoolData.size();
}
// getView method is called for each item of ListView
public View getView(int position, View view, ViewGroup parent) {
// inflate the layout for each item of listView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.view_listview_row, parent, false);
// get the reference of textView and button
TextView txtSchoolTitle = (TextView) view.findViewById(R.id.txtSchoolTitle);
Button btnAction = (Button) view.findViewById(R.id.btnAction);
// Set the title and button name
txtSchoolTitle.setText(mArrSchoolData.get(position));
btnAction.setText("Action " + position);
// Click listener of button
btnAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Logic goes here
}
});
return view;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}}
For activity_listview,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1FFFF"
android:orientation="vertical">
<ListView
android:id="#+id/listSchool"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0000CC"
android:dividerHeight="0.1dp"></ListView>
For view_listview_row,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="7.5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="7.5dp">
<TextView
android:id="#+id/txtSchoolTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="20dp" />
<Button
android:id="#+id/btnAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Click Me" />
At last but not least, do not forgot to add your activity in manifest.xml
Create a custom list view in another file with the only content of each item in the list.
Then create a Custom Adapter extending BaseAdapter and bind it.
Please refer to this website for example.
https://looksok.wordpress.com/tag/listview-item-with-button/
OR
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
I am having difficulties setting OnCheckedChangeListener callback on my ToggleButton which is in one of rows of my list view. When I try to access the ToggleButton by using its resource id, it doesn't return null. It does return the toggle button. However, it is like that the returned refference doesn't belong to what I wanted. Here is my layout of one of the rows of my list view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="20dp">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mac_address"
android:layout_weight="1" />
<ToggleButton
android:text="ToggleButton123456"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toggle"
android:layout_weight="1"
android:checked="false" />
</LinearLayout>
And the MainActivity's layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.bora.advancedlistview.MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/mac_list" />
</LinearLayout>
Here is the code I have written to set my listview up and to try accessing the second row's ToggleButton.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] macAddresses = new String[] {
"a1:23:34:6f:2b",
"5f:4a:8c:00:5b"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_layout,
R.id.mac_address, macAddresses);
ListView listView = (ListView) findViewById(R.id.mac_list);
listView.setAdapter(adapter);
View row = adapter.getView(1, null, listView);
ToggleButton button = (ToggleButton) row.findViewById(R.id.toggle);
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println(isChecked);
}
});
}
}
I don't get some kind of null pointer exception. But there is also nothing happens when I click on the second toggle button. It should print the value of isChecked but nothing happens.
Also, last but not least, I would like to be able to change the state of one toggle button programmaticaly. But the problem is that it doesn't render the change to the screen. Example:
((ToggleButton) row.findViewById(R.id.togge)).toggle();
The change doesn't get render on the screen. Do I have to manually refresh? If yes, how can I do it?
You can create a custom ArrayAdapter, override the getView method, and add the OnCheckedChangedListener for the buttons there:
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] macAddresses = new String[] {
"a1:23:34:6f:2b",
"5f:4a:8c:00:5b"
};
ArrayAdapter<String> adapter = new MyArrayAdapter(this, R.layout.list_layout, R.id.mac_address, macAddresses);
ListView listView = (ListView) findViewById(R.id.mac_list);
listView.setAdapter(adapter);
}
}
MyArrayAdapter
public class MyArrayAdapter extends ArrayAdapter<String> {
public MyArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects){
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
ToggleButton button = (ToggleButton) convertView.findViewById(R.id.toggle);
final int POSITION = position;
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("Button " + POSITION + ": " + isChecked);
}
});
return convertView;
}
}
I have button, which on clicked displays a listview and on clicking the listview item it must print something on logcat. I implemented this one in such a way that the list view and its listener are present inside the button onclick listener.
All is working well except the listview listener is not getting called(i.e. nothing is printing on logcat when clicked on listview item)
Here is my listener.
public class MyClickListener implements View.OnClickListener {
LinearLayout parent;
LayoutInflater layoutInflater;
Context context;
Home home;
#Override
public void onClick(View view) {
parent = (LinearLayout) home.findViewById(R.id.main_view);
View childLayout = layoutInflater.inflate(R.layout.mylayout, (ViewGroup) view.findViewById(R.id.list_layout));
parent.addView(childLayout, 0);
LinearLayout layout = (LinearLayout) home.findViewById(R.id.main_view);
ListView list = (ListView) layout.findViewById(R.id.list);
list.setAdapter(new MyListAdapter(context));
lsit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("hello Android");
}
});
}
}
mylayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list"
android:padding="15dp"
android:divider="#android:color/transparent"
android:dividerHeight="5dp"
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"/>
</LinearLayout>
Home.java
public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button button= (Button) findViewById(R.id.but);
button.setOnClickListener(new MyClickListener(this,this,layoutInflater));
}
}
home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#color/background">
...
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/main_view">
</LinearLayout>
.....
Can anyone help
Edit 1
MyListAdapter.java
private class MyListAdapter extends BaseAdapter {
String[] items;
LayoutInflater inflater;
Context context;
public MyListAdapter(Context context) {
this.context = context;
Resources resources = context.getResources();
items = new String[]{resources.getString(R.string.q1), resources.getString(R.string.q2), resources.getString(R.string.q3)};
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = view;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view1 = inflater.inflate(R.layout.list_layout_item, null);
TextView question = (TextView) view1.findViewById(R.id.itemtext);
question.setText(items[i]);
return view1;
}
}
list_layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/itemStyle">
<TextView
android:id="#+id/itemtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:drawableRight="#drawable/disclosure_indicator"/>
</LinearLayout>
Remove this code from your onClick() method and implement outside of that onClick() method. Just set Adapter there, but implementation is wrong og onItemClickListener().
lsit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("hello Android");
}
});
Make sure that in your list_item_layout doesn't have any element is stealing event, like button or TextView with setClickable(true).
I am very new to android development and I feel like this is very simple, yet, I haven't managed to find anyone on google with the same problem.
I have a Gridview that is filled with a TextView (which has an image on top) and an ImageButton (to delete the current item). What I want to do is to remove the item of which I click the ImageButton.
Here is my Main :
public class ActivityMain extends Activity
{
GridView gridview;
public GridAdapter mainActivityAdapter;
public ArrayList<String> listService = new ArrayList<String>();
#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);
}
}
And here is my Adapter :
public class GridAdapter extends BaseAdapter
{
Context context;
ArrayList<String> list = new ArrayList<String>();
GridAdapter adapter = this;
public GridAdapter(Context context, ArrayList<String> 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;
}
}
The thing is, when I click on one ImageButton, it's always the last item that has been added that is being removed and I can't figure out why or how to fix this.
Thank you.
==================
EDIT :
Here is my activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DDDDDD"
android:orientation="vertical" >
<GridView
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" />
</LinearLayout>
And my item_gridmain.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
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"
/>
<ImageButton
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" />
</RelativeLayout>
You have ImageButton's in every item right? You can set
holder.close.setTag(Integer.valueOf(position));
And then, you have all positions hidden in the right buttons.Change the OnClickListener like below:
close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
list.remove((Integer) v.getTag());
adapter.notifyDataSetChanged();
}
});
Ok actually it was my own mistake and I feel incredibly dumb now.
I was actually removing the right item but in each view I was replacing each item at position with what was there in the first place. So each item was the correct one but the picture/text were the old one because I was modifying them inside getView().
Sorry for that lose of time, my bad, I want to punch myself now.
Please use ViewHolder pattern, and also add your gridMain_text.xml
else you will keep receiving wrong index of click
http://www.binpress.com/tutorial/smooth-out-your-listviews-with-a-viewholder/9
This is example of ListView but equally applicable for GridView
Please use
holder.close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg1)
{
// list.remove(position);
list.remove(position);
adapter.notifyDataSetChanged();
}
});
instead of close.setOnClickListener, i ran your this code an now it work fine
add Log.debug() in listener to ensure the remove position is right.
How do you know after click,the last item is removed?I mean,if there has text label in item view?Maybe you remove the right item,but after gridview refresh,it looks like the last item is removed.
You can add position label with setText in getView.That may helps you.
Ok,I know.Where did you do render item view?You must do it in getView.The AdapterView cached it's item view.As you removed the right position.May be the position is not the right view.You can run hierarchyviewer to detect what happen.