Android-dev.it Community

 
Annunci

:arrow: Benvenuto!, se non sei ancora un utente registrato: puoi registrati qui, oppure effettua il login
:arrow: Apri un forum sul tuo nuovo smartphone Android e collabora con noi! (dettagli)

www.agendadigitale.org
Image


Sponsor
It is currently Thu May 24, 2012 8:35 am

All times are UTC + 1 hour


 Topics   Replies   Views   Last post 
No new posts Applicazione per gestire SMS

by filippo729 on Wed May 23, 2012 9:39 pm in Sviluppo e programmazione in Android

0

18

Wed May 23, 2012 9:39 pm

filippo729

No new posts На интернет-витрине

by popowahgjfghjoiuyt on Sat May 19, 2012 2:25 pm in Amministrazione

0

18

Sat May 19, 2012 2:25 pm

popowahgjfghjoiuyt

No new posts Apri un forum sul tuo nuovo smartphone Android!

[ Go to page: 1, 2, 3 ]

by ReattileGar on Fri May 18, 2012 5:27 pm in Amministrazione

27

12556

Fri May 18, 2012 5:27 pm

ReattileGar

No new posts Новый: базы для хрумера - профессиональный софт для СЕО.

by Renatafe on Fri May 18, 2012 3:14 pm in Amministrazione

0

27

Fri May 18, 2012 3:14 pm

Renatafe

No new posts Pace is undoubtedly the vital affordable Jerseys Wholesale d

by barnettmmolive2012 on Wed May 16, 2012 7:36 am in Amministrazione

1

49

Wed May 16, 2012 7:36 am

barnettmmolive2012




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Problema del "recycling views"
PostPosted: Wed Aug 24, 2011 4:57 pm 
Offline
User avatar

