Reputation: 311
Well After inserting the text and image by using the Onclick Listener Functionality when the Screen becomes full i.e when The list becomes Scrollable and when I try to add the new item and when I scroll down the list,I see the first Item of the List has been added to the list instead of current item which has to be added to the list.
CustomListView
public class CustomListView extends ListActivity {
ArrayList<Item> imageArry = new ArrayList<Item>();
//CustomImageAdapter adapter;
private MyCustomAdapter mAdapter;
Button a, b;
EditText abc;
String result;
ListView dataList;
Item abcd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
abc = (EditText) findViewById(R.id.one);
a = (Button) findViewById(R.id.button);
b = (Button) findViewById(R.id.button2);
mAdapter = new MyCustomAdapter(this);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result = abc.getText().toString();
mAdapter.addItem(new Item(result));
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAdapter.addSeparatorItem(new Item(R.drawable.google));
}
});
setListAdapter(mAdapter);
}
}
MyCustomAdapter
public class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
Context context;
private ArrayList<Item> mData = new ArrayList<Item>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public void addItem(final Item item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final Item item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Item getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Item myImage = mData.get(position);
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.textlayout, null);
holder.textView = (TextView) convertView.findViewById(R.id.resulttext);
holder.textView.setText(myImage.getName());
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.resultimage, null);
holder.img = (ImageView) convertView.findViewById(R.id.resultimage);
holder.img.setImageResource(myImage.getImage());
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public static class ViewHolder {
public TextView textView;
public ImageView img;
}
}
Item.java
public class Item {
int image;
String name;
public Item(int image, String name) {
super();
this.image = image;
this.name = name;
}
public Item(String name) {
super();
this.name = name;
}
public Item() {
}
public Item(int image) {
super();
this.image=image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.55"
android:cacheColorHint="#00000000"></ListView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/one"
android:layout_width="230dp"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a"
android:id="@+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b"
android:id="@+id/button2"/>
</LinearLayout>
textlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resulttext"/>
resultimage.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/resultimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Upvotes: 1
Views: 97
Reputation: 3047
Issue is in getView method of Adapter, In listview, the view that is going off the screen will be reused instead of creating a new view, so the view will have the details for the first view.
Try replacing your getView method with the below one, it should solve your problem of first item appearing again.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Item myImage = mData.get(position);
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.textlayout, null);
holder.textView = (TextView) convertView.findViewById(R.id.resulttext);
//holder.textView.setText(myImage.getName());
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.resultimage, null);
holder.img = (ImageView) convertView.findViewById(R.id.resultimage);
// holder.img.setImageResource(myImage.getImage());
break;
}
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (type) {
case TYPE_ITEM:
holder.textView.setText(myImage.getName());
break;
case TYPE_SEPARATOR:
holder.img.setImageResource(myImage.getImage());
break;
}
convertView.setTag(holder);
return convertView;
}
So, we need to set the details again the view which is recycled.
Upvotes: 1