spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

ListView is reusing views and I lose information after scrolling


In your getView method you have to check whether the row was already clicked, so your code should be something like this:

@Override
public View getView(int position, View
convertView, ViewGroup parent) {
try {
    Station item = getItem(position);
    View v = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(layoutResourceId,
null);
    } else {
        v = convertView;
    }

    TextView stationName = (TextView)
v.findViewById(R.id.stationName);
    stationName.setText(item.getStationName());

    if (head == position) { //first click
        v.setBackgroundColor(Color.GREEN);
    } else if (terminal == position){  //second
click
        terminal = position;
        v.setBackgroundColor(Color.RED); 
    } else {
        v.setBackgroundColor(Color.WHITE);
    }

    ImageView line_image = (ImageView)
v.findViewById(R.id.line);

   
line_image.setBackgroundColor(Color.parseColor(setLineColor(line.getLineName())));
   
line_image.setImageResource(R.drawable.ic_line_simple);

   return v;
} catch (Exception ex) {
    Log.e(LOG_TAG, "error", ex);
    return null;
}
}

And the code of the setOnItemClickListener:

lv.setOnItemClickListener(new
android.widget.AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?>
parent, View view,
            int position, long id) {

        station =
(Station)parent.getItemAtPosition(position);
        if (head ==-1) { //first click
            head = position;
            myAdapter.setHead(head);
            view.setBackgroundColor(Color.GREEN);
        }
        else if (terminal ==-1){  //second click
            terminal = position;
            myAdapter.setTerminal(terminal);
            view.setBackgroundColor(Color.RED); 
     }
 }

The reason for this behaviour is that when row disappears and comes back, the getView method will be called again. So in your getView method you have to take into account the state of the row. Therefore you need the state variables head and terminal in your adapter.


Categories : Android

Related to : ListView is reusing views and I lose information after scrolling
ListView scrolling animation
The ViewTransition provides a lot of interesting examples on how to animate a ListView for operations like populate (the transition for the initial items at component creation), add, remove (self-explanatory) as well as other operations. Given a ListView you define an element Transition for each operation you want to animate. The animation framework can be exploited to create compound animations,

Categories : Qt
Android - Listview only works after scrolling
i've seen some problems related to getLayoutInflater... so in your getView method i suggest you to change your LayoutInflater construction: //(...) if(convertView==null){ LayoutInflater inflater = LayoutInflater.from(context); row = inflater.inflate(R.layout.club_board_post, parent, false); //(...) hope to be helpful.

Categories : Android
Listview containing checkbox and text is getting reset on scrolling
It because of recycling use of views in ListView. You should create some HashMap: HashMap<Integer, Boolean> checked; Then in your constructor do this: checked = new HashMap<Integer, Boolean>(getCount()); After set OnCheckedChangeListener on your checkboxes, and in event method do this: checked.put(position, yourCheckBoxCheckedState); And in getView() method do this: Boolean i

Categories : Javascript
How to stop onDraw() methods causing jerky scrolling of a ListView
A few things: 1) Post more source 2) read http://developer.android.com/training/improving-layouts/smooth-scrolling.html 3) consider using an ArrayAdapter http://developer.android.com/reference/android/widget/ArrayAdapter.html You may want to track the number of times you call create bitmap, and how garbage collection is being handled there. If you're not properly recycling your views, and yo

Categories : Android
How to dynamically show an image on certain child views of a expandable listview
You don't need to recycle views, from the looks of it. Don't do it if you don't have to. (All the docs make it sound like a requirement and all the examples use it, but it isn't required. It's just "good practice" but when a "good practice" takes 10x as long as getting the job done should take, it really isn't a good practice is it??) The concept behind recycling has to do with scrolling through

Categories : Android
Recently Add
I'm trying to create an onListItemClick even for a customized adapter. The array of items is found in res/values folder
No navigation/status bar touch listener exists, why?
z-index in Relative layout
java.lang.SecurityException: Permission Denial: getIntentSender() when using UiAutomation in a test
mvvmcross videoview URL binding
Android Emulator Unfortunately dev tools has stopped
Square Dagger error: class is exclusively members injected / cannot be scoped required
Installing a widget from an APP
How to stop onDraw() methods causing jerky scrolling of a ListView
How to find my Google Play Services Android BASE64_PUBLIC_KEY
Set style programatically does not work
How to get contacts from phonebook and sim but skype, facebook etc. in android
Where to put JUnit test in Android project
My Android Studio project keeps running my old project/application
how to avoid OUtOfMemory problems with create Bitmap
What tools use and how do continuous integration and test in Android with eclipse?
Converting drawable resource to Bitmap Android
Fragment Transaction show() does not show fragment on second time
Espresso - How to match ActionBar visibility?
Viewport setting ignored in Phonegap Android app, but only on certain phones?
how to open an activity with url scheme android
Android AsyncTask and Thread different behavior
How can one ellicit a Toast from a Service to a Fragment
GCMIntentService onRegistered not called
LinearLayout cropping but not expanding in Scrollview
Get a file from the application directory
How to set layout params using view's on touch listener?
How to disable Google Fit and revoke permissions from the app itself
Android Wear - receiving message from Wear without WearableListenerService?
How can I start new activity from another package?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.