Joined: Tue Aug 23, 2011 12:30 am
Posts: 2
Punti Android: ?
Salve a tutti sono una new entry nella programmazione Android. Ho già studiato un paio di libri (Android Programming Tutorials e The Busy Coder's) ma forse per miei limiti non riesco a capire quale strada devo percorrere.
Mi spiego... ho una ListActivity con 5 campi. 3 vengono prelevati da DB tramite cursore e bind, altri due vengono fatti comparire onItemListClick(). Uno è rappresentato da una immagine, l'altro è un coutdown. Il problema era che dopo il click e la magica apparizione della immagine e del coutdown questi venivano ripetuti allo scroll su altre righe e sparivano quelli buoni.
Per l'immagine ho risolto creando un ArrayList e tenendomi ad "1" gli elementi buoni, cosi dentro la funzione di popolamento chiamata dal bind andavo a visualizzare le immagini buone e nascondere quelle non cliccate. Per i countdown ho provato a fare la stessa cosa ma continua a non funzionare :a3:
Di seguito il dettaglio del codice con i commenti... :help:

Per tenere traccia delle righe cliccate ho creato un
Code:
static ArrayList<String> statusSms = new ArrayList<String>();

ed in base a questo ho inserito
Code:
                if(statusSms.get(c.getPosition())== "0"){
                        sms.setVisibility(View.INVISIBLE);

all'interno della funzione
Code:
void populateFrom(Cursor c, BookingHelper helper)

richiamata dal bind. Funziona benissimo per l'immagine nella riga ma il coutdown (textview) compare e scompare su diverse righe come prima... sarà che non è applicabile a questo oggetto? Vi incollo il codice che utilizzo per far comparire la stringa:

Code:
        public void setCountDown(){
        final TextView dt = (TextView)currentView.findViewById(R.id.countDownList);
        final CountDownTimer myTimer = new CountDownTimer(Long.parseLong(prefs.getString("timeWait", null))*60000, 1000) {                           
            public void onTick(long millisUntilFinished) {
                dt.setText("Arrivo previsto tra " + millisUntilFinished/60000 + " minuti");
            }
            public void onFinish() {
                dt.setText("Arrivato?");
            }
        };
        myTimer.start();
        }


Ho trovato un esempio di coutdown fatto su una intera listactivity che non riesco a "mappare" nel mio caso. Vi riporto il codice che ho trovato, magari qualcuno che ci capisce sicuramente più di me può darmi una mano:

Code:
package com.rochdev.example.countdownlistview;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CountDownList extends ListActivity {

        private Button mButton;

        private HashMap<Integer, TextView> mCounterList = new HashMap<Integer, TextView>();
        private ArrayList<MyData> mDataList = new ArrayList<MyData>();

        private Handler mHandler;
        private boolean mCountersActive;

        public CountDownList() {
                mHandler = new Handler();
        }

        private final Runnable mRunnable = new Runnable() {
                public void run() {
                        MyData myData;
                        TextView textView;

                        // if counters are active
                        if (mCountersActive) {                         
                                if (mCounterList != null && mDataList != null) {
                                        for (int i=0; i < mDataList.size(); i++) {
                                                myData = mDataList.get(i);
                                                textView = mCounterList.get(i);
                                                if (textView != null) {
                                                        if (myData.getCount() >= 0) {
                                                                textView.setText(myData.getCountAsString());
                                                                myData.reduceCount();
                                                        }
                                                }
                                        }
                                }
                                // update every second
                                mHandler.postDelayed(this, 1000);
                        }
                }
        };

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                // start and stop button
                mButton = (Button) findViewById(R.id.myButton);
                mButton.setText("Stop");
                mButton.setOnClickListener(new OnClickListener() {                     
                        @Override
                        public void onClick(View v) {                 
                                stopStart();
                        }
                });

                // add some test data
                mDataList = new ArrayList<MyData>();
                MyData a = new MyData("Start 1000", 1000);
                MyData b = new MyData("Start 900", 900);
                MyData c = new MyData("Start 800", 800);
                MyData d = new MyData("Start 700", 700);
                MyData e = new MyData("Start 600", 600);
                MyData f = new MyData("Start 500", 500);
                MyData g = new MyData("Start 400", 400);
                MyData h = new MyData("Start 300", 300);
                MyData i = new MyData("Start 200", 200);
                MyData j = new MyData("Start 100", 100);
                MyData k = new MyData("Start 50", 50);
                MyData l = new MyData("Start 10000", 10000);
                mDataList.add(a);
                mDataList.add(b);
                mDataList.add(c);
                mDataList.add(d);
                mDataList.add(e);
                mDataList.add(f);
                mDataList.add(g);
                mDataList.add(h);
                mDataList.add(i);
                mDataList.add(j);
                mDataList.add(k);
                mDataList.add(l);

                initData();
        }

        private void initData() {
                // set the list adapter
                setListAdapter(new MyListAdapter(this, R.layout.row, mDataList));
                // start counters
                stopStart();
        }     

        private void stopStart() {
                if (mCountersActive) {
                        mCountersActive = false;
                        mButton.setText("Start");
                } else {
                        mCountersActive = true;
                        mHandler.post(mRunnable);
                        mButton.setText("Stop");
                }
        }

        private class MyListAdapter extends ArrayAdapter<MyData> {

                private ArrayList<MyData> items;

                public MyListAdapter(Context context, int textViewResourceId,
                                ArrayList<MyData> items) {
                        super(context, textViewResourceId, items);
                        this.items = items;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {                                       
                        LayoutInflater vi = (LayoutInflater)
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View v = vi.inflate(R.layout.row, null);

                        MyData myData = items.get(position);

                        if (myData != null) {
                                TextView text = (TextView) v.findViewById(R.id.myTextView);
                                if (text != null) {
                                        text.setText(myData.getText());
                                }

                                TextView counter = (TextView) v.findViewById(R.id.myTextViewTwo);
                                if (counter != null) {
                                        counter.setText(myData.getCountAsString());
                                        // add the TextView for the counter to the HashMap.
                                        mCounterList.put(position, counter);
                                }
                        }

                        return v;
                }             
        }

        private class MyData {
                private String text;
                private int count;

                public MyData(String text, int count) {
                        this.text = text;
                        this.count = count;
                }

                public String getText() {
                        return text;
                }

                public int getCount() {
                        return count;
                }

                public String getCountAsString() {
                        return Integer.toString(count);
                }

                public void reduceCount() {
                        if (count > 0) {
                                count--;
                        }
                }
        }
}


Top
 Profile  
 
 Post subject: Re: Problema del "recycling views"
PostPosted: Sat Aug 27, 2011 10:37 am 
Offline
User avatar

Joined: Tue Aug 23, 2011 12:30 am
Posts: 2
Punti Android: ?
Nessun consiglio? lanciando un countdown su una listactivity il countdown appare su diverse righe, e non posso gestirlo con un array di booleani per vedere dove l'ho attivato perchè ad ogni tick me lo rimette su + righe :(

Code:
final CountDownTimer myTimer = new CountDownTimer(Long.parseLong(prefs.getString("timeWait", null))*60000, 5000)..... myTimer.start();


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
phpBB SEO

All trademarks and logos used in this site are of properties of their respective owners.