我的片段(称为"购买")列表视图。所以当然我有一个适配器。问题是我的适配器中有一个数学过程,我需要将结果发送到我的txtview。但不幸的是,我的文本观点在我的片段中。那么我该如何发送这个变量呢?有些像
((FragmentBuy) Fragment).send(prize);
但当然这不起作用。谢谢你的帮助!
我有一个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) {
}
});
}
当然还有我的寻呼机
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;
}
}
我的适配器
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;
}
我的片段
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;
}
答案 0 :(得分:0)
因此,直接的方法是直接在Activity中为数学计算创建变量。以下是步骤:
答案 1 :(得分:0)
您可以使用事件总线与适配器类中的片段进行通信。它简化了组件之间的通信。它还可以很好地处理活动,片段和后台线程。对于简单的通信过程,您可以使用Otto事件总线..
有关详情,请访问此处.. Otto event bus
答案 2 :(得分:0)
您可以使用界面在Listview适配器和Fragment之间进行通信。
步骤:
1.使用send方法创建一个接口。 2.在Fragment中实现该方法。 3.当crating适配器传递接口的引用(即实现类的接口..这是当前片段。) 4)在使用该Interface对象调用send方法的适配器中。