Why does my the listview position items wrongly? - java

I'm coding a scrollable listview in android with textview and edittext in each row. Because there's the issue when you scroll, that the new data gets lost i update my ArrayList in an TextChangedListener like this:
TextWatcher txtwatch = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
if (!scrolling) {
InhalteArr.set(position, s.toString());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
My problem is now, that this method is working, but only for the seven first rows. The following rows are showing the right information, but they don't set the updated data at the right position in the ArrayList.
I can't understand why the first seven rows are doing what I want and the rest seems to begin setting the updated data upcounting from zero again and not going on with eight.
It's very creepy because all rows also the last rows seems to get the right data from ArrayList, but the last view rows are setting the data at wrong place in ArrayList.
In getView() Method I'm setting the data like this
viewHolder.Inhalt.setText(InhalteArr.get(position).toString());
but that works.
Has anyone any idea?
Thank you!
EDIT: Here's my adapter:
public class SyAdapter extends ArrayAdapter<ArrayList> {
Typeface bahn = Typeface.createFromAsset(getContext().getAssets(), "fonts/bahnschrift.ttf");
Typeface ih = Typeface.createFromAsset(getContext().getAssets(), "fonts/corbel.ttf");
private static class ViewHolder {
TextView Merkmal;
TextView Inhalt;
}
ArrayList MerkmaleArr;
ArrayList InhalteArr;
public SyAdapter (Context context, ArrayList Merkmale, ArrayList Inhalte) {
super(context, R.layout.listanzeige, R.id.MerkmalT, Merkmale);
this.MerkmaleArr = Merkmale;
this.InhalteArr = Inhalte;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listanzeige, parent, false);
viewHolder.Merkmal = (TextView) convertView.findViewById(R.id.MerkmalT);
viewHolder.Merkmal.setTypeface(bahn);
viewHolder.Inhalt = (TextView) convertView.findViewById(R.id.InhaltT);
viewHolder.Inhalt.setTypeface(ih);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
System.out.println("Aktuelle Position: " + position);
viewHolder.Inhalt.setText(InhalteArr.get(position).toString());
viewHolder.Merkmal.setText(MerkmaleArr.get(position).toString());
return convertView;
}}
EDIT2: Now i figured out that all rows with position >7 have at the beginning already convertView != null, what means, that they skip the if (convertView == null) code. But why?

Assuming size of InhalteArr and MerkmaleArr is same
Add this method to your adapter class
#Override
public int getCount() {
return InhalteArr.size();
}
Adapter Class refinement Example:
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(R.layout.listanzeige, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.Inhalt.setText(InhalteArr.get(position).toString());
viewHolder.Merkmal.setText(MerkmaleArr.get(position).toString());
return convertView;
}
private class ViewHolder {
final EditText Inhalt;
final TextView Merkmal;
public ViewHolder(View v) {
Inhalt = v.findViewById(R.id.InhaltT);
Merkmal = v.findViewById(R.id.MerkmalT);
}
}

For those who have the same problem:
I solved my issue setting a tag out of the if(convertview == null), at first i was trying to set the tags into that ifmethod, but that won't work at all rows.
To get the edittext'stag into the onTextChangeListeneri found some good code here at stackoverflow:
View focView = activity.getCurrentFocus();
if (focView != null) {
EditText edit = (EditText) focView.findViewById(R.id.tvinhalt);
if (edit != null && edit.getText().toString().equals(s.toString())) {
int positionof = Integer.parseInt(edit.getTag().toString());
System.out.println("Tag= " + positionof);
YouArray.set(positionof, s.toString());
}
Hope with this answer you don't have to invest as much time as me to understand, that the tag must been set outside of that iffunction :D

Related

The method is being applied wrong elements in listview

I have a problem about listview. Each item of listview have an imageview and a textview. I extended BaseAdapter class for Listview adapter and overrided some methods that I must override. By the way I want to shrink the size of text in the textview if greater than 25. For this reason I created a method whose name is "shrinkText()". When I execute the application first time, this method works correctly.So the textviews whose size of text grater than 25 have been shrinked and other textviews keep their size. However, when I scrool down the listview, textviews that their text size less than 25 have been shrinked too. What should I do to fix this? Thanks
My listview adapter...
public class ListAdapter extends BaseAdapter{
private final ProgramInfo values;
private LayoutInflater mInflater;
public ListAdapter(Context context, ProgramInfo values) {
this.values = values;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount(){
return values.getSize();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.brd_stream_list_item,parent,false);
viewHolder = new ViewHolder();
viewHolder.p_Name = (TextView)convertView.findViewById(R.id.prgName);
viewHolder.p_Image = (ImageView)convertView.findViewById(R.id.prgImage);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.p_Name.setText(values.programNames.get(position));
viewHolder.p_Image.setImageResource(R.drawable.alarm_clock_ed);
CharSequence text = viewHolder.p_Name.getText();
shrinkText(text,viewHolder.p_Name); //Call for shrink
}
My shrinkText() method..
private void shrinkText(CharSequence text, TextView v){
if(text.length()>25){
v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
}
}
You need to add the else statement :
private void shrinkText(CharSequence text, TextView v){
if(text.length()>25){
v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
}
else {
v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25); //the default text size
}
}
A tips : to avoid bug, always use if-else statement in getView (not only if).

Custom BaseAdapter getview get wrong positions android

I know questions like this one was already asked but I went through all the solutions I saw and non has worked.
This is my adapter
class Adapter extends BaseAdapter {
Activity context;
public Adapter(Activity context) {
this.context = context;
}
#Override
public int getCount() {
return Constants.CHARACTERS_NAMES.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = context.getLayoutInflater().inflate(R.layout.list_charecter_item, null);
holder = new ViewHolder();
holder.PlayerName = (TextView) convertView.findViewById(R.id.name);
holder.PlayerName.setText(Constants.CHARACTERS_NAMES[position]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ViewHolder {
TextView PlayerName;
}
}
This is a standard adapter but when I debug it with my listview Instead of getting All my 7 items which are in Constants.CHARACTERS_NAMES I get the 4 first items right, but the I get the first item twice and then the second item.
Please help.

getView in ListView Android stopped working and shows only first data

I made some changes in some files (not the ones I am having the problem - or I think so) and suddendly my ListView stopped working. The problems is that, athough the data passed as an argument are correcct, the ListView generates only the 0 position.
However, I cannot unerstand why. Here is my code:
public BussinessAdapter(Context context, ArrayList<BussinessListClass> listData) {
this.listbussiness = listData;
System.out.println("Business Adapter : Result :"+listData.toString());
layoutInflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
.cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(20)).build();
imageloader = ImageLoader.getInstance();
imageloader.init(ImageLoaderConfiguration.createDefault(context));
}
#Override
public int getCount() {
System.out.println("Total number is:"+listbussiness.size());
return listbussiness.size();
}
#Override
public Object getItem(int position) {
return listbussiness.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
System.out.println("Called at position :" + position);
holder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.bussinesslist, null);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.title = (TextView) convertView.findViewById(R.id.titleNearby);
holder.imageView = (ImageButton) convertView.findViewById(R.id.thumbImageNearby);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BussinessListClass item = (BussinessListClass) listbussiness.get(position);
holder.name.setText(item.getName());
holder.title.setText(item.getTitle());
if (holder.imageView != null) {
holder.imageView.setTag(item.getPhoto() + item.getName());
imageloader.displayImage(item.getPhoto(), holder.imageView, options, animateFirstListener);
}
return convertView;
}
The weird thing is this:
Data are being printed out correctly
in the getCount I do see 4, which is the size of the ArrayList
but the getView gets called only for position 0.
Can anyone help me on that? it used to work last weeek :/
SOLUTION: problem was in xml, my height beame something like 10 times more, so I thought there was only 1 item but it was because I had to scroll and scroll to see the others.
have u changed the size of listview, are u able to scroll it and see more items. It probably is calling only for one position and it can only show that at any time

Android Listview clickable textview conflict

I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from "Accept" to "Accepted". But when i click it on first listview item, another textview listview items below are changing. Here's some photos to descibe you better
Activity class
feedListView.setAdapter(new CustomListAdapter(this, feedList));
adapter class
public class CustomListAdapter extends BaseAdapter
{
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData)
{
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount()
{
return listData.size();
}
#Override
public Object getItem(int position)
{
return listData.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.title);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("accepted");
}
}
);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(newsItem);
return convertView;
}
static class ViewHolder
{
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
}
}
textview code
<TextView
android:id="#id/approveTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="#drawable/pressed"
android:gravity="fill"
android:text="Accept"
android:clickable="true"
android:textColor="#0D98BA"
android:textSize="17sp" />
You have to write some tracking position functionality which remember the position which you have changed and make sure that only text of position changed .
you must set id to each row, and save it in one array,like integer and when you click on one row you must change the value of that, and in changing text or anything you must check the value of that row,if 0 then set default and if 1 then so what you want: my idea is create int[] with length of that is size of your list,
first you must define you int[] like below and set to zero every index.
int[] selected = new int[listData.getsize()]
something like this in getview:
holder.approve.setId(Html.fromHtml(newsItem.getId());
and after else statement in getView:
if (selected[position] == 0)
{
// set default
}
else
{
// any changing that you want
}
and in onClick:
selected[holder.approve.getId()] = 1;
// any change that you want
Try the below code:-
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder.headlineView = (TextView)convertView.findViewById(R.id.title);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("accepted");
}
}
);
return convertView;
}
Thanks!!

Delete item from listview

I have listview which contains textview and buttons. When i delete listview item and i try to scroll down, i get exception on this:
BuildQueue eile = countryList.get(position);
Exception:
02-08 19:11:04.279: E/AndroidRuntime(10509): java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15
Seems i do not updating something when i delete item from listview. I think i have problem with ViewHolder, but i do not know what kind of...
My ArrayAdapter code:
public class MyCustomAdapter extends ArrayAdapter<BuildQueue> {
private ArrayList<BuildQueue> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<BuildQueue> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<BuildQueue>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
TextView field;
Button del;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.queue_buildings, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.field = (TextView) convertView.findViewById(R.id.field_text);
holder.del = (Button) convertView.findViewById(R.id.del_button);
convertView.setTag(holder);
holder.del.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button del_button = (Button) v;
BuildQueue building = (BuildQueue) del_button.getTag();
countryList.remove(building);
dataAdapter.notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
BuildQueue eile = countryList.get(position);
holder.code.setText(" ( Level: " + eile.getOld_level() + " to "+eile.getNew_level()+")");
holder.field.setText(eile.getNameSort());
holder.field.setTag(eile);
holder.del.setText("Delete");
holder.del.setTag(eile);
return convertView;
}
}
You are using a two arrays in your Adapter, but only changing one of them.
Every Adapter uses getCount() to determine how many row should be drawn. ArrayAdapter's getCount() simply asks for the size of the array that you pass to the super constructor here: super(context, textViewResourceId, countryList);. But you are also using a second, local array and when you delete a value from this countryList getCount() has no idea this happened which results in getView() throwing an IndexOutOfBoundsException...
Either extend BaseAdapter, or use ArrrayAdapter's methods like getItem(), add(), and remove() and remove your local data set.

Categories