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

时间:2014-03-16 13:40:44

标签: android listview android-fragments fragment

我正在使用导航抽屉,其中有一个活动和许多碎片。我的项目中的片段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;

   }

  }

1 个答案:

答案 0 :(得分:0)

在你的onResume()方法调用listview.setAdapter(someData)或者这个(你必须为listview设置适配器):

@Override
    public void onActivityCreated(Bundle paramBundle)
    {   
        super.onActivityCreated(paramBundle);
        this.listView = ((ListView) getActivity().findViewById(R.id.my_listView));        
        this.listView.setAdapter(this.someAdapter);
    }