简体   繁体   中英

Call a fragment onClick in CardView in Android

Just want to call a fragment onClick on a Card.I hav tried all of the handful solutions available but nothing seems to work. here is my code for CardArrayAdapter

package com.example.cards;
import java.util.ArrayList;
import java.util.List;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CardArrayAdapter  extends ArrayAdapter<Card> {
private static final String TAG = "CardArrayAdapter";
private List<Card> cardList = new ArrayList<Card>();
public View row;

static class CardViewHolder {
    TextView line1;
    TextView line2;
     TextView line3;
}

public CardArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

@Override
public void add(Card object) {
    cardList.add(object);
    super.add(object);
}

@Override
public int getCount() {
    return this.cardList.size();
}

@Override
public Card getItem(int index) {
    return this.cardList.get(index);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     row = convertView;
    CardViewHolder viewHolder;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_item_card, parent, false);
        viewHolder = new CardViewHolder();
        viewHolder.line1 = (TextView) row.findViewById(R.id.line1);
        viewHolder.line2 = (TextView) row.findViewById(R.id.line2);
      //  viewHolder.line3 = (TextView) row.findViewById(R.id.line3);

        row.setTag(viewHolder);
    } else {
        viewHolder = (CardViewHolder)row.getTag();
    }
    Card card = getItem(position);
    viewHolder.line1.setText(card.getLine1());
    viewHolder.line2.setText(card.getLine2());
  //  viewHolder.line3.setText(card.getLine3());
    return row;
}

public Bitmap decodeToBitmap(byte[] decodedByte) {
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}


public void onListItemClick(int position, View convertView, ViewGroup parent) {             

      String passTitle = ((TextView)  row.findViewById(R.id.line1)).getText().toString();
      String passURL = ((TextView) row.findViewById(R.id.line2)).getText().toString();

      DetailFragment fragment2 = new DetailFragment();
         Bundle args = new Bundle();

         args.putString("title", passTitle);
         args.putString("url", passURL);
         fragment2.setArguments(args);  

         FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frame_container, fragment2);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }}

just want to know what to use instead of getFragmentManager() to call a fragment coz it shows error of getFragmentManager() is undefined for the type CardArrayAdapter and it is the only error in this java file.

If the Context that you pass into the Constructor is an Activity, you can use the following code:

FragmentManager fragmentManager = ((Activity)getContext()).getFragmentManager();

getFragmentManager() is an Activity method, not a CardArrayAdapter method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM