如何在另一个片段中将ListAdapter用于ListView?

时间:2016-01-22 01:01:15

标签: java android listview android-arrayadapter

我正在尝试将DemandAdapter(称为适配器)设置为DemandFragment中的另一个ListView,而DemandFragment中的ListView称为lv。

如何在FinishFostFragment中将适配器放入/设置到DemandFragment中的ListView(callend lv)?

FinishPostFragment:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add_post_finish:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            FragmentManager fm = getActivity().getFragmentManager();

            // put strings to listview in DemandFragment
            ParseObject post = new ParseObject("Posts");

            post.put("airport", airport_finish.getText().toString());
            post.put("date", date_finish.getText().toString());
            post.put("time", time_finish.getText().toString());
            post.put("persons", persons_finish.getText().toString());
            post.put("address", address_finish.getText().toString());
            post.put("flightnr", flightnr_finish.getText().toString());

            post.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    airport_finish.setText("");
                    date_finish.setText("");
                    time_finish.setText("");
                    persons_finish.setText("");
                    address_finish.setText("");
                    flightnr_finish.setText("");

                    ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
                    query.whereEqualTo("playerName", "Dan Stemkoski");
                    query.findInBackground(new FindCallback<ParseObject>() {
                        public void done(List<ParseObject> scoreList, ParseException e) {
                            if (e == null) {
                                ArrayList arraypost = new ArrayList<String>();

                                for (ParseObject j : scoreList) {

                                    arraypost.add(j.getString("airport"));
                                    arraypost.add(j.getString("date"));
                                    arraypost.add(j.getString("time"));
                                    arraypost.add(j.getString("persons"));
                                    arraypost.add(j.getString("address"));
                                    arraypost.add(j.getString("flightnr"));
                                }

                                ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, arraypost);

                                Log.d("score", "Retrieved " + scoreList.size() + " scores");
                            } else {
                                Log.d("score", "Error: " + e.getMessage());
                            }
                        }
                    });
                }
            });

                    fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();
            return true;

DemandFragment:

public class DemandFragment extends Fragment {

ListView lv;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_demand, container, false);

    lv = (ListView) rootView.findViewById(R.id.listDemand);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // clicked on item show post

        }
    });

    return rootView;
}

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

    setHasOptionsMenu(true);

}

}

1 个答案:

答案 0 :(得分:0)

这是我最近使用的一个snippit ......

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_game_history, container, false);
    if(view != null){
        //Init views
        yourList = new ArrayList<>();
        lv= (ListView) view.findViewById(R.id.list_Games);
        adapter = new YourAdapter(getContext(), yourList)
        lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // clicked on item show post

    }
});

}
return view;
}

括号可能有点偏,我试着从头顶做这个。如果有效,请告诉我!