0

I am trying to set 5 strings, airport_finish, date_finish, time_finish, persons_finish, address_finish & flightnr_finish, in one Item of ListView which associated to the Class DemandFragment Which extends Fragment.

Will it be possible? How do I do it?

Right now all the strings are in separate items.

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...
            final FragmentManager fm = getActivity().getFragmentManager();
            final Fragment fragment = new DemandFragment();

            // 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.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"));
                                }

                                Bundle bundle = new Bundle();
                                bundle.putStringArrayList("listPost", arrayPost);
                                fragment.setArguments(bundle);
                                fm.beginTransaction().replace(R.id.content_main, fragment).commit();

                            } else {
                                // error
                            }
                        }
                    });
                }
            });

            return true;

DemandFragment:

    public class DemandFragment extends Fragment {

    ListView lv;

    ArrayList yourList;
    ArrayAdapter adapter;

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

        if(rootView != null){
            lv = (ListView) rootView.findViewById(R.id.listDemand);

            ArrayList<String> arraypost = getArguments().getStringArrayList("listPost");
            Log.d("arraylist", "arraypost");

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

            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 rootView;
    }

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

        setHasOptionsMenu(true);

    }

}
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Rang92
  • 67
  • 9

1 Answers1

1

What you want to do is to create a custom item for you ListView.

And then you can make custom row how ever you want with xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Header"/>

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text"/>


</LinearLayout>

Which will look like that:

enter image description here

In your case you will have only 5 textviews...

HERE IS HOW YOU DO IT: https://stackoverflow.com/a/15832564/3180983

You will need to extend BaseAdapter and override some methods in order to make it work.

Community
  • 1
  • 1
BananaBuisness
  • 339
  • 2
  • 18