issue is this:
There are 49 items in the arraylist, and the adapter is stopping at item 3 without any error notification. The app does not crash, and no textviews are displayed. Here is the adapter:
public class ExhibitorObjectAdapter extends ArrayAdapter<ExhibitorObject> {
public ArrayList<ExhibitorObject> exhibitors;
public Activity act;
public TextView tvExhibitorName;
public ImageView ivExhibitorLogo;
public ExhibitorObjectAdapter(Activity a, int layoutResourceId, ArrayList<ExhibitorObject> ex) {
super(a, layoutResourceId,ex);
this.exhibitors = ex;
this.act = a;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return exhibitors.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_exhibitor, null);
}
ExhibitorObject ex = exhibitors.get(position);
if (ex != null) {
//System.out.println(ex.companyName);
tvExhibitorName = (TextView) v.findViewById(R.id.textViewListExhibitorName);
tvExhibitorName.setText( ex.companyName );
}
return v;
}
}
EDIT: here is the xml containing the listview:
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:background="#134882"
android:paddingBottom="15sp"
android:paddingTop="5sp"
android:paddingLeft="5sp"
android:textColor="#FFFFFF"
android:text="Exhibitors" />
<ListView android:id="#+id/listViewExhibitors"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
I have several other adapters that are exactly like this one and they work just fine. I have wasted an entire day on this (it's probably a silly problem), any help would be appreciated. Thanks.
It is bad to save tvExhibitorName in the scope of your adapter. Make it local within getView(), or you subject yourself to all kinds of leaks. That well may be the source of your problem.
Also, it is very strange that you don't inflate your new view under the parent. May be using inflate(layout, parent , false) will help.
Related
I have problem that my list view is not showing and the getView method never invoked
here is my list view inside the onCreate in the MainActivity
l = (ListView) findViewById(R.id.listvv);
CustomAdapter adapter = new CustomAdapter(MainActivity.this, maainimg);
l.setAdapter(adapter);
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/white"
tools:context=".MainActivity">
<ListView
android:id="#+id/listvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#b4be"
android:dividerHeight="2dp"></ListView>
<LinearLayout
android:id="#+id/ad_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_alignParentBottom="true">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
ads:adSize="BANNER"
ads:adUnitId="#string/BottomBanner">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
and here is my CustomAdapter Class it is inside the MainActivity
public class CustomAdapter extends ArrayAdapter<String> {
int[] img;
Activity activity;
ImageButton imageButton;
public CustomAdapter(Activity act, int [] images) {
super(act, R.layout.custom_row);
this.activity=act;
this.img=images;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("getView","Called!");
View row = convertView;
if(row==null){
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row, parent, false);
imageButton = (ImageButton) row.findViewById(R.id.preview);}
imageButton.setImageResource(img[position]);
Log.d("added",position+"");
if (getSelectedcolor() == 0) {
imageButton.setColorFilter(Color.RED);
setSelectedcolor(Color.RED);
} else {
imageButton.setColorFilter(Color.parseColor(colorToHexString(getSelectedcolor())));
}
return row;
}
}
and the custom_row.xml
<?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:background="#color/background">
<ImageButton
android:id="#+id/preview"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:src="#drawable/ic_1_red"
android:scaleType="fitXY"
android:background="#drawable/roundedbutton"
/>
</LinearLayout>
Update
here is my array maainimg its not empty
maainimg = new int[]{R.drawable.ic_1_red,R.drawable.ic_2_red,R.drawable.ic_3_red,R.drawable.ic_4_red,R.drawable.ic_5_red,R.drawable.ic_6_red,
R.drawable.ic_7,R.drawable.ic_8,R.drawable.ic_8,R.drawable.ic_9,R.drawable.ic_10,R.drawable.ic_11,R.drawable.ic_12
,R.drawable.ic_13,R.drawable.ic_14,R.drawable.ic_15,R.drawable.ic_16,R.drawable.ic_17,R.drawable.ic_18,R.drawable.ic_19,R.drawable.ic_20,
R.drawable.ic_21,R.drawable.ic_22,R.drawable.ic_23,R.drawable.ic_24,R.drawable.ic_25};
While you call super(act, R.layout.custom_row); (without passing the objects), you need to override getCount() method, otherwise your adapter can't figure out how many items you have, and this is why there is no call of getView() method.
#Override
public int getCount(){
return img.length;
}
The first problem I see is your Layout.
<ListView
android:id="#+id/listvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#b4be"
android:dividerHeight="2dp"></ListView>
<LinearLayout
android:id="#+id/ad_holder"
android:layout_width="fill_parent"
Your ListView has no room left because the bottom linear layout is using FILL_PARENT (which is deprecated btw, use MATCH_PARENT instead).
Try making your ListView height (and width) to be both MATCH_PARENT. And then deal with the "Ad".
You could have the LinearLayout replaced by a RelativeLayout and have the Ad pin at the bottom of its parent and below the listview.
Second: Why are you using an ArrayAdapter<String> if you're internally using an array of int.
Third: since you're using the wrong type of adapter, you're not correctly implementing it, you need to override more methods (getCount() for example), to tell the underlying adapter: hey, this is the number of items we have.
Since you're not using the provided array of strings, but your custom array of ints that you pass during construction, try overriding getCount() and return the size of your int array instead.
I this there is a issue in your Adapter class, Try this, But keep in mind that there are cleaner ways of writing this same code,
public class CustomAdapter extends ArrayAdapter<Integer> {
int[] img;
Context context;
ImageButton imageButton;
public CustomAdapter(Context context, Integer [] images) {
super(context, R.layout.custom_row, images);
this.context=context;
this.img=images;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("getView","Called!");
View row = convertView;
if(row==null){
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row, parent, false);
}
imageButton = (ImageButton) row.findViewById(R.id.preview);
imageButton.setImageResource(img[position]);
Log.d("added",position+"");
if (getSelectedcolor() == 0) {
imageButton.setColorFilter(Color.RED);
setSelectedcolor(Color.RED);
} else {
imageButton.setColorFilter(Color.parseColor(colorToHexString(getSelectedcolor())));
}
return row;
}
}
I have a ListView in a Fragment containing also some other UI-Elements. Everything works until I call setAdapter on the ListView. In that moment I can debug that the ListView is filled with the elements but immmediately after the whole fragment disappears (including the other UI-Elements). If I set the Adapter 0,1 seconds later, everything works.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_listview, null);
mListView = (ListView)mView.findViewById(R.id.list_items);
TextView emptyView = (TextView)mView.findViewById(R.id.text_empty);
mListView.setEmptyView(emptyView);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// this is the only work around to not make the list view disappear
loadContacts();
}
}, 100);
// if I call loadContacts() here, the Fragment disappears
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person p = mContacts.get(position);
if (pickerInterface != null) {
pickerInterface.pickedContact(p);
}
}
});
return mView;
}
public void loadContacts() {
mContacts = ContactsDataSource.getAllPhoneContacts(getActivity(), true);
mAdapter = new ContactsAdapter(getActivity());
mAdapter.mContacts = mContacts;
mAdapter.isPlainList = true;
// after the following line the Fragment is filled and then immediately disappears
mListView.setAdapter(mAdapter);
}
the ContactsAdapter is very simple:
public class ContactsAdapter extends BaseAdapter {
public List<Person> mContacts = new ArrayList<Person>();
public Activity context;
// Constructor
public ContactsAdapter(Activity c){
context = c;
}
#Override
public int getCount() {
return mContacts.size();
}
public Person getItem(int position){
return mContacts.get(position);
}
#Override
public boolean isEmpty()
{
return mContacts.size() == 0;
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_contact, null);
}
Person person = mContacts.get(position);
TextView tvName = (TextView) convertView.findViewById(R.id.text_name);
tvName.setText(person.getDisplayName());
return convertView;
}
}
I have spent hours debugging and searching for reasons and have no clue. Thanks for any idea.
Edit: XML as following:
<?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:background="#color/white" >
<ListView
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/line_horizontal"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/text_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:layout_alignParentTop="true"
android:text="#string/label_no_entries"
android:textColor="#color/grey"
android:textSize="21sp" />
<View
android:id="#+id/line_horizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#+id/layout_navigation"
android:background="#color/black" />
<RelativeLayout
android:id="#+id/layout_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:id="#+id/text_scrollback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:drawablePadding="10dp"
android:drawableRight="#drawable/backward"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:text="#string/activity_contacts_backward_label"
android:textColor="#color/mediumgrey" />
<View
android:id="#+id/view_coverback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_scrollback"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/text_scrollback"
android:layout_alignTop="#+id/text_scrollback"
android:background="#c0ffffff" />
<TextView
android:id="#+id/text_scrollforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:drawableLeft="#drawable/forward"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:text="#string/activity_contacts_forward_label"
android:textColor="#color/mediumgrey" />
<View
android:id="#+id/view_coverforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_scrollforward"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/text_scrollforward"
android:layout_alignTop="#+id/text_scrollforward"
android:background="#c0ffffff" />
<TextView
android:id="#+id/text_pages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/text_scrollforward"
android:layout_toLeftOf="#+id/text_scrollforward"
android:layout_toRightOf="#+id/text_scrollback"
android:gravity="center"
android:textColor="#color/mediumgrey"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
Where did you initialize your "mListView"?
The time that fragment inflated is "onCreateView()" and general way of getting view instances from layout is done on this method. like below
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, null);
mListView = (ListView)v.findViewById(R.id.list_items);
...
return v;
}
How about trying to initalize your views(ListViews, and others) in "onCreateView"?
I think that the problem is caused from getting view instances before fragment is inflated.
Not sure what your problem is. May be with the custom super class. Otherwise, wild guess here, but try inflating your view like this.
mView = inflater.inflate(R.layout.fragment_listview, container, false);
First of all, storing a context object can be dangerous and should therefore be avoided if possible... You can easily get rid of it in your adapter with following steps (that hopefully also solves your problem):
--> try following constructor for your adapter:
public ContactsAdapter(Context c){
super(c, R.layout.yourlayout_listitem_name;
}
--> and adjust the getView-method to use something like:
final View rowView = View.inflate(viewGroup.getContext(), R.layout.yourlayout_listitem_name, null);
instead of the inflater-if-block. then always return rowView at the end.
and just to be sure I would remove the overriden-method:
#Override
public long getItemId(int position) {
return position;
}
since it is not doing anything useful anyway ;-)
Thank you for all your support. By removing all code and re-assembling step by step I foundt the reason. Just in case someone runs in the same problem one day.
I had an OnScrollListener on the ListView declared in the superclass. In the OnScrollListener I was hiding the navigation Arrows on demand with Visibility.GONE. Even if the NavigationArrows did not affect the layout of the ListView this was the problem. By setting Visibility.INVISIBLE everything works fine. I guess this is related to some re-drawing-issue of the whole layout that does not work.
I have used a ListFragment with an ArrayAdapter to show a list of items. You can find the code below.
public class SelectRepiceFragment extends ListFragment {
private BuyRecipeActivity mActivity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = mActivity.getResources();
int recipeNum = res.getInteger(R.integer.recipe_number);
String[] titles = res.getStringArray(R.array.titles);
ArrayList<RecipeItem> recipeItems = new ArrayList<RecipeItem>(recipeNum);
for (int i = 0; i < recipeNum; i++) {
RecipeItem item = new RecipeItem();
item.title = titles[i];
recipeItems.add(item);
}
RecipeItemAdapter recipeItemAdapter = new RecipeItemAdapter(mActivity, recipeItems);
setListAdapter(recipeItemAdapter);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = (BuyRecipeActivity) activity;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
mActivity.onRecipeSelected(position);
}
}
The size of recipeItems is 3 (I check it with debugger). So the getView function in ArrayAdapter should be called 3 times to generate the ListFragment. Here I have copied the ArrayAdapter.
public class RecipeItemAdapter extends ArrayAdapter<RecipeItem> {
private final Activity mActivity;
static class ViewHolder {
public MyTextView title;
public ImageView image;
}
public RecipeItemAdapter(Activity context, ArrayList<RecipeItem> recipeItems) {
super(context, R.layout.recipe_list, recipeItems);
this.mActivity = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.recipe_item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (MyTextView) rowView.findViewById(R.id.recipeItemTitle);
viewHolder.image = (ImageView) rowView.findViewById(R.id.recipeItemImage);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
RecipeItem recipeItem = getItem(position);
holder.title.setText(recipeItem.title);
holder.image.setImageResource(R.drawable.ic_launcher);
return rowView;
}
}
The problem is that getView is actually called three times, while every time the position parameter is equal to 0. So After running the app, I just see a single row in my list instead of 3 rows.
UPDATE: I found out that I just see the first item, but if I scroll it up, then it shows the rest of items in that single row! Consequently I guessed there should be a mistake in the xml file. So I add xml files bellow.
This is my main activity xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/fragmentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
</ScrollView>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#dcdcdc"
android:choiceMode="singleChoice"
android:divider="#dcdcdc"
android:dividerHeight="0.5dp" />
</android.support.v4.widget.DrawerLayout>
And this is my recipe_item xml file.
<?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="100dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/recipeItemTitle"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="#drawable/design_price_view"
android:gravity="center" />
<ImageView
android:id="#+id/recipeItemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/app_icon" />
</LinearLayout>
UPDATE 2: I removed the DrawerLayout from my main activity xml and now I see the list. Just confusing.
You may try removing the ScrollView, take the FrameLayout out, put
layout_height="0dp"
layout_weight="1"
to the FrameLayout. You will need to manually fix the height of your navigation drawer to predefined pixel.
The above setup will make your FrameLayout & your ListView inside it take all the available space left from the navigational drawer.
The problem is, if you place a ListView inside a ScrollView - there is a rendering problem. I have not worked with ListView inside FrameLayout so cant really comment on that.
If you absolutely need to keep the ScrollView, you would need to re-render your ListView so that it spans the height it needs. Check this thread
I've got huge problem that I don't understand. I have got custom ListView with my own adapter. Each row in ListView has two TextViews - one is a title and the second one is invisible at start. It's going to be visible when there is something new in this item.
In my adapter I set title for every row and set second TextView to be visible when it should be. When I run my app it's fine, but when i scroll down and up the list almost every row is changing invisible TextView to be visible!
I don't know why, but I spaculate that this is something with convertView. Can anybody tell me what's going on?
My adapter:
public class MyListAdapter extends BaseAdapter {
#SuppressWarnings("unused")
private Activity activity;
private ArrayList<String> titles;
private LayoutInflater inflater;
public MyListAdapter (Activity activity, ArrayList<String> titles) {
this.activity = activity;
this.titles = titles;
inflater = (LayoutInflater)activity.getLayoutInflater();
}
public int getCount() {
return titles.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.wpis_list_row, null);
}
TextView title = (TextView)vi.findViewById(R.id.movie_title);
title.setText(titles.get(position));
String name = titles.get(position);
if (name.equals("Name 1") || name.equals("Name 2")
|| name.equals("Name 3")) {
TextView news = (TextView)vi.findViewById(R.id.new_sounds);
news.setVisible(0);
}
return vi;
}
}
and my layout for single row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/movie_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:layout_marginLeft="#dimen/list_margin"
android:text="#string/entry"
android:textColor="#FFFFFF"
android:textSize="22sp" />
<TextView
android:id="#+id/new_sounds"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#8C1717"
android:textSize="13sp"
android:text="#string/news"
android:visibility="invisible" />
</LinearLayout>
Sometimes you are setting the visibility of the text view as visible, based on some if condition . Make sure to change the visibility to gone in the else part.I hope this work . Also you are assigning view view= convertview and then doing if( convertview = null) . Assign the view to convert view in the else part of this check.
To make a View invisible in its layout, use android:visibility attribute set to gone or invisible depending on what you want to do.
Programmatically, to change visibility, call the View.setVisibility(VISIBILITY_YOU_WANT) method. There are some constants in View class you can use: View.GONE , View.VISIBLE , View.INVISIBLE.
BTW, read this post to better understand the ListView recycling.
I have custom list_row :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content" android:baselineAligned="false">
<Button android:layout_width="30dip" android:layout_marginTop="7dip" android:gravity="right"
android:id="#+id/delete" android:layout_height="30dip" android:background="#drawable/delete"
android:layout_gravity="top"></Button>
<TextView android:textSize="20dip"
android:text="TextView" android:id="#+id/tavsiye" android:layout_marginTop="10dip"
android:layout_gravity="top" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
I have a ListView like that:
<ListView
android:id="#+id/tavsiyeler"
android:layout_height="300dip"
android:layout_width="170dip"
android:fastScrollEnabled="true"
android:scrollbars="vertical"/>
and a custom adapter which extends ArrayAdapter :
public class HekimTavsiyeleriAdapter extends ArrayAdapter<String> {
private Context context;
private int resource;
private ArrayList<String> tavsiyeler;
public HekimTavsiyeleriAdapter(Context context, int resource,
ArrayList<String> objects) {
super(context, resource, objects);
this.context=context;
this.resource=resource;
this.tavsiyeler=objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(this.resource, null);
}
if (this.tavsiyeler.size()!=0) {
TextView tavsiye = (TextView) v.findViewById(R.id.tavsiye);
Button but= (Button) v.findViewById(R.id.delete);
if (tavsiye != null) {
String st=this.tavsiyeler.get(position);
tavsiye.setText(st);
}
if( but!=null){
but.setId(position);
but.setOnClickListener(new AdapterView.OnClickListener() {
#Override
public void onClick(View v) {
int id=v.getId();
tavsiyeler.remove(id);
notifyDataSetChanged();
}
});
}
}
return v;
}
I am creating adapter and fill the list like that :
eklenecekTavsiyeler=new ArrayList<String>();
adapter= new HekimTavsiyeleriAdapter(context,
R.layout.hekim_tavsiyeleri_row, eklenecekTavsiyeler);
ListView tavsiyelerListesi = (ListView)findViewById(R.id.tavsiyeler);
tavsiyelerListesi.setAdapter(adapter);
And adding new items like that:
this.adapter.add(<some-string>);
this.adapter.notifyDataSetChanged();
and my list view is seen like that:
http://imageshack.us/photo/my-images/97/listir.jpg/
Here is my question:
I am adding new items to the list. I have fixed height for the list. When I fill the list until all height is occupied, then I add one new item to the list which requires scrolling becasue overflow in list height. The last item I added gets wrong id and when I pressed the cross button, it removes wrong item. However, when the list is not overflowed, everything works fine. After overflow, the ids of buttons are set wrongly (seems randomly). By the way, for setting the button's id, I am using getView's position argument.
Thanks in advance.
I am afraid that you have flaw in the code.
You have to stop calling but.setId(). With this you are overriding internal id of the view which is the value of R.id.delete. Probably you meant to use but.setTag() / but.getTag()?