并非列表视图中显示的所有项目(Android Studio)

时间:2015-09-26 07:42:14

标签: android listview arraylist adapter

并非所有元素都显示在我的列表视图中。有时它显示前5-6个元素,但有时只显示2-3个元素。我想在列表视图中仅显示该书的标题。所以我重写了toString方法。 当我有弦乐的时候,一切都很好,但现在我需要书籍的arraylist。 这是我的代码。

public class GetAWebResourceTask extends AsyncTask<String, Void, String> {
final public static String TAG = "GetAWebResourceTask";
private RestTemplate restTemplate;
private TextView textView;
private Button b;
private ListView list;
private ArrayAdapter<Book> adapter;
private int page;
private ArrayList<Book> items;
public GetAWebResourceTask(RestTemplate restTemplate, ListView list, int page) {
    this.restTemplate = restTemplate;
    this.list=list;
    this.page=page;

}

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

}

@Override
protected String doInBackground(String... params) {


    String url_params = URLEncoder.encode(params[0]);

    String search = "http://www.it-ebooks-api.info/v1/search/" + url_params + "/page/"+page;

  //  Log.d(TAG,search);
    String result = restTemplate.getForObject(search, String.class);

    //Log.d(TAG, result);

    if (result == null) {
        result = " { \"Title\" : \"Task Failed\"  } ";
    }
    String s = "";
     items = new ArrayList<Book>();
    try {
        JSONObject jo = new JSONObject(result);
        JSONArray cast = jo.getJSONArray("Books");
        for(int i=0; i<cast.length(); i++){

            JSONObject temp = cast.getJSONObject(i);
            Book bo=new Book();
            bo.setDescription(temp.getString("Description"));
            bo.setId(temp.getString("ID"));
            bo.setImage(temp.getString("Image"));
            bo.setSubTitle(temp.getString("SubTitle"));
            bo.setTitle(temp.getString("Title"));
            items.add(bo);
            Log.d(TAG, i + " " + bo.getTitle() + " " +cast.length() );
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }


    // Book or = new Gson().fromJson(result, Book.class);
  //  Log.d(TAG, or.getTitle() + " " + or.getID());
    return s;

}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    adapter = new ArrayAdapter<Book>(list.getContext(),
            android.R.layout.simple_list_item_1, android.R.id.text1, items);
    list.setAdapter(adapter);
}




    public class Book {
private String Title;
private String id;
private String Description;
private String SubTitle;
private String Image;

public String getTitle() {
    return Title;
}

public void setTitle(String title) {
    Title = title;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getDescription() {
    return Description;
}

public void setDescription(String description) {
    Description = description;
}

public String getSubTitle() {
    return SubTitle;
}

public void setSubTitle(String subTitle) {
    SubTitle = subTitle;
}

public String getImage() {
    return Image;
}

public void setImage(String image) {
    Image = image;
}

@Override
public String toString() {
    return getTitle();
}

}         

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:choiceMode="singleChoice"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

1 个答案:

答案 0 :(得分:0)

您没有通知适配器数据集已更改。为此,您需要访问doInBackground()的{​​{1}}内的适配器并致电

AsynTask
阻挡后的