Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access fragment from adapter

The idea is i have a listview where each item is a product, when I click in the item, I need to go to another Fragment from the click which is inside the adapter of the listview.

My adapter is:

package info.android.adapter;

import info.android.ProductFragment;
import info.android.R;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductOffersListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<info.android.model.ProductsOffers> navProOffers;

    public ProductOffersListAdapter(Context context, ArrayList<info.android.model.ProductsOffers> navProOffers) {
        this.context = context;
        this.navProOffers = navProOffers;
    }

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

    @Override
    public info.android.model.ProductsOffers getItem(int position) {        
        return navProOffers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.fragment_row, null);
        }

        convertView.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                          // I NEED CLALL HERE FRAGMENT_PRODUCT.XML                             
              }
            });

        ImageView img = (ImageView) convertView.findViewById(R.id.image); 

        TextView txtCodigo = (TextView) convertView.findViewById(R.id.txtCodigo);
        TextView txtCiudad = (TextView) convertView.findViewById(R.id.txtCiudad);               
        TextView txtVF = (TextView) convertView.findViewById(R.id.txtVF);

        String sVF =  Double.toString(getItem(position).ValorFinal) + "€";  

        txtVF.setText(sVF);
        txtCiudad.setText(getItem(position).ciudad);
        txtCodigo.setText(getItem(position).codigo);

        convertView.setTag(getItem(position).codigo);

        img.setImageBitmap(getItem(position).Imagen);

        return convertView;
    }
}

You have to asume all code is working.

How can I go to the product Fragment inside the adapter?

like image 428
David Avatar asked Jun 23 '14 17:06

David


2 Answers

You would need to pass it in your Constructor. For Example:

public class ProductOffersListAdapter extends BaseAdapter
{
 private Context context;
 private ArrayList<info.android.model.ProductsOffers> navProOffers;
 Fragment myFragment;

 public ProductOffersListAdapter(Context context, ArrayList<info.android.model.ProductsOffers> navProOffers, Fragment myFragment) 
 {
    this.context = context;
    this.myFragment = myFragment;
    this.navProOffers = navProOffers;
 }

...
like image 185
zuscher Avatar answered Oct 21 '22 12:10

zuscher


What I did to resolve this is:

Intent i = new Intent(v.getContext(), MainActivity.class);                      
i.putExtra("fragment_value", "2");
v.getContext().startActivity(i);

In main class I get the parameter as value and with a switch I set fragment in main which I am interested.

like image 1
David Avatar answered Oct 21 '22 12:10

David