PullToRefresh与自定义单元格listView Android

时间:2012-06-03 23:58:51

标签: java android listview

我使用了这个库:https://github.com/johannilsson/android-pulltorefresh

运行良好,但在ListView简单(简单就是我在单元格android的模式中所说的)。

我在自定义ListView中放置了一个Adpter,用于包含图像,标题和描述的单元格。

我的班级与adpter:

public class extends Main_podcast ListActivity implements {OnItemClickListener

private Bitmap pathImgSmal;
    / ** Called When the activity is first created. * /
    @ Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        Mybundle bundle = new Bundle ();
    mybundle.putString ("podcastnumber", "0");

        / / Check for connection and starts downloading the XML
    Connected conected Boolean = (this);
        if (conected == true) {
    DoInBackground new (). execute ();
    Else {}
    new AlertDialog.Builder (Main_podcast.this)
    . SetTitle ("Alert!")
. SetMessage ("Unable to connect to the Internet," +
"Check your network or try again later.")
. SetNeutralButton ("OK"
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog,
int which) {}
}). Show ();
    }

    }


    / / Method calls the XML Parser
    public void do_update ()
    {
    PodcastParser.parse ();
    }

    / / Method that populates the list view with XML parameters
    public void populate_listview ()
    {
        List <Itens> podcastList <Itens> = new ArrayList ();

        for (int i = 0; i <arraysPodcast.PodcastTitle.length; i + +) {
            end item Items = new Items ();
        item.setTitle (arraysPodcast.PodcastTitle [i]);
        item.setData (arraysPodcast.PodcastData [i]);


        item.setImage (arraysPodcast.ArrayIMGPodcast [i]);
        podcastList.add (item);

        }

        setListAdapter (new PodcastAdapter (this, podcastList));
        ListView shot getListView = ();

        shot.setOnItemClickListener ((OnItemClickListener) this);

    }

    public void onItemClick (AdapterView <?> arg0, View v, int position, long id) {
    / / Start the Activity podcast regarding your number from the list
    Myintent intent = new Intent ("com.example.player.PODCAST");
    Mybundle bundle = new Bundle ();
    mybundle.putInt ("podcastnumber" position);
    myintent.putExtras (mybundle);

    startActivity (myintent);

}


    private class extends DoInBackground AsyncTask <Void, Void, Void>
                                 implements DialogInterface.OnCancelListener
    {
        ProgressDialog private dialog;

        protected void OnPreExecute ()
        {
        dialog = ProgressDialog.show (Main_podcast.this, "", "Loading Podcasts ...", true);
        }

        protected void doInBackground (Void. unused ..)
        {
        do_update ();
        return null;
        }

        OnPostExecute protected void (Void unused)
        {
        dialog.dismiss ();
        populate_listview ();
        }

        public void onCancel (DialogInterface dialog)
        {
        cancel (true);
        dialog.dismiss ();
        }
    }

    public static boolean Connected (Context context) {
        try {
            = ConnectivityManager cm (ConnectivityManager)
            context.getSystemService (Context.CONNECTIVITY_SERVICE);
            LogSync String = null;
            LogToUserTitle String = null;
            if (cm.getNetworkInfo (ConnectivityManager.TYPE_MOBILE). isConnected ()) {
                    LogSync + = "\ nConectado 3G Internet";
                    LogToUserTitle + = "Connected to 3G Internet";

                    return true;
            } Else if (cm.getNetworkInfo (ConnectivityManager.TYPE_WIFI). IsConnected ()) {
                    LogSync + = "\ nConectado WIFI Internet";
                    LogToUserTitle + = "Connected to Wireless Internet";
                    return true;
            Else {}
                    LogSync + = "\ nYou have internet connection";
                    LogToUserTitle + = "Do not have an internet connection";

                    return false;
            }
        } Catch (Exception e) {
                return false;
        }
    }


} / / Class

问题是,如何使用自定义单元库johannilsson实现我的类?

欢迎任何帮助!

1 个答案:

答案 0 :(得分:0)

决定如下:

public class PullToRefreshActivity extends ListActivity implements OnItemClickListener {
    private LinkedList<String> mListItems;
    private List<Itens> podcastList;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_to_refresh);
        // Set a listener to be invoked when the list should be refreshed.
        // ======================================================
        Bundle mybundle = new Bundle();
        mybundle.putString("podcastnumber", "0");

        //verifica se há conexao e inicia download do XML
        Boolean conected = Conectado(this);
        if(conected == true){
            new DoInBackground().execute();
        }else{
            new AlertDialog.Builder(PullToRefreshActivity.this)
            .setTitle("Alerta!")
            .setMessage("Não foi possível estabelecer uma conexão com a Internet," +
                    "verifique sua rede ou tente novamente mais tarde.")
            .setNeutralButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
             int which) {}
      }).show();
        }

     // ======================================================


        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new DoInBackground().execute();
            }
        });


    }

    // ==================================================================
    public void do_update() 
    {
        PodcastParser.parse();
    }

    //metodo que preenche a list view com parametros do XML
    public void populate_listview()
    {  

        podcastList = new ArrayList<Itens>();

        for (int i = 0; i < arraysPodcast.PodcastTitle.length; i++) {
            final Itens item = new Itens();
            item.setTitle(arraysPodcast.PodcastTitle[i]);
            item.setData(arraysPodcast.PodcastData[i]);


            item.setImage(arraysPodcast.ArrayIMGPodcast[i]);
            podcastList.add(item);

        }  

        setListAdapter(new PodcastAdapter(this, podcastList));

        ((PullToRefreshListView) getListView()).setOnItemClickListener((OnItemClickListener) this);
    }

    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
        //Inicia Activity da podcast referente a seu numero da lista
        Intent myintent = new Intent("com.example.player.PODCAST");
        Bundle mybundle = new Bundle();
        mybundle.putInt("podcastnumber", (position-1));
        Log.e("posicao na lista",""+position);
        myintent.putExtras(mybundle);

        startActivity(myintent);

    }


    private class DoInBackground extends AsyncTask<Void, Void, Void>
                                 implements DialogInterface.OnCancelListener
    {
        private ProgressDialog dialog;

        protected void onPreExecute() 
        {

            dialog = ProgressDialog.show(PullToRefreshActivity.this, "", "Carregando Podcasts...", true);
        }

        protected Void doInBackground(Void... unused) 
        { 
            PodcastAdapter.topBar=true;
            do_update(); 
            return null; 
        }

        protected void onPostExecute(Void unused) 
        { 
            ((PullToRefreshListView) getListView()).onRefreshComplete();
            dialog.dismiss(); 
            populate_listview();
        }

        public void onCancel(DialogInterface dialog) 
        { 
            cancel(true); 
            dialog.dismiss(); 
        }
    }

    public static boolean Conectado(Context context) {
        try {
            ConnectivityManager cm = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
            String LogSync = null;
            String LogToUserTitle = null;
            if (cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()) {
                    LogSync += "\nConectado a Internet 3G ";
                    LogToUserTitle += "Conectado a Internet 3G ";

                    return true;
            } else if(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()){
                    LogSync += "\nConectado a Internet WIFI ";
                    LogToUserTitle += "Conectado a Internet WIFI ";
                    return true;
            } else {
                    LogSync += "\nNão possui conexão com a internet ";
                    LogToUserTitle += "Não possui conexão com a internet ";

                    return false;
            }
        } catch (Exception e) {
                return false;
        }
    }

}