我正在创建一个应用程序,我调用Web服务器,下拉JSON数据,解析它并将其保存到SQLite数据库并向用户显示信息。
该应用程序的第一页只显示电影海报,或者它的意思是!每当应用程序刚刚安装时,GridView都不会更新,但是当它关闭并重新打开时,电影海报就会显示出来。我不确定我做错了什么,它在新安装时不会更新。
这是我的代码;
MainActivityFrament
public void update() {
Log.e(LOG_TAG, "In onUpdate");
FetchMovieTask movieTask = new FetchMovieTask(getContext());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userChoice = sharedPreferences.getString(getString(R.string.pref_sort_key), getString(R.string.pref_sort_value_mostpop));
movieTask.execute(userChoice);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onStart() {
super.onStart();
Log.e(LOG_TAG, "In onStart");
update();
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
Log.e(LOG_TAG, "In onActivityCreated");
getLoaderManager().initLoader(MOVIE_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(LOG_TAG, "In onCreateView");
Cursor cursor = getActivity().getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI, null, null, null, null);
customAdapter = new CustomAdapter(getActivity(), cursor, 0);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
gridView = (GridView) rootView.findViewById(R.id.gridView_movies);
gridView.setAdapter(customAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
Log.i(LOG_TAG, "set movie object values");
int _id = cursor.getInt(cursor.getColumnIndex(MovieContract.MovieEntry._ID));
if (cursor != null) {
Intent intent = new Intent(getActivity(), MovieDetailActivity.class);
intent.putExtra("movieId", _id);
Log.i(LOG_TAG, "About to start intent");
startActivity(intent);
}
}
});
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.e(LOG_TAG, "In onCreateLoader");
return new CursorLoader(
getActivity(),
MovieContract.MovieEntry.CONTENT_URI,
MOVIE_COLUMNS,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e(LOG_TAG, "In onLoadFinished");
customAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.e(LOG_TAG, "In onLoadReset");
customAdapter.swapCursor(null);
}
的CursorAdapter
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.e(LOG_TAG, "In newView()");
View view = LayoutInflater.from(context).inflate(R.layout.list_image_movie, parent, false);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.e(LOG_TAG, "in bindView()");
String title =cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.COLUMN_MOVIE_TITLE));
Log.e(LOG_TAG, "Movie Title - " + title);
ImageView iv = (ImageView) view.findViewById(R.id.list_movie_pics_imageview);
int columIndex = cursor.getColumnIndex(MovieContract.MovieEntry.COLUMN_POSTER);
Bitmap image = Utility.getImage(cursor.getBlob(columIndex));
iv.setImageBitmap(image);
}
的AsyncTask
@Override
protected Void doInBackground(String... params) {
Log.e(LOG_TAG, "In doInBackground");
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String movieJsonStr = null;
try {
final String MOVIE_BASE_URL = "https://api.themoviedb.org/3/discover/movie?";
final String SORT_PARAM = "sort_by";
final String APIKEY_PARAM = "api_key";
Uri builtUri = Uri.parse(MOVIE_BASE_URL).buildUpon()
.appendQueryParameter(SORT_PARAM, params[0])
.appendQueryParameter(APIKEY_PARAM, BuildConfig.OPEN_MOVIE_MAP_API_KEY)
.build();
URL url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI :" + builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
movieJsonStr = buffer.toString();
parseMovieData(new JSONObject(movieJsonStr));
checkDBValues(mContext);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Log.e(LOG_TAG, "In onPostExecute");
customAdapter.notifyDataSetChanged();
MainActivityFragment.progressBar.setVisibility(View.GONE);
}
public void parseMovieData(JSONObject movieData) {
Log.e(LOG_TAG, "In parseMovieData");
try {
JSONArray movieArray = movieData.getJSONArray("results");
Time dayTime = new Time();
dayTime.setToNow();
int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);
if (movieArray != null) {
for (int i = 0; i < movieArray.length(); i++) {
JSONObject movies = movieArray.getJSONObject(i);
long dateTime = dayTime.setJulianDay(julianStartDay + i);
Bitmap moviePoster = Utility.getBitmapFromURL(CustomAdapter.IMAGE_BASE_URL + movies.getString(CustomAdapter.IMAGE_PATH));
String moviePosterUrl = CustomAdapter.IMAGE_BASE_URL + movies.getString(CustomAdapter.IMAGE_PATH);
String movieTitle = movies.getString(CustomAdapter.MOVIE_TITLE);
String movieDescription = movies.getString(CustomAdapter.MOVIE_DESCRIPTION);
double movieRating = movies.getDouble(CustomAdapter.MOVIE_RATING);
String movieReleaseDate = movies.getString(CustomAdapter.MOVIE_RELEASE_DATE);
ContentValues movieValues = new ContentValues();
movieValues.put(MovieContract.MovieEntry.COLUMN_POSTER_URL, moviePosterUrl);
movieValues.put(MovieContract.MovieEntry.COLUMN_POSTER, Utility.getBytes(moviePoster));
movieValues.put(MovieContract.MovieEntry.COLUMN_RELEASE_DATE, movieReleaseDate);
movieValues.put(MovieContract.MovieEntry.COLUMN_DESCRIPTION, movieDescription);
movieValues.put(MovieContract.MovieEntry.COLUMN_MOVIE_RATING, movieRating);
movieValues.put(MovieContract.MovieEntry.COLUMN_MOVIE_TITLE, movieTitle);
movieValues.put(MovieContract.MovieEntry.COLUMN_DATE, dateTime);
if (MovieProvider.rowExists(movieTitle)) {
continue;
}
insertMoviesToDB(movieValues);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void insertMoviesToDB(ContentValues movieValues){
Log.e(LOG_TAG, "In insertMoviesToDB");
MovieProvider.mOpenHelper.getReadableDatabase().insert(MovieContract.MovieEntry.TABLE_NAME, null, movieValues);
}
public void checkDBValues(Context context) {
Log.e(LOG_TAG, "In checkDBValues");
Cursor cursor = context.getContentResolver().query(
MovieContract.MovieEntry.CONTENT_URI,
null,
null,
null,
null
);
int count = 0;
while (cursor.moveToNext()) {
count++;
}
Log.i(LOG_TAG, "Got " + count + " values from the DB");
}
MainLayout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityFragment">
<GridView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:id="@+id/gridView_movies"
android:layout_gravity="center"
android:stretchMode="columnWidth"
android:numColumns="2"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="center"
android:visibility="gone" />
我想我已经包含了所有相关信息。没有产生错误,因此没有显示相关日志。如果需要任何额外信息,请询问,我会提供。
非常感谢有关此事的任何信息
答案 0 :(得分:1)
在onPostExecute中,notifyDatasetChanged将无效,因为customAdapter游标中的数据仍然相同。
您需要获取具有更新信息的新光标,然后调用更改光标。所以在notifyDataSetChanged之前它应该是这样的
Cursor newCursor = getActivity().getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI, null, null, null, null);
customAdapter.changeCusrsor(newCursor);
customAdapter.notifyDataSetChanged();