我的应用程序的轮换问题

时间:2017-08-02 11:30:20

标签: android android-activity rotation

我正在构建一个测试应用,用户可以使用Google Books API搜索图书。

这是它的样子。

portrait mode

当我旋转手机时,listview消失了。我必须再次输入一本书的名称并查看新数据。因此,在旋转部分期间,活动将被销毁以及我对服务器的Web请求。

所以,让我分享一下我的代码。首先,我有我的Book类,它实现了Parcelable

public class Book implements Parcelable{

private String title;
private String author;
private String imageUrl;

protected Book(Parcel in) {
    title = in.readString();
    author = in.readString();
    imageUrl = in.readString();

}

public static final Creator<Book> CREATOR = new Creator<Book>() {
    @Override
    public Book createFromParcel(Parcel in) {
        return new Book(in);
    }

    @Override
    public Book[] newArray(int size) {
        return new Book[size];
    }
};

public String getImageUrl() {
    return imageUrl;
}



/**
 *
 * @param title is the title of the book
 * @param author is the author of the book
 */
public Book(String title,String author,String imageUrl){
    this.title = title;
    this.author = author;
    this.imageUrl = imageUrl;

}

// Returns title
public String getTitle() {
    return title;
}
// Returns author
public String getAuthor() {
    return author;
}



@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(title);
    parcel.writeString(author);
    parcel.writeString(imageUrl);

}

}

我的MainActivity类。

public class MainActivity extends AppCompatActivity{
public static final String URL = "https://www.googleapis.com/books/v1/volumes?q=";
BookAdapter adapter;
EditText searchText;
Button searchButton;
String search;
ListView listview;
ArrayList<Book> bookList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


        searchText = (EditText) findViewById(R.id.search_text);
        searchButton = (Button) findViewById(R.id.search_button);


        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search = searchText.getText().toString();

                String url = URL + search + "&maxResults=15";


                BookAsyncTask bookAsyncTask = new BookAsyncTask();
                bookAsyncTask.execute(url);

                adapter = new BookAdapter(getApplicationContext(), bookList, R.color.list_color);

                listview = (ListView) findViewById(R.id.list);
                listview.setAdapter(adapter);
            }
        });

}

/*
public void searchBook(View view){


}
*/



private class BookAsyncTask extends AsyncTask<String,Void,List<Book>>{

    @Override
    protected List<Book> doInBackground(String... urls) {

        if (urls.length < 1 || urls[0] == null) {
            return null;
        }

        List<Book> result = QueryUtils.fetchBookData(urls[0]);
        return result;
    }

    @Override
    protected void onPostExecute(List<Book> books) {
        adapter.clear();

        if(books != null && !books.isEmpty()){
            adapter.addAll(books);
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("key",bookList);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    bookList = savedInstanceState.getParcelableArrayList("key");
}
}

正如您所看到的,我正在覆盖存储列表数据的onSaveInstantState(..)方法(我使用调试器检查过)和onRestoreInstanceState(...)

任何想法如何解决我的问题?

谢谢,

西奥。

3 个答案:

答案 0 :(得分:1)

  

如果您不想覆盖配置更改

,请将其包含在清单中
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize|screenLayout" >

答案 1 :(得分:1)

你可以在横向模式下创建一个新的activity.in以便屏幕可以适用于两个屏幕方向。或者如果你想让它保持为肖像,那么在你的清单中将这个代码添加到你要保留的活动中一幅肖像

 android:screenOrientation="portait"

答案 2 :(得分:1)

添加到您的清单

id