我使用带有两个文本视图和评级栏的回收站视图。我在sqlite数据库中有一个表,其中包含三个字段id,name和music。现在我想和两个文本视图中的名称和音乐一起动态地显示tv1和tv2。
我的布局是 `
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
card_view:cardBackgroundColor="@color/popup_tv_color"
card_view:cardCornerRadius="20dp"
card_view:cardElevation="0dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:id="@+id/linearLayout1"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Baatein Ye Kabhi Na"
android:gravity="center"
android:textColor="@android:color/holo_red_dark"
android:textSize="20dp"
android:id="@+id/tv1"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bollywood Song"
android:id="@+id/tv2"
android:paddingBottom="4dp"
android:layout_gravity="center_horizontal"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar1"
android:stepSize="0.05"
android:rating="0"
android:layout_gravity="left"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:textColor="@android:color/white"
android:layout_marginRight="10dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:background="@drawable/rounded_button"
android:backgroundTint="@color/pink"
android:id="@+id/button"
android:layout_alignBottom="@+id/ratingBar1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
我的main fragmeng is
<view
android:id="@+id/recycler_view"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
sqlite代码是
public List<String> getAllNames(){
SQLiteDatabase db=this.getWritableDatabase();
List<String> musicNames=new ArrayList<String>();
String SelectQuery="SELECT * FROM "+TABLE_NAME;
Cursor cursor=db.rawQuery(SelectQuery, null);
if(cursor.moveToFirst()){
do{
int id=cursor.getInt(0);
String name=cursor.getString(1);
String music=cursor.getString(2);
musicNames.add(name);
musicNames.add(music);
}while(cursor.moveToNext());
}
return musicNames;
}
现在MusicFragment
类是
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_music, container, false);
db = new DatabaseHandler(getContext());
List<String> musicNames = db.getAllNames();
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
MyAdapter myAdapter=new MyAdapter();
return v;
}
适配器类是
public class MyAdapter extends ecyclerView.Adapter<MyAdapter.PlanetViewHolder> {
ArrayList<String> songList;
public MyAdapter()
{
this.songList = songList;
}
@Override
public MyAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_list, parent, false);
PlanetViewHolder viewHolder = new PlanetViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(MyAdapter.PlanetViewHolder holder, int position) {
holder.text.setText(songList.get(position).toString());
// holder.text1.setText(songList.get(position).toString());
}
@Override
public int getItemCount() {
return songList.size();
}
public static class PlanetViewHolder extends RecyclerView.ViewHolder {
protected CardView card_view;
protected TextView text;
// protected TextView text1;
public PlanetViewHolder(View itemView) {
super(itemView);
card_view = (CardView) itemView.findViewById(R.id.card_view);
text = (TextView) itemView.findViewById(R.id.tv1);
// text1 = (TextView) itemView.findViewById(R.id.tv2);
}
}
我不知道如何添加添加请帮助
答案 0 :(得分:0)
在MyAdapter类中添加一个新方法并在此方法中传递歌曲列表
public void updateData(List<String> songList) {
this.songList.clear();
this.songList.addAll(songList);
notifyDataSetChanged();
}
答案 1 :(得分:0)
create class Music
public class Music {
private String name;
private String song;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
Update you getAllName() method to
public List<Music> getAllNames(){
SQLiteDatabase db = this.getWritableDatabase();
List<Music> musicNames = new ArrayList();
String selectQuery="SELECT * FROM "+ TABLE_NAME;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Music music = new Music;
music.setName(cursor.getString(1));
music.setSong(cursor.getString(2));
musicNames.add(music);
}while(cursor.moveToNext());
}
return musicNames;
}
Update your fragments onCreateView() to
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_music, container, false);
db = new DatabaseHandler(getActivity());
List<Music> musicNames = db.getAllNames();
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
MyAdapter myAdapter=new MyAdapter(getActivity(),musicNames);
mRecyclerView.setAdapter(myAdapter);
return v;
}
Update your Adapter to
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.PlanetViewHolder> {
List<Music> songList;
Context c;
public MyAdapter(Context c, List<Music> songList)
{
this.c = c;
this.songList = songList;
}
@Override
public MyAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_list, parent, false);
return new PlanetViewHolder(v);
}
@Override
public void onBindViewHolder(MyAdapter.PlanetViewHolder holder, int position) {
holder.textName.setText(songList.get(position).getName());
holder.textSong.setText(songList.get(position).getSong());
}
@Override
public int getItemCount() {
return songList.size();
}
public static class PlanetViewHolder extends RecyclerView.ViewHolder {
private TextView textName,textSong;
public PlanetViewHolder(View itemView) {
super(itemView);
textName = (TextView) itemView.findViewById(R.id.tv1);
textSong = (TextView) itemView.findViewById(R.id.tv2);
}
}
}