android中Fragment类的方法没有被调用

时间:2016-04-19 04:40:38

标签: android android-fragments

嗨,我是Android的初学者,在我的应用程序中,我必须在右侧显示SlidingPaneLayout。

到目前为止一切正常。

但是当我点击任何行的SlidingPaneLayout时,我想为此重新加载Listview,我写了下面的代码。

但是这里Listview默认重新加载,但是当我点击SlidingPaneLayout时它没有重新加载。

当我从MainActivity调用getAllTips()方法时,下面的行没有执行。

if (friendArrayList.size() != 0) {
           
     Log.d("=======>" ," data Available");
        ˚   
     adapter.notifyDataSetChanged();
        
    }else{
         
    Log.d("=======>" ,"No data Available");
        
    }

ListViewFragment: -

public class ListViewFragment extends Fragment implements AsyncTaskClass.BackGroundServiceCall {

    private ArrayList<MyTripBean> friendArrayList;
    ListViewAdapter adapter;

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

        View view =inflater.inflate(R.layout.listview_layout,container,false);

        ListView listView = (ListView)view.findViewById(R.id.list_item);

        //set ListView header:-
        inflater =  getActivity().getLayoutInflater();
        View header = inflater.inflate(R.layout.header_listview,listView,false);
        listView.addHeaderView(header);

        //adapter = new ListViewAdapter(getActivity(), R.layout.item_listview, friendArrayList);

        friendArrayList =  new ArrayList<MyTripBean>();

        adapter = new ListViewAdapter(getActivity(),
                R.layout.list_item, friendArrayList);

        listView.setAdapter(adapter);

        getAllTips(getActivity(),9); //Getting friendArrayList data from Services

        return view;
    }

    // get all trips of the User
    public void getAllTips(Activity activity,int tripstatus) {

        try {

            JSONObject json = new JSONObject();
            json.put("TripStatus", tripstatus);
            json.put("medicaidId", "104584743999");

            if (CommonUtilities.isNetWorkStateAvailble(activity)) {

                AsyncTaskClass task = new AsyncTaskClass(this, activity,
                        json);
                task.execute(ServiceUrl.GET_ALL_TRIPS, "1",
                        "Token");

            } else {
                Log.d("==========>", "There is Network Error");
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void doPostExecute(StatusObject statusObject) {

        // TODO Auto-generated method stub
        if (statusObject != null) {

            if (statusObject.getResponseCode() == 401) {
                Log.d("==========>", "Session Has been Expired");

            } else if (statusObject.getResponseCode() == 200) {

                handleResponseData(statusObject.getMessage());

            } else {
                Log.d("==========>", "Server not responding. Please try again later.");
            }

        } else {
            Log.d("==========>", "Server not responding. Please try again later.");
        }
    }

    // handle the response
    public void handleResponseData(String result) {

        Log.d("Final Json==========>", result);

        try {
            Object json = new JSONTokener(result).nextValue();
            if (json instanceof JSONObject) {
                // you have an object

                JSONObject jsonObject = new JSONObject(result);
                if (jsonObject.has("Error")) {

                    if (jsonObject.getString("Error").equalsIgnoreCase("true")) {

                        if (jsonObject.has("message")) {

                            Log.d("==========>", "there is erorr mesage coming from server");
                        }
                    }
                }
            } else if (json instanceof JSONArray) {
                // you have an array
                handleTripsResponse(result);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // handle the json array data
    public void handleTripsResponse(String result) {

        try {

            Type type = new TypeToken<List<MyTripBean>>() {
            }.getType();

            ArrayList<MyTripBean>  newListData = new Gson().fromJson(result, type);
            friendArrayList.addAll(newListData);

            if (friendArrayList.size() != 0) {
                Log.d("=======>" ," data Available");
                adapter.notifyDataSetChanged();
            }else{
                Log.d("=======>" ,"No data Available");
            }

        } catch (Throwable throwable) {

        }
    }
}

MainActivity: -

public class MainActivity extends AppCompatActivity {

    SlidingPaneLayout mSlidingPanel;
    ListView mMenuList;

    String [] MenuTitles = new String[]{"First Item","Second Item","Third Item","Fourth Item"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        mMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ListViewFragment fragment1 = new ListViewFragment();
                fragment1.getAllTips(MainActivity.this,8);
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

代码看起来很好。 OnItemClickListeners看起来很适合listview并且可以正常工作。 我怀疑你是否在方法handleTripsResponse()中得到异常。请尝试在此方法的catch块中打印日志。那么你就会知道异常以及如何解决它。

同样在您的MainActivity中,在onItemClickListener中,不要创建ListViewFragment的新实例,而是使用您在片段事务中附加的片段实例。

 ListViewFragment fragment1 = new ListViewFragment();
            fragment1.getAllTips(MainActivity.this,8);