Android 为什么片段没有执行?
我试图在android中调用一个新片段,当我单击按钮搜索时,它正在工作,但在执行代码之后:Android 为什么片段没有执行?,android,android-fragments,fragment,Android,Android Fragments,Fragment,我试图在android中调用一个新片段,当我单击按钮搜索时,它正在工作,但在执行代码之后: Activity ac = (Activity) v.getContext(); AllFragment to = new AllFragment(null, false); FragmentTransaction fragTransaction = ac.getFragmentManager().beginTransaction(); fragTransaction.replace(R.id.fram
Activity ac = (Activity) v.getContext();
AllFragment to = new AllFragment(null, false);
FragmentTransaction fragTransaction = ac.getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.frame_container, to);
fragTransaction.addToBackStack(null);
fragTransaction.commit();
之后,我在emulator中看到下一个屏幕:
然后我期望的是产品的列表视图。我在片段中的代码工作得非常完美
我的片段代码是:
@SuppressLint("UseValueOf")
public class AllFragment extends ListFragment {
String codigo = null;
boolean esOferta = false;
public AllFragment(String codigo, boolean esOferta) {
this.esOferta = esOferta;
this.codigo = codigo;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
try
{
DataBaseManager db = new DataBaseManager(inflater.getContext());
Integer poblacion = null;
Integer provincia = null;
String codigo = null;
ArrayList<ProductsOffers> data = null;
if (db.existKey("PROVINCIA")) provincia = new Integer(db.getDatoByKey("PROVINCIA"));
if (db.existKey("POBLACION")) poblacion = new Integer(db.getDatoByKey("POBLACION"));
if (db.existKey("CODIGO")) codigo = db.getDatoByKey("CODIGO");
if (!this.esOferta) data = IAndroid.getAllProductsAndOffers(provincia, poblacion, this.codigo, null, null);
else {
data = IAndroid.getProductsByOfferCode(this.codigo);
}
if (data != null) setListAdapter(new ProductOffersListAdapter(inflater.getContext(), data));
}
catch (Exception ex)
{
}
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
}
在我的模拟器中,我使用的是4.0.3版
我的适配器代码是:
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);
}
ProductsOffers po = (ProductsOffers) getItem(position);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView vwtipo = (TextView) v.findViewById(R.id.vwTipo);
TextView vwcodigo = (TextView) v.findViewById(R.id.txtCodigo);
Activity act = (Activity) v.getContext();
ProductFragment to = new ProductFragment(vwcodigo.getText().toString(), vwtipo.getText().equals("OFERTA"));
FragmentTransaction fragTransaction = act.getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.frame_container, to);
fragTransaction.addToBackStack(null);
fragTransaction.commit();
}
});
try
{
ImageView img = (ImageView) convertView.findViewById(R.id.image);
TextView vwtipo = (TextView) convertView.findViewById(R.id.vwTipo);
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) + "€";
String aux = "";
aux = po.tipo;
vwtipo.setText((aux.equals("O"))? "OFERTA":"PRODUCTO");
vwtipo.setBackgroundColor(aux.equals("O")?Color.RED: Color.WHITE);
txtVF.setText(sVF);
txtCiudad.setText(po.ciudad);
txtCodigo.setText(po.codigo);
convertView.setTag(po.codigo);
img.setImageBitmap(po.Imagen);
} catch (Exception ex) {}
return convertView;
}
}
为什么我的片段没有执行?我认为你的代码很好,只是你的模拟器有点慢。哪种配置最好?对于模拟器,请使用英特尔x86模拟器。这应该会加快速度。