インターフェイスでclickListenerを作成する方法??これは、データベースからのバックグラウンドのロードデータと完全に機能します。私は他のクラスでリスナーを作成したい場合しかし、その後MapsActivityは アダプタから他のクラスへのClickListener
が動作していない私のmainActivity:public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface {
private DrawerLayout drawerLayout;
private RecyclerView clubRecycler;
private RecyclerView.LayoutManager clubLayoutManager;
private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();
private RecyclerView.Adapter clubAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MyMapFragment()).commit();
new LoadAllClubs(this).execute(); //load list in background from database
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
clubRecycler = (RecyclerView) findViewById(R.id.recycler_view);
clubRecycler.setHasFixedSize(true);
clubLayoutManager = new LinearLayoutManager(this);
clubRecycler.setLayoutManager(clubLayoutManager);
}
@Override
public void finishDataLoad(ArrayList<HashMap<String, String>> clubs) {
Iterator<HashMap<String, String>> iterator = clubs.iterator();
Map<String, String> map = new HashMap<String, String>();
while (iterator.hasNext()){
map = iterator.next();
clubList.add(new ClubBean(map.get("name"),map.get("localization"), map.get("score")));
}
clubAdapter = new ClubAdapter(clubList, this);
clubRecycler.setAdapter(clubAdapter);
}
}
マイアダプタクラス:
public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.MyViewHolder> {
private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();
private OnClubClickListener onClubListClickListener;
Context mContext;
public ClubAdapter(ArrayList<ClubBean> clubList, Context mContext){
this.clubList = clubList;
this.mContext = mContext;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.club_view_item, parent, false);
MyViewHolder myHolder = new MyViewHolder(cv);
return myHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final ClubBean club = clubList.get(position);
CardView cardView = holder.cardView;
TextView textClubName = (TextView) cardView.findViewById(R.id.club_name);
TextView textClubLoc = (TextView) cardView.findViewById(R.id.club_loc);
TextView textClubRating = (TextView) cardView.findViewById(R.id.club_rating);
textClubName.setText(club.getClubName());
textClubLoc.setText(club.getClubLoc());
textClubRating.setText(club.getClubRating());
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
onClubListClickListener.onClicked(club);
}
});
}
public interface OnClubClickListener{
void onClicked(ClubBean club);
}
public void setOnClickListener(OnClubClickListener onClubListClickListener) {
this.onClubListClickListener = onClubListClickListener;
}
@Override
public int getItemCount() {
return clubList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
private CardView cardView;
public MyViewHolder(CardView v){
super(v);
cardView = v;
}
}
}
マイマップフラグメント:
public class MyMapFragment extends Fragment implements OnMapReadyCallback, ClubAdapter.OnClubClickListener{
GoogleMap mGoogleMap;
MapView mMapView;
View mView;
public MyMapFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.map_fragment, container, false);
return mView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = (MapView) mView.findViewById(R.id.map);
if (mMapView != null){
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getActivity().getApplicationContext());
mGoogleMap = googleMap;
LatLng city = new LatLng(10.4158773,10.6337789);
mGoogleMap.addMarker(new MarkerOptions().position(city).title("city"));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(city, 11));
}
@Override
public void onClicked (ClubBean club) {
ClubBean bean = club;
Log.d("Name: ", bean.getClubName());
}
}
?あなたのスタックトレースをここに入れてください –
java.lang.NullPointerException:ヌルオブジェクトリファレンス com.clubのインタフェースメソッド 'void com.club.clubapp.ClubAdapter $ OnClubClickListener.onClicked(com.club.clubapp.ClubBean)'を呼び出そうとしています。 clubapp.ClubAdapter $ 1.onClick(ClubAdapter.java:47) – Masaj