我有一个RecyclerView,它显示了一个自定义布局,其中填充了两件事:
我查询SQLite数据库,以从单击的歌手那里获得特定的歌曲。除了一件事情,我有这个工作。
我想使用图像的文本名称(2)作为查询中的变量,因为字符串设置为我要在搜索查询中使用的值。
我不知道如何访问此变量。
Artists.java
package com.apps.robloxmusic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
public class Artists extends AppCompatActivity {
private static final String TAG = "Artists";
//Variables
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImagesUrls = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artists);
Log.d(TAG, "onCreate: started.");
initImageBitmaps();
}
private void initImageBitmaps() {
Log.d(TAG, "initImageBitmaps: preparing bitmaps.");
mImagesUrls.add("https://cdn.blocktoro.com/wp-content/uploads/2019/07/bts-1097661412_1-e1562687430275-800x400.jpg");
mNames.add("BTS");
mImagesUrls.add("https://hdqwalls.com/download/post-malone-new-tattoo-2018-4k-lg-1920x1080.jpg");
mNames.add("Post Malone");
initRecyclerView();
}
private void initRecyclerView() {
Log.d(TAG, "initRecyclerView: preparing recycler view.");
RecyclerView recyclerView = findViewById(R.id.artistView);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(mNames, mImagesUrls, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
RecyclerViewAdapter.java
package com.apps.robloxmusic;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
public ArrayList<String> mImagesNames = new ArrayList<>();
private ArrayList<String> mImages = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(ArrayList<String> mImagesNames, ArrayList<String> mImages, Context mContext) {
this.mImagesNames = mImagesNames;
this.mImages = mImages;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_artistlist, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewerHolder: called.");
Glide.with(mContext)
.asBitmap()
.load(mImages.get(position))
.into(holder.artistImage);
holder.artistName.setText(mImagesNames.get(position));
holder.artistLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on:" + mImagesNames.get(position));
Intent intent = new Intent(mContext, BandView.class);
intent.putExtra("image_url", mImages.get(position));
//might not need this?
intent.putExtra("image_name", mImagesNames.get(position));
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mImagesNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView artistImage;
TextView artistName;
RelativeLayout artistLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
artistImage = itemView.findViewById(R.id.artistImage);
artistName = itemView.findViewById(R.id.artistName);
artistLayout = itemView.findViewById(R.id.artistLayout);
}
}
}
DatabaseAccess.java
私有字符串allArtistSongs是我想要设置文本变量的内容。 “防弹少年团”;只是一个占位符。
package com.apps.robloxmusic;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;
private String allArtistSongs = "BTS";
/**
* Private constructor to avoid object creation from outside classes.
*
* @param context
*/
private DatabaseAccess(Context context) {
this.openHelper = new DatabaseOpenHelper(context);
}
/**
* Return a singleton instance of DatabaseAccess.
*
* @param context the Context
* @return the instance of DatabaseAccess
*/
public static DatabaseAccess getInstance(Context context) {
if (instance == null) {
instance = new DatabaseAccess(context);
}
return instance;
}
/**
* Open the database connection.
*/
public void open() {
this.database = openHelper.getWritableDatabase();
}
/**
* Close the database connection.
*/
public void close() {
if (database != null) {
this.database.close();
}
}
/**
* Read all songs from the database.
*
* @return a List of songs
*/
public List<String> getSongs() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM [Music_Codes]", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
public List<String> getArtistSongs() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM [Music_Codes] WHERE Song LIKE ?", new String[] {"%"+ allArtistSongs+ "%"});
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
}
答案 0 :(得分:0)
您需要在哪里调用getArtistSongs()?我怀疑您需要在viewHolder上的clickListener启动的新活动中调用此方法。 如果是这样,则可以将此字符串提供给意图(image_name ??)。然后,当新活动启动时,您可以从意图中获取字符串,并在getArtistSongs()中使用它。
getArtistSongs()需要具有用于查询的参数String,类似
public List<String> getArtistSongs(String artist) {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM [Music_Codes] WHERE Song LIKE ?", new String[] {"%"+ artist + "%"});
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}