【问题标题】:PullToRefresh with custom cells listView AndroidPullToRefresh 与自定义单元格 listView Android
【发布时间】:2012-06-03 23:58:51
【问题描述】:

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

效果很好,但是在 ListView 中很简单(简单就是我所说的 cell android 的模式)。

我佩戴了一个 Adpter,将我的自定义 ListView 放入带有图像、标题和描述的单元格。

我的带有适配器的班级:

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 实现我的类?

欢迎任何帮助!

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    决定如下:

    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;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 2015-02-09
      • 2020-10-16
      相关资源
      最近更新 更多