我需要从SQLite检索单击的RecyclerView项的数据,并将其显示在新的Fragment中。该数据包括图像URL,该URL也需要在该片段中显示。找不到有关该操作的信息。 我正在尝试使用单击项的ID检索数据(该ID传递给Fragment)。
我尝试将ImageView添加到Fragment的布局中,但这似乎并不是这样做的方法。
适配器
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.RowViewHolder> {
private Context context;
private List<Movie> movieList;
private FragmentTransaction ft;
public MoviesAdapter(Context context, FragmentManager fm) {
this.context = context;
movieList = new ArrayList<>();
}
@NotNull
@Override
public RowViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context)
.inflate(R.layout.row_list_movies, parent,false);
return new RowViewHolder(itemView);
}
class RowViewHolder extends RecyclerView.ViewHolder {
TextView viewTitle;
TextView viewReleaseYear;
LinearLayout movieEntry;
RowViewHolder(View view) {
super(view);
viewTitle = view.findViewById(R.id.title);
viewReleaseYear = view.findViewById(R.id.releaseYear);
movieEntry = view.findViewById(R.id.eventsListRowId);
}
}
@Override
public void onBindViewHolder(@NotNull RowViewHolder holder, int position) {
final Movie currentMovie = movieList.get(position);
holder.viewTitle.setText(movieList.get(position).getTitle());
holder.viewReleaseYear.setText(String.valueOf(movieList.get(position).getReleaseYear()));
holder.movieEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toMovieDetailsFragment(currentMovie, v);
}
});
}
private void toMovieDetailsFragment(Movie currentMovie, View v) {
AppCompatActivity activity = (AppCompatActivity) v.getContext();
int id = currentMovie.getMovieId();
Fragment mdf = new MovieDetailsFragment();
Bundle movieBundle = new Bundle();
movieBundle.putInt("id", id);
mdf.setArguments(movieBundle);
Log.d("Monitoring", "Going to MovieDetailsFragment");
activity.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragments_container, mdf)
.addToBackStack(null)
.commit();
}
@Override
public int getItemCount() {
return movieList.size();
}
public void attachMoviesList(List<Movie> movieList) {
// getting list from Fragment.
this.movieList = movieList;
// notifyDataSetChanged();
}
}
需要在其中显示信息的片段
public class MovieDetailsFragment extends Fragment{
Movie movie;
int movieId;
GetDatabase getDatabase;
@Nullable
@Override
public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details_movie, container, false);
Bundle idBundle = getArguments();
if (idBundle != null) {
movieId = getArguments().getInt("id");
}
movie = new GetDatabase(getActivity()).getMovieDetails(movieId);
// movie = Movie.getCurrentMovie();
return rootView;
}
public void setMovie(Movie movie){
this.movie = movie;
}
}
从SQLite检索点击项数据的方法
public Movie getMovieDetails(int movieId) {
Movie movie = new Movie();
String sqlQuery = "SELECT * FROM " + TABLE_NAME +" WHERE id="+"'"+movieId+"'";
open();
Cursor cursor = db.rawQuery(sqlQuery, null);
try
{
if(cursor.getCount() > 0)
{
while (!cursor.isAfterLast()) {
movie.setMovieId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Movie.ID))));
movie.setTitle(cursor.getString(cursor.getColumnIndex(Movie.TITLE)));
movie.setImage(cursor.getString(cursor.getColumnIndex(Movie.IMAGE_URL)));
movie.setRating(cursor.getDouble(cursor.getColumnIndex(Movie.RATING)));
movie.setReleaseYear(cursor.getInt(cursor.getColumnIndex(Movie.RELEASE_YEAR)));
movie.setGenre(cursor.getString(cursor.getColumnIndex(Movie.GENRE)));
cursor.moveToNext();
}
}
else
{
cursor.close();
}
}
catch(Exception ex)
{
Log.e("Error", "Look at Class's Method");
Log.e("Error", ex.toString());
}
return movie;
}
显示列表的片段
public class MoviesListFragment extends Fragment implements ScanResultReceiver {
private TextView formatTxt, contentTxt;
Context context;
MoviesAdapter moviesAdapter;
RecyclerView moviesRV;
FragmentManager fm;
private FragmentTransaction ft;
Button addBtn;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_movies, container, false);
Log.i(TAG,"In moviesListFragment");
context = getActivity();
addBtn = rootView.findViewById(R.id.addBtnId);
formatTxt = rootView.findViewById(R.id.scan_format);
contentTxt = rootView.findViewById(R.id.scan_content);
Bundle listBundle = getArguments();
if (listBundle != null) {
ArrayList<Movie> moviesList = getArguments().getParcelableArrayList("moviesList");
if (moviesList != null) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
moviesAdapter = new MoviesAdapter(context, fm);
moviesRV = rootView.findViewById(R.id.moviesRVId);
moviesRV.setLayoutManager(linearLayoutManager);
moviesRV.setHasFixedSize(true);
moviesAdapter.attachMoviesList(moviesList);
DividerItemDecoration dividerItemDecoration =
new DividerItemDecoration(context, linearLayoutManager.getOrientation());
moviesRV.addItemDecoration(dividerItemDecoration);
moviesRV.setAdapter(moviesAdapter);
}
}
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return rootView;
}
@Override
public void scanResultData(String codeFormat, String codeContent){
// display it on screen
formatTxt.setText(String.format("FORMAT: %s", codeFormat));
contentTxt.setText(String.format("CONTENT: %s", codeContent));
Log.i("Monitoring: ", formatTxt+" "+contentTxt);
}
@Override
public void scanResultData(NoScanResultException noScanData) {
Toast toast = Toast.makeText(context,noScanData.getMessage(), Toast.LENGTH_SHORT);
toast.show();
}
}
列表片段的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clipChildren="false"
android:clipToPadding="false"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/scan_format"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:gravity="center_horizontal"
android:layout_marginBottom="@dimen/alertBtnMargin" />
<TextView
android:id="@+id/scan_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_marginBottom="@dimen/alertBtnMargin"
android:gravity="center_horizontal" />
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" />
<LinearLayout
android:layout_weight="500"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/moviesRVId"
android:layout_margin="@dimen/dimen_10" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/addBtnId"
android:textSize="@dimen/btnTxtSize"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/addAMovie" />
</LinearLayout>
</LinearLayout>
<!--<include layout="@layout/change_name" />-->
</android.support.constraint.ConstraintLayout>
谢谢。