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;
}
So a straight forward approach will be creating a variable for your math calculation directly in your Activity. So here are the steps:
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
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(ie interface implementing class.. which is current fragment.) 4) In adapter using that Interface object call send 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.