gpt4 book ai didi

android - DrawerLayout 内的 CardView

转载 作者:行者123 更新时间:2023-11-29 19:15:27 24 4
gpt4 key购买 nike

当我向列表中添加一些想法时,我的卡片是空白的:

 list.add(new LocBean("ss", "dd", "aa"));

我哪里弄错了?下面是我的部分代码。

我的 onCreate 方法:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

new LoadAllLocs(this).execute(); //load list in background from database

titles = getResources().getStringArray(R.array.loc_array);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

locRecycler = (RecyclerView) findViewById(R.id.recycler_view);
locRecycler.setHasFixedSize(true);
locLayoutManager = new GridLayoutManager(this, 1);
locRecycler.setLayoutManager(locLayoutManager);
list.add(new LocBean("ss", "dd", "aa"));

locAdapter = new LocAdapter(list);
locRecycler.setAdapter(LocAdapter);

我的适配器类:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;


public class LocAdapter extends RecyclerView.Adapter<LocAdapter.ViewHolder> {
ArrayList<LocBean> list;
public LocAdapter(ArrayList<LocBean> list){
this.list= list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.loc_view_item, parent, false);

return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MyBean loc = list.get(position);
holder.textName.setText(loc.getName());
holder.textLoc.setText(loc.getLoc());
holder.textRating.setText(loc.getRating());
}

@Override
public int getItemCount() {
return list.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder{
protected TextView textName;
protected TextView textLoc;
protected TextView textRating;
public ViewHolder(View v){
super(v);
textName = (TextView) v.findViewById(R.id.name);
textLoc = (TextView) v.findViewById(R.id.loc);
textRating = (TextView) v.findViewById(R.id.rating);

}
}
}

loc_view_layout.xml:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:padding="12dp"
android:layout_margin="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loc"
android:layout_below="@+id/name"
android:layout_alignParentStart="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rating"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
/>

</RelativeLayout>

</android.support.v7.widget.CardView>

和activity_maps.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.masaj.myapp.MapsActivity" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>

最佳答案

在为您的适配器设置数据列表后,确保您调用notifyDataSetChanged() 以便适配器可以反射(reflect)您的更改

adapter = new LocAdapter();

//将您的列表 (list.add(new LocBean("ss", "dd", "aa")));) 传递给您的适配器并打电话

adapter.notifyDataSetChanged()

您可以选择调用此方法,无论是您的 Activity 还是在您更改适配器数据的方法中

祝您编码愉快:)

关于android - DrawerLayout 内的 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com