这个应用程序用于使用Google Books API将书籍作为JSON获取,并且在研究中我还发现JSON数据正在加载正常,书籍标题,作者被正确解析并且我使用祝酒词进行了测试。但是,即使使用适配器设置listView,listView也不会显示。什么一定是错误的?该应用程序也没有崩溃使得排除故障变得困难。提前谢谢!
主要活动
package com.praveent.booklisting;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
public String rootUrl = "https://www.googleapis.com/books/v1/volumes";
public String queryParameter = "q";
Button search_button;
EditText search_string;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
search_button = (Button) findViewById(R.id.search_button);
search_string = (EditText) findViewById(R.id.search_string);
search_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String query = search_string.getText().toString();
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if(activeNetworkInfo != null && activeNetworkInfo.isConnected()){
new BackgroundTask().execute(query);
}
else{
Toast.makeText(MainActivity.this, "Please switch on the internet!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void populate(BookAdapter adapter){
listView.setAdapter(adapter);
}
public class BackgroundTask extends AsyncTask<String, JSONArray, String> {
@Override
protected String doInBackground(String... strings) {
String query = strings[0];
URL url = null;
Uri uri = Uri.parse(rootUrl).buildUpon().appendQueryParameter(queryParameter, query).build();
try {
url = new URL(uri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
String data = null;
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
if(scanner.hasNext()){
data = scanner.next();
}
} catch (IOException e1) {
e1.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
BookAdapter bookAdapter;
JSONObject jsonObject = null;
JSONArray jsonArray = null;
JSONObject jsonArrayObject = null;
JSONObject volumeInfo = null;
JSONArray authorInfo = null;
JSONObject imageInfo = null;
String imageUrl = null;
String bookTitle = null;
String bookAuthor = null;
try {
jsonObject = new JSONObject(s);
jsonArray = jsonObject.getJSONArray("items");
ArrayList<Book> books= new ArrayList<Book>();
for(int i = 0; i <= jsonArray.length(); i++){
jsonArrayObject = jsonArray.getJSONObject(i);
volumeInfo = jsonArrayObject.getJSONObject("volumeInfo");
bookTitle = volumeInfo.getString("title");
authorInfo = volumeInfo.getJSONArray("authors");
bookAuthor = " ";
if (authorInfo != null){
bookAuthor = authorInfo.getString(0);
}
//imageInfo = volumeInfo.getJSONObject("imageLinks");
//imageUrl = imageInfo.getString("smallThumbnail");
books.add(new Book(bookTitle, bookAuthor));
Toast.makeText(MainActivity.this, bookAuthor, Toast.LENGTH_SHORT).show();
}
bookAdapter = new BookAdapter(MainActivity.this, books);
populate(bookAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
书籍适配器
package com.praveent.booklisting;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class BookAdapter extends ArrayAdapter<Book>{
Context c = null;
public BookAdapter(Context context, ArrayList<Book> books) {
super(context, 0, books);
c = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Book currentBook = getItem(position);
TextView bookTextView = (TextView) listItemView.findViewById(R.id.book_name);
bookTextView.setText(currentBook.getBookName());
TextView authorTextView = (TextView) listItemView.findViewById(R.id.book_author);
authorTextView.setText(currentBook.getAuthorName());
//ImageView coverImageView = (ImageView) listItemView.findViewById(R.id.cover_image);
//Picasso.with(c).load(currentBook.getImageURL()).into(coverImageView);
return listItemView;
}
}
图书类
package com.praveent.booklisting;
public class Book {
private String bookName;
private String authorName;
private String imageURL;
public Book(String name, String author){
bookName = name;
authorName = author;
//imageURL = url;
}
public String getBookName() {
return bookName;
}
public String getAuthorName() {
return authorName;
}
public String getImageURL() {
return imageURL;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.praveent.booklisting.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:id="@+id/search_string"
android:textAlignment="center"
android:gravity="center"
android:hint="@string/search_hint"/>
<Button
android:layout_width="match_parent"
android:id="@+id/search_button"
android:layout_height="wrap_content"
android:text="@string/search_button" />
<ListView
android:layout_width="fill_parent"
android:id="@+id/list_view"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/cover_image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/book_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000"
android:id="@+id/book_author"/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
关于PostExecute Change
for(int i = 0; i <=jsonArray.length(); i++)
到for(int i = 0;i<jsonArray.length(); i++)
在list_item.xml上,将文本颜色更改为白色
答案 1 :(得分:-1)
你需要在适配器中调用getCount()。
public class BookAdapter extends ArrayAdapter<Book>{
Context c = null;
ArrayList<Book> bookslist = new ArrayList();
public BookAdapter(Context context, ArrayList<Book> books) {
super(context, 0, books);
c = context;
bookslist = books;
}
@Override
public int getCount() {
return bookslist.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Book currentBook = getItem(position);
TextView bookTextView = (TextView) listItemView.findViewById(R.id.book_name);
bookTextView.setText(currentBook.getBookName());
TextView authorTextView = (TextView) listItemView.findViewById(R.id.book_author);
authorTextView.setText(currentBook.getAuthorName());
//ImageView coverImageView = (ImageView) listItemView.findViewById(R.id.cover_image);
//Picasso.with(c).load(currentBook.getImageURL()).into(coverImageView);
return listItemView;
}
}
答案 2 :(得分:-1)
将适配器更改为此
public class BookAdapter extends ArrayAdapter<Book> {
Context c = null;
private List<Book> bookList;
public BookAdapter(Context context, ArrayList<Book> books) {
super(context, 0, books);
c = context;
bookList = new ArrayList<>();
bookList = books;
}
@Override
public int getCount() {
return bookList.size();
}
@Nullable
@Override
public Book getItem(int position) {
return bookList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Book currentBook = getItem(position);
TextView bookTextView = (TextView) listItemView.findViewById(R.id.book_name);
bookTextView.setText(currentBook.getBookName());
TextView authorTextView = (TextView) listItemView.findViewById(R.id.book_author);
authorTextView.setText(currentBook.getAuthorName());
//ImageView coverImageView = (ImageView) listItemView.findViewById(R.id.cover_image);
//Picasso.with(c).load(currentBook.getImageURL()).into(coverImageView);
return listItemView;
}
}
我检查了你的代码错误是在AsyncTask onPostExecute方法中,将你的for循环条件改为
for(int i = 0; i&lt; jsonArray.length(); i ++)
现在正在运作。