如何动态地在列表视图中添加数据

时间:2014-10-02 08:08:00

标签: android json listview extends

我希望动态地将来自webservice的数据显示到listview并完美地工作。

现在我希望在它结束时扩展listview(如facebook主页)。我该怎么做?

注意:我正在使用json webervice从php传递我的数据,并使用列表视图在我的应用程序中显示已解析的数据。

这是我的代码:

package info.androidhive.customlistviewvolley.adater;

import info.androidhive.customlistviewvolley.R;
import info.androidhive.customlistviewvolley.app.AppController;
import info.androidhive.customlistviewvolley.model.Movie;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView; 

public class CustomListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Movie> movieItems;
    ProgressDialog pDialog;
    VideoView videoview;
    ProgressBar mSpinner;
    public static String s1="10";
    // Insert your Video URL
    String VideoURL;// = "http://demo.cogzideltemplates.com/client/sedio/video/d2b6a230106f355d2eee796863f4088a.mp4";

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<Movie> movieItems) {
        this.activity = activity;
        this.movieItems = movieItems;
    }

    @Override
    public int getCount() {
        return movieItems.size();
    }

    @Override
    public Object getItem(int location) {
        return movieItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
        inflater = (LayoutInflater) activity
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

        convertView.setClickable(true);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView rating = (TextView) convertView.findViewById(R.id.rating);
        TextView genre = (TextView) convertView.findViewById(R.id.genre);
        TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
        //TextView desc = (TextView) convertView.findViewById(R.id.desc);
        videoview=(VideoView) convertView.findViewById(R.id.VideoView);
        mSpinner=(ProgressBar) convertView.findViewById(R.id.progressBar1);

        Movie m = movieItems.get(position);
        VideoURL=m.getvideoUrl();
        videoview.setVideoURI(Uri.parse(VideoURL));
        // videoview.setMediaController(new MediaController(this));
        videoview.requestFocus();
        // mSpinner.setVisibility(View.GONE);
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                //pDialog.dismiss();
                mSpinner.setVisibility(View.GONE); 
                videoview.start();
            }
        });


        //videoview.setLayoutParams(new FrameLayout.LayoutParams(550,550));

        //    videoview.start();

        // Execute StreamVideo AsyncTask


        //mSpinner.setVisibility(View.VISIBLE);

        //play video

        // Start the MediaController

        //mediacontroller.setAnchorView(videoview);
        // Get the URL from String VideoURL
        //  Uri video = Uri.parse(VideoURL);
        //videoview.setMediaController(mediacontroller);
        //  videoview.setVideoURI(video);

        //videoview.requestFocus(); 

        // Close the progress bar and play the video

        //videoview.start();

        // getting movie data for the row

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

        // title
        title.setText(m.getTitle());

        // rating
        rating.setText(m.getDate());

        //desc.setText(m.getdesc());
        // release year
        year.setText(String.valueOf(m.getYear())); 

        return convertView;
    }
}

3 个答案:

答案 0 :(得分:0)

Listviews有一个用于检查滚动位置的监听器。以下代码可帮助您检查自己是否位于ListView

的末尾

首先制作Activity Fragment实施OnScrollListener。然后添加

private int preLast;
// Initialization stuff.
yourListView.setOnScrollListener(this);



@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
             final int visibleItemCount, final int totalItemCount) {

switch(lw.getId()) {
    case R.id.yourIDListview:     

       final int lastItem = firstVisibleItem + visibleItemCount;
       if(lastItem == totalItemCount) {
          if(preLast!=lastItem){ //to avoid multiple calls for last item
            //LOAD MORE ITEMS
            preLast = lastItem;
          }
       }
   }
}

答案 1 :(得分:0)

这是我的方法:

我抓住onScroll的{​​{1}}并加载更多项目(如果需要)。
在我的情况下,我加载更多,如果我滚动到看到的产品的一半

ListView

此处任务是调用Web服务的@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if ((task == null || task.getStatus() == AsyncTask.Status.FINISHED) && productHolder.hasProductRemainsToBeLoad() && productHolder.getProducts().size() - (Config.PRODUCT_NUMBER_PER_PAGE/2) < firstVisibleItem + visibleItemCount){ loadProduct(); } }
然后通知您的适配器

Asynctask

答案 2 :(得分:0)

您可以使用OnScrollListener实现它。

 listview.setOnScrollListener(new OnScrollListener(){

             @Override
             public void onScroll(AbsListView view,int firstVisibleItem, int visibleItemCount,int totalItemCount) {
                //Algorithm to check if the last item is visible or not
                final int lastItem = firstVisibleItem + visibleItemCount;
                if(lastItem == totalItemCount){                 
                    // you have reached end of list, load more data  
                    //Toast.makeText(getApplicationContext(), "You have reached last item!", Toast.LENGTH_SHORT).show();
                    //Sent_details.clear();
                    //In here give a call to database and take another 5 posts and notify the listview
                    //adapter.notifyDataSetChanged();

                }
             }

             @Override
             public void onScrollStateChanged(AbsListView view,int scrollState) {
                //blank, not using this
             }
            });