使用ArrayAdapter使用AsyncTask限制ListView

时间:2016-04-20 07:02:35

标签: android listview android-asynctask android-arrayadapter

我正在像YOUTUBE一样开发应用程序。我从服务器获取数据并在listview中显示它。因为数据太大所以我想将列表视图限制为5个项目,然后当我向下滚动到listview的底部时,它应该再添加5个项目。

我从片段

调用ASyncTask
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());

    view = layoutInflater.inflate(R.layout.tab_fragment_2, container, false);

    listView = (ListView) view.findViewById(R.id.myList);
    New.jsonAsyncTask.execute("my url select_video.php?");
}

现在我有单独的JsonAsyncTask类,因为我从上面提到的方式从3个片段调用它。

public   class JSONAsyncTask extends AsyncTask<String, String, List<SetDatails>> {

ListView listView1;
Context context1;
List<SetDatails> videoLiast1;
private static int tab_id;


public JSONAsyncTask(ListView listView, List<SetDatails> videoLiast,Context context,int id) {
    this.listView1 = listView;
    this.context1 = context;
    this.videoLiast1 = videoLiast;
    tab_id=id;
}



@Override
protected List<SetDatails> doInBackground(String... params) {

    HttpURLConnection connection = null;
    BufferedReader myReader = null;


    try {
        URL url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        InputStream myInputStream = connection.getInputStream();
        myReader = new BufferedReader(new InputStreamReader(myInputStream));

        StringBuffer buffer = new StringBuffer();

        String line = "";

        while ((line = myReader.readLine()) != null) {
            buffer.append(line);

        }

        String Json_data = buffer.toString();

        videoLiast1 = new ArrayList<>();
        JSONObject parentObj = new JSONObject(Json_data);
        JSONArray dataArray = parentObj.getJSONArray("data");
        JSONObject tabObject = dataArray.getJSONObject(tab_id);
        JSONArray videoArray = tabObject.getJSONArray("videos");
        for (int i = 0; i < videoArray.length(); i++) {
            int len=dataArray.length();
            SetDatails setDatails = new SetDatails();
            JSONObject arrayChild = videoArray.getJSONObject(i);

            String vid_ID = arrayChild.getString("id");
            setDatails.setVidID(vid_ID);

            String movie_title = arrayChild.getString("name");
            setDatails.setTitle(movie_title);

            String movie_catagory = arrayChild.getString("category_name");
            setDatails.setGenre(movie_catagory);


            videoLiast1.add(setDatails);
        }

        return videoLiast1;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (myReader != null) {
                myReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

}


protected void onPostExecute(List<SetDatails> videoList) {
    super.onPostExecute(videoList);
    if(videoList!=null) {
        try {
            MyListAdapter myListAdapter = new MyListAdapter(context1, videoList);
                listView1.setAdapter(myListAdapter);

        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

}

我已经创建了一个数组适配器来填充列表视图。

public class MyListAdapter extends ArrayAdapter<SetDatails>{

public static int count =5;
List<SetDatails> videoList;

MyListAdapter(Context context,List<SetDatails> object){
    super(context,R.layout.tab2_bookchilds,object);
    videoList = object;
}

/**
 * {@inheritDoc}
 */
@Override
public int getCount() {
    return super.getCount();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater layoutInflater=LayoutInflater.from(getContext());
    View view = layoutInflater.inflate(R.layout.tab2_bookchilds, parent, false);

    TextView title = (TextView)view.findViewById(R.id.txt_title);
    title.setText(videoList.get(position).getTitle());

    ImageView imgV_Thumbnail = (ImageView) view.findViewById(R.id.imgV_thumbnail);
    Picasso.with(getContext())
            .load(videoList.get(position).getThumbnail())
            .error(R.drawable.icon_white)
            .into(imgV_Thumbnail);


    return view;
}

}

我搜索了很多,很多人建议使用getCount()方法,虽然我尝试了很多方法但是不知道怎么做。 请各位帮帮我,因为我的应用程序已准备好上传,但我无法解决这个问题。

0 个答案:

没有答案