繁体   English   中英

2 ListFragment中的刷新问题

[英]2 Refreshing Issues within a ListFragment

我的ListFragment有两个问题:单击XML文件中定义的按钮后,我想刷新ListFragment。最初将DataAdapter的数据加载到TitlesFragment中的AsyncTask中。

我还没有找到一种方法来创建可以访问AsyncTask的按钮的代码-并刷新我的TitlesFragment

另外一点:每次我更改手机的方向时,Listfragment都会自动更新,这绝对不是所需的行为。

public class ClosestPlaces extends FragmentActivity {

private static KantinenListe kantinen;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_ACTION_BAR);        
    setContentView(R.layout.kantinen_results);

}   

/**
 * This is the "top-level" fragment, showing a list of items that the
 * user can pick.  Upon picking an item, it takes care of displaying the
 * data to the user as appropriate based on the currrent UI layout.
 */

public static class TitlesFragment extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;


    private class BuildKantinen extends AsyncTask<String, Integer, KantinenListe>   {

        private KantinenListe kantinen;


        @Override
        protected KantinenListe doInBackground(String... params) {
            try{

                Gson gson = new Gson();             
                // SOAP Test
                String NAMESPACE = "http://tempuri.org/";
                String METHOD_NAME = "fullSyncGPS";
                String SOAP_ACTION = "http://tempuri.org/IDatenService/fullSyncGPS";
                String URL = "http://webserviceURL?wsdl";

                SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

                PropertyInfo pi = new PropertyInfo();
                request.addProperty("radius",10);
                request.addProperty("lat", "14.089201");
                request.addProperty("lng", "02.136459");
                request.addProperty("von", "01.09.2011");
                request.addProperty("bis", "01.09.2011");

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);        

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

                String resultData = result.toString(); 


                resultData = "{\"meineKantinen\":"+resultData+"}";
                this.kantinen = gson.fromJson(resultData, KantinenListe.class);
                Log.i("test", "blubber" );
            }
            catch(Exception e)
            {
                 e.printStackTrace();
            }
                return this.kantinen;

        }

        @Override
        protected void onPostExecute(KantinenListe result) {
            // populate the List with the data
            Log.i("test", "postexecute" );
            setListAdapter( new MenuAdapter(getActivity(), R.layout.simple_list_item_checkable_1, kantinen.getMeineKantinen()));
        }
    }


    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        new BuildKantinen().execute("test");                       

        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

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

    /**
     * Helper function to show the details of a selected item, either by
     * displaying a fragment in-place in the current UI, or starting a
     * whole new activity in which it is displayed.
     */
    void showDetails(int index) {
        mCurCheckPosition = index;

            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Log.i("Test",Integer.toString(index));
            Intent intent = new Intent();
            intent.setClass(getActivity(), BeAPartner.class);
            startActivity(intent);
    }
}

public static class MenuAdapter extends ArrayAdapter {

    private LayoutInflater mInflater;
    private List<Kantine> items;
    private Context context;

    public MenuAdapter(Context context, int textViewResourceId, List<Kantine> items) {
        super(context, textViewResourceId);
        mInflater = LayoutInflater.from(context);
        this.items = items;
        this.context = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.menu_row, parent, false);
            holder = new ViewHolder();
            holder.color = (TextView) convertView.findViewById(R.id.color);
            holder.title = (TextView) convertView.findViewById(R.id.detail);
            holder.subdetail = (TextView) convertView.findViewById(R.id.subdetail);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // Fill in the actual story info
        Kantine s = items.get(position);

        s.setName( Html.fromHtml(s.getName()).toString() );
        if (s.getName().length() > 35)
            holder.title.setText(s.getName().substring(0, 32) + "...");
        else
            holder.title.setText(s.getName());

        Log.i("display", "Here I am");
        return convertView;
    }


 }
    static class ViewHolder {
        TextView color;
        TextView title;
        TextView subdetail;
    }   

要阻止该活动重新启动,您只需在运行该片段的活动上设置android:configChanges属性即可。

    android:configChanges="orientation"

告诉系统不要在方向更改时重新启动活动的设置,仅用于更改方向。

至于按钮,请使用android:onClick =“ myFunction”属性以XML设置点击侦听器。 然后在您的片段中定义此函数:

    public void myFunction(View v)
    {
             new myAsync.execute('test');       
    }

更改电话方向时,它将重新启动活动。 您需要永久保存的所有内容都需要保存在onSaveInstanceState(Bundle savedInstanceState)中,并使用onRestoreInstanceState(Bundle savedInstanceState)进行还原。

至于单击按钮时更新数据,请尝试在适配器上调用notifyDataSetChanged()。 如果这不起作用,您可能只需要再次运行asynctask。

好的,我认为最好的做法是在新答案中发布可能的解决方案-出于明显的可读性原因:

public void reload(View v)
{
    if (getSupportFragmentManager().findFragmentById(R.id.titles) != null) { 
            Fragment titles = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.titles);
            //recreate the fragment
            titles.onActivityCreated(null);
    }
}

正是我想要的(重新加载数据)! 但是...我认为此解决方案的内存占用可能不好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM