Android 通过反按从另一个片段返回后,片段中的列表视图不显示

Android 通过反按从另一个片段返回后,片段中的列表视图不显示,android,listview,android-fragments,fragment,Android,Listview,Android Fragments,Fragment,我正在使用导航抽屉,其中有一个活动和许多片段。我的项目中的片段A用于搜索总线,它扩展了listfragment,我有显示总线信息的自定义列表视图。单击列表项后,我通过向堆栈中添加片段A移动到另一个片段B。但是当从片段B返回到片段A时,列表视图没有显示..之后,片段A中的列表视图永远不会显示,即使我执行另一个操作来显示信息 如果有人能帮助显示列表视图,我还需要在从片段B返回后在片段A中显示列表视图数据 调用片段B代码 fgTran = getSupportFragmentManager().beg

我正在使用导航抽屉,其中有一个活动和许多片段。我的项目中的片段A用于搜索总线,它扩展了listfragment,我有显示总线信息的自定义列表视图。单击列表项后,我通过向堆栈中添加片段A移动到另一个片段B。但是当从片段B返回到片段A时,列表视图没有显示..之后,片段A中的列表视图永远不会显示,即使我执行另一个操作来显示信息

如果有人能帮助显示列表视图,我还需要在从片段B返回后在片段A中显示列表视图数据

调用片段B代码

fgTran = getSupportFragmentManager().beginTransaction();

     fg=new BusInfoFg();
     fg.setArguments(Bus_Info);
     fgTran.replace(R.id.frame_container, fg);//tran.
     fgTran.addToBackStack(null);
     fgTran.commit();
编辑: 破译代码

 Fragment A code

    public class LocationsFg extends ListFragment{

AutoCompleteTextView from,to;
ProgressDialog barProgressDialog;
Db_helper dbh;
CommonUtilities Com_Util;
TextView emptyLV;
ImageView ivTo_ClrText;
ImageView ivFrom;

//String source,dest;
String Bus_No,Bus_Source,Bus_Dest,SearchMsg;
String Bus_Id;
String Bus_From;
String Bus_To;
String nearLoc;

ListAdapter search_adapter;
ArrayAdapter<String> atv_adapter;

ArrayList<HashMap<String, String>> busList,busId;
ArrayList<String> busAll = new ArrayList<String>();

static final String TAG_BUS_SEARCH = "search_info";
static final String TAG_BUS_ID = "busid";
static final String SBUS_NO="Bus_No";
static final String SBUS_SOURCE="Bus_Src";
static final String SBUS_DEST="Bus_Dest";

ListView listView;
ListView preFindListView;
Cursor curs;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    setRetainInstance(true);

    View rootView = inflater.inflate(R.layout.activity_location_fragment, container,false);

    dbh=new Db_helper(getActivity());
    dbh.openDB();
    Com_Util = new CommonUtilities(getActivity());

    busList = new ArrayList<HashMap<String, String>>();
    busId = new ArrayList<HashMap<String, String>>();


    from=(AutoCompleteTextView) rootView.findViewById(R.id.fromAutoCompleteTextView);
    to=(AutoCompleteTextView) rootView.findViewById(R.id.toAutoCompleteTextView);

    emptyLV = (TextView)rootView.findViewById(android.R.id.empty);

}

return rootView;
}


public void onResume()
{
    super.onResume();

    listView=(ListView) getActivity().findViewById(android.R.id.list);

    atv_adapter = new ArrayAdapter<String>getActivity(),                 android.R.layout.simple_dropdown_item_1line,GBUS_STOPS);

    from.setAdapter(atv_adapter);
    to.setAdapter(atv_adapter);

    if(busList.size()>0)
    {
         search_adapter = new sbusAdapter(
               getActivity(), busList,
                  R.layout.redcross, new String[] {SBUS_NO,SBUS_SOURCE,SBUS_DEST}, 
                  new int[] { R.id.textView1 });
         setListAdapter(search_adapter);
    }


    from.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> av, View arg1, int pos,long id) {
      // TODO Auto-generated method stub

    SBUS_FROM = Bus_From = from.getText().toString();
    SBUS_TO = Bus_To = to.getText().toString();

    if(Bus_From.length() >1 || Bus_To.length()>1)
    {
        busList.clear();            
        new searchBuses().execute();

    }
}
});

to.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> av, View arg1, int pos,long id) {
      // TODO Auto-generated method stub

    SBUS_FROM = Bus_From = from.getText().toString();
    SBUS_TO = Bus_To = to.getText().toString();

    if(Bus_From.length() >1 || Bus_To.length()>1)
    {
        busList.clear();
        new searchBuses().execute();
    }
}
});


}


@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
      super.onListItemClick(l, v, position, id);

        @SuppressWarnings("unchecked")
        HashMap<String, String> data=(HashMap<String, String>) 

        listView.getItemAtPosition(position);

        String Bus_Source=data.get("Bus_Src");
        String Bus_Dest=data.get("Bus_Dest");
        String Bus_No=data.get("Bus_No");

        Bundle bundle = new Bundle();
        bundle.putString("bus_no", Bus_No);
        bundle.putString("start", Bus_Source);
        bundle.putString("end", Bus_Dest);
        bundle.putString("from", Bus_From);
        bundle.putString("to", Bus_To);

        MainActivity ma=(MainActivity) getActivity();

        ma.callBusInfo(bundle);

}

    }


class searchBuses extends AsyncTask<String,String,String>
{

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub

         curs=dbh.busSearch(Bus_From,Bus_To);
         if(curs.getCount()>0)
         {
             if (curs.moveToFirst()) 
             {
                 do
                 {
                  Bus_Id=curs.getInt(curs.getColumnIndex("bus_id"))+"";
                      Bus_No=curs.getString(curs.getColumnIndex("bus_no"));
                      Bus_Source=curs.getString(curs.getColumnIndex("bus_source"));
                      Bus_Dest=curs.getString(curs.getColumnIndex("bus_dest"));

                      SearchMsg=" "+Bus_No+" "+Bus_Source+" => "+ Bus_Dest+" ";

                      HashMap<String, String> mapSR = new HashMap<String, String>();
                          mapSR.put(SBUS_NO,Bus_No);
                              mapSR.put(SBUS_SOURCE,Bus_Source);
                              mapSR.put(SBUS_DEST,Bus_Dest);
                                  busList.add(mapSR);

                 }while(curs.moveToNext());
              }
         }

        return null;

    }

    protected void onPostExecute(String params)
    {

               search_adapter = new sbusAdapter(
                       getActivity(), busList,
                        R.layout.redcross, new String[] {SBUS_NO,SBUS_SOURCE,SBUS_DEST}, 
                        new int[] { R.id.textView1 });
               setListAdapter(search_adapter);

                if(busList.size()==0)
                {
                    emptyLV.setText("No Bus found");
                }

    }
}

public void onDestroyView()
{
    super.onDestroy();
    dbh.close();
}


    }

    class sbusAdapter extends  ArrayAdapter<HashMap<String, String>>
    {

private ArrayList<HashMap<String, String>> items;
Context context;
String dbusdir,dloc,ddist;
String[] bus_no;


 public sbusAdapter(Context context, ArrayList<HashMap<String, String>> items,
         int layout,String [] from,int [] to)
     {
     super(context,layout,items);
        this.context = context;
        this.items = items; 
}

 public class ViewHolder {
       TextView tBus,tBusNo,tBusSrc,tBusDes;
        public String getTextBus() {
            return tBus.getText().toString();
        }
 }


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

       ViewHolder holder = null;
       View vix=convertView;
        if (convertView == null) 
       {
           if (vix == null) {
               LayoutInflater vi = (LayoutInflater)getContext().getSystemService

         (Context.LAYOUT_INFLATER_SERVICE);
               convertView = vi.inflate(R.layout.redcross, null);
                }

       holder = new ViewHolder();
       holder.tBusNo = (TextView) convertView.findViewById(R.id.textview_busno);
       holder.tBusSrc = (TextView) convertView.findViewById(R.id.sourcePoint);
       holder.tBusDes = (TextView) convertView.findViewById(R.id.destPoint);
       convertView.setTag(holder);

       } 
       else {
        holder = (ViewHolder) convertView.getTag();
       }
    HashMap<String, String> currentData = new HashMap<String, String>();
       currentData = items.get(position);

       if (currentData != null) {
           holder.tBusNo.setText(currentData.get(LocationsFg.SBUS_NO));
           holder.tBusSrc.setText(currentData.get(LocationsFg.SBUS_SOURCE));
           holder.tBusDes.setText(currentData.get(LocationsFg.SBUS_DEST));
       }       

       return convertView;

   }

  }
Fragment一段代码
公共类位置SFG扩展了ListFragment{
AutoCompleteTextView从、到;
ProgressDialog barProgressDialog;
dbh;
公用事业公司;
TextView emptyLV;
ImageView ivTo_ClrText;
图像视图;
//字符串源,dest;
字符串总线号、总线源、总线目的地、搜索消息;
字符串总线标识;
串总线从;
串总线到;
近LOC的字符串;
ListAdapter搜索适配器;
阵列适配器atv_适配器;
ArrayList总线列表,busId;
ArrayList busAll=新的ArrayList();
静态最终字符串TAG\u BUS\u SEARCH=“SEARCH\u info”;
静态最终字符串TAG_BUS_ID=“busid”;
静态最终字符串SBUS_NO=“Bus_NO”;
静态最终字符串SBUS_SOURCE=“Bus_Src”;
静态最终字符串SBUS_DEST=“Bus_DEST”;
列表视图列表视图;
ListView-preFindListView;
光标光标;
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
setRetainInstance(真);
视图根视图=充气机。充气(R.layout.activity\u location\u片段,容器,false);
dbh=新的Db_帮助程序(getActivity());
dbh.openDB();
Com_Util=newcommonutilities(getActivity());
busList=newarraylist();
busId=newarraylist();
from=(AutoCompleteTextView)rootView.findViewById(R.id.fromAutoCompleteTextView);
to=(AutoCompleteTextView)rootView.findViewById(R.id.toAutoCompleteTextView);
emptyLV=(TextView)rootView.findviewbyd(android.R.id.empty);
}
返回rootView;
}
恢复时公开作废()
{
super.onResume();
listView=(listView)getActivity().findViewById(android.R.id.list);
atv_adapter=new ArrayAdaptergetActivity(),android.R.layout.simple_下拉列表_项目线,GBUS站点);
from.setAdapter(atv_适配器);
至.setAdapter(atv_适配器);
if(busList.size()>0)
{
搜索\ U适配器=新sbusAdapter(
getActivity(),总线列表,
R.layout.redcross,新字符串[]{SBUS_NO,SBUS_SOURCE,SBUS_DEST},
新的int[]{R.id.textView1});
setListAdapter(搜索适配器);
}
来自.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView av、视图arg1、内部位置、长id){
//TODO自动生成的方法存根
SBUS_FROM=Bus_FROM=FROM.getText().toString();
SBUS_TO=Bus_TO=TO.getText().toString();
如果(总线从.length()>1 | |总线到.length()>1)
{
busList.clear();
新建searchBus().execute();
}
}
});
to.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView av、视图arg1、内部位置、长id){
//TODO自动生成的方法存根
SBUS_FROM=Bus_FROM=FROM.getText().toString();
SBUS_TO=Bus_TO=TO.getText().toString();
如果(总线从.length()>1 | |总线到.length()>1)
{
busList.clear();
新建searchBus().execute();
}
}
});
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id)
{
super.onListItemClick(左、右、位置、id);
@抑制警告(“未选中”)
HashMap数据=(HashMap)
listView.getItemAtPosition(位置);
字符串Bus_Source=data.get(“Bus_Src”);
字符串Bus_Dest=data.get(“Bus_Dest”);
字符串Bus_No=data.get(“Bus_No”);
Bundle=新Bundle();
bundle.putString(“总线号”,总线号);
bundle.putString(“开始”,总线源);
bundle.putString(“结束”,总线目的地);
bundle.putString(“from”,Bus_-from);
bundle.putString(“到”,总线到);
MainActivity ma=(MainActivity)getActivity();
ma.callBusInfo(bundle);
}
}
类searchBus扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
curs=dbh.busSearch(总线从,总线到);
if(curs.getCount()>0)
{
if(curs.moveToFirst())
{
做
{
总线Id=curs.getInt(curs.getColumnIndex(“总线Id”)+“”;
总线号=curs.getString(curs.getColumnIndex(“总线号”);
总线源=curs.getString(curs.getColumnIndex(“总线源”);
Bus_Dest=curs.getString(curs.getColumnIndex(“Bus_Dest”);
SearchMsg=“”+总线号+“+总线源+”=>“+总线目的地+”;
HashMap mapSR=新的HashMap();
mapSR.put(SBUS_编号、公共汽车_编号);
mapSR.put(备用电源、总线电源);
mapSR.put(备用总线目的地、总线目的地);
总线列表。添加(mapSR);
}while(curs.moveToNext());
}
}
返回null;
}
受保护的void onPostExecute(字符串参数)
{
搜索\ U适配器=新sbusAdapter(
getActivity(),总线列表,
R.layout.redcross,新字符串[]{SBUS_NO,SBUS_SOURCE,SBUS_DEST},
新的int[]{R.id.textView1});
setListAdapter(搜索适配器);
if(busList.size()==0)
{
emptyLV.setText(“未找到总线”);
}
}
}
公共voi
@Override
    public void onActivityCreated(Bundle paramBundle)
    {   
        super.onActivityCreated(paramBundle);
        this.listView = ((ListView) getActivity().findViewById(R.id.my_listView));        
        this.listView.setAdapter(this.someAdapter);
    }