当我添加一些列表时,我有空白卡:
list.add(new LocBean("ss", "dd", "aa"));
我在哪里犯错?我下面代码的一部分。
我的发作方法:
@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((,以便适配器可以反映您的更改
adapter = new LocAdapter();
//通过列表(list.add(new locbean(" ss"," dd"," aa"((;(并致电
adapter.notifyDataSetChanged()
您选择呼叫此方法的选择,无论是您的活动/在您搅动适配器数据的方法中
有有趣的编码:(
idrawerlayout您必须有2个元素,一个片段,一个用于抽屉。我看到recyclerview是您抽屉layout中的第三元素。您将在左菜单片段的XML文件中放置RecyClerview。