1

I have in my fragment (called "buy") a listview. So of course I have a adapter. The problem is that I have a math process in my adapter and I need the result will be sent to my txtview. But unfortunatly my textview is in my fragment. So How can I send this variable? some like

((FragmentBuy) Fragment).send(prize);

But of course this doesn't work. Thanks for helping!

I have a MainActivity

public class MainActiviry extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);


        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Buy"));
        tabLayout.addTab(tabLayout.newTab().setText("Sell"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

And of course my pager

        public class PagerAdapter extends FragmentStatePagerAdapter {
        int mNumOfTabs;
        public PagerAdapter(FragmentManager fm, int NumOfTabs) {
            super(fm);
            this.mNumOfTabs = NumOfTabs;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    FragmentCompras tab1 = new FragmentBuy();
                    return tab1;
                case 1:
                    FragmentDespensa tab2 = new FragmentSell();
                    return tab2;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    }

My adapter

    public class ListViewAdapter extends BaseAdapter{

public ArrayList<HashMap<String, String>> list;

Activity activity;
int contador = 0;
public ListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
    super();
    this.activity = activity;
    this.list = list;

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}
private class ViewHolder {
TextView name;
TextView marc, cant, prec;}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();
    if (convertView == null){
        convertView = inflater.inflate(R.layout.list_colum, null);
        holder = new ViewHolder();

        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.marc = (TextView) convertView.findViewById(R.id.marc);
        holder.cant = (TextView) convertView.findViewById(R.id.cant);
        holder.prec = (TextView) convertView.findViewById(R.id.prec);
        convertView.setTag(holder);
    }
    else{

        holder=(ViewHolder) convertView.getTag();

    }

    HashMap<String, String> map = list.get(position);
    holder.name.setText(map.get(FIRST_COLUMN));
    holder.marc.setText(map.get(SECOND_COLUMN));
    holder.prec.setText(map.get(THIRD_COLUMN));
    holder.cant.setText(map.get(FOURTH_COLUMN));



    return convertView;

}

And my fragment

public class FragmentBuy extends android.support.v4.app.Fragment implements View.OnClickListener{
    private ArrayList<HashMap<String, String>> list;
    //HashMap<String, String> temp = new HashMap<String, String>();
    private Button scanBtn;
    static boolean guardar;
    private TextView txttotal, formatTxt, contentTxt, lblmx, textView, cantidadproducto;
    ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activy_micompra, container, false);
        final DbHelper admin = new DbHelper(view.getContext(), null);
        list = new ArrayList<HashMap<String, String>>();
        setHasOptionsMenu(true);
        guardar = false;

        txttotal = (TextView)view.findViewById(R.id.total); //this is my textview
        textView = (TextView) view.findViewById(R.id.mxcompra);
        cantidadproducto = (TextView)view.findViewById(R.id.cantidadproducto);
        return view;
    }
  • please follow http://stackoverflow.com/a/37535113/5381331 for send data from `ListViewApdapter` back to `Fragment` – Linh Jul 21 '16 at 06:43
  • So you mean to say that You want to send total amount from your List which is in Fragment A to the TextView in Fragment B right? and the both are in a ViewPager – Cyph3rCod3r Jul 21 '16 at 06:59
  • I want to send a variable from my listviewadapter to my fragment A (send to my txttotal on my fragment called buy) – Patricia Tapia Jul 21 '16 at 07:06

3 Answers3

0

So a straight forward approach will be creating a variable for your math calculation directly in your Activity. So here are the steps:

  1. Create an interface callback which sends the data to your activity from the ListAdapter
  2. Create instance variable for your fragment which holds public method send(prize) in your MainActivity
  3. Call your method directly from the activity by fragmentBuy.send(prize) in the callback of the interface
Cyph3rCod3r
  • 1,978
  • 23
  • 33
0

You can go with Event bus to communicate back to fragment from your adapter class.It simplifies the communication between components.It also performs well with Activities, Fragments, and background threads.For simple communication process you can go with Otto Event Bus..

For more detail , you can go here.. Otto event bus

Riten
  • 2,879
  • 3
  • 24
  • 28
0

You can communicate between Listview adapter and Fragment using interface.

Steps:

1.Create an interface with send method. 2.Implement that method in your Fragment. 3. While crating adapter pass a reference of Interface(i.e interface implementing class.. which is current fragment.) 4) In adapter using that Interface object call send method.

Jogendra Gouda
  • 405
  • 4
  • 17