【问题标题】:How to implement a listview using volley android如何使用 volley android 实现列表视图
【发布时间】:2014-08-17 11:25:22
【问题描述】:

我正在开发一个获取 API 客户端列表的应用程序,我需要在列表视图中显示它,我正在使用 volley,我尝试做的是以下操作,但它不起作用:

 public ListView txtDisplay;

@Override
        protected Boolean doInBackground(Void... params) {


            try {


                txtDisplay = (Listview) findViewById(R.id.listView);

                String url = "http://192.168.1.1/rep-mais-api/api";


                 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                        url, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {


                                findViewById(R.id.progressBar1).setVisibility(View.GONE);
                            }
                        }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("Error: " + error.getMessage());


                    }
                })

ActivityMain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pager" />
    <TextView
        android:id="@+id/txtDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <ListView
  android:layout_width="wrap_content"
   android:layout_height="wrap_content"
    android:id="@+id/listView"
  android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/>
    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

【问题讨论】:

    标签: android listview android-listview android-volley


    【解决方案1】:

    使用这个例子,解决了我的情况

    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    
    public class MainActivity extends Activity {
    
        private String TAG = this.getClass().getSimpleName();
        private ListView lstView;
        private RequestQueue mRequestQueue;
        private ArrayList<NewsModel> arrNews ;
        private LayoutInflater lf;
        private VolleyAdapter va;
        private ProgressDialog pd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lf = LayoutInflater.from(this);
    
    
            arrNews = new ArrayList<NewsModel>();
            va = new VolleyAdapter();
    
            lstView = (ListView) findViewById(R.id.listView);
            lstView.setAdapter(va);
            mRequestQueue =  Volley.newRequestQueue(this);
            String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
            pd = ProgressDialog.show(this,"Please Wait...","Please Wait...");
            try{
                Thread.sleep(2000);
            }catch(Exception e){
    
                }
            JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG,response.toString());
                    parseJSON(response);
                    va.notifyDataSetChanged();
                    pd.dismiss();
    ;            }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i(TAG,error.getMessage());
                }
            });
            mRequestQueue.add(jr);
    
    
    
        }
    
        private void parseJSON(JSONObject json){
            try{
                JSONObject value = json.getJSONObject("value");
                JSONArray items = value.getJSONArray("items");
                for(int i=0;i<items.length();i++){
    
                        JSONObject item = items.getJSONObject(i);
                        NewsModel nm = new NewsModel();
                        nm.setTitle(item.optString("title"));
                        nm.setDescription(item.optString("description"));
                        nm.setLink(item.optString("link"));
                        nm.setPubDate(item.optString("pubDate"));
                        arrNews.add(nm);
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
    
    
        }
    
    
        class NewsModel{
            private String title;
            private String link;
            private String description;
            private String pubDate;
    
            void setTitle(String title) {
                this.title = title;
            }
    
            void setLink(String link) {
                this.link = link;
            }
    
            void setDescription(String description) {
                this.description = description;
            }
    
            void setPubDate(String pubDate) {
                this.pubDate = pubDate;
            }
    
            String getLink() {
                return link;
            }
    
            String getDescription() {
                return description;
            }
    
            String getPubDate() {
                return pubDate;
            }
    
            String getTitle() {
    
                return title;
            }
        }
    
    
        class VolleyAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                return arrNews.size();
            }
    
            @Override
            public Object getItem(int i) {
                return arrNews.get(i);
            }
    
            @Override
            public long getItemId(int i) {
                return 0;
            }
    
            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ViewHolder vh ;
               if(view == null){
                   vh = new ViewHolder();
                   view = lf.inflate(R.layout.row_listview,null);
                   vh.tvTitle = (TextView) view.findViewById(R.id.txtTitle);
                   vh.tvDesc = (TextView) view.findViewById(R.id.txtDesc);
                   vh.tvDate = (TextView) view.findViewById(R.id.txtDate);
                   view.setTag(vh);
              }
                else{
                   vh = (ViewHolder) view.getTag();
               }
    
                NewsModel nm = arrNews.get(i);
                vh.tvTitle.setText(nm.getTitle());
                vh.tvDesc.setText(nm.getDescription());
                vh.tvDate.setText(nm.getPubDate());
                return view;
            }
    
             class  ViewHolder{
                TextView tvTitle;
                 TextView tvDesc;
                 TextView tvDate;
    
            }
    
        }
    }
    

    Source

    【讨论】:

    • 请不要发布“仅链接”的答案。这使得其他用户难以访问信息,并且链接可能由于某种原因变得无效(删除)并且信息丢失。
    • 我看到这个链接非常有用而且很长,但是,尝试发布对你帮助最大的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2012-04-13
    • 2017-03-19
    • 2019-01-17
    • 2015-02-10
    • 2016-01-31
    相关资源
    最近更新 更多