Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Array of Objects for ListView using ListFragment
    primarykey
    data
    text
    <p>I seem to be having troubles implementing a list of objects into a simple ListView.</p> <p>Here is my object class for each of the list elements. package com.example.android.fragments;</p> <pre><code>package com.example.android.fragments; public class ListObject { public String objectname; //name of schedule public boolean[] Days = { false, //monday false, //tuesday false, //and soforth false, false, false, false}; public int starthour, startminute, stophour, stopminute; //times from TimePicker public boolean vibrate; public ListObject() { objectname = null; //if doesnt work, do objectname = ""; vibrate = false; starthour = 8; startminute = 0; stophour = 15; stopminute = 0; } public ListObject(String name, boolean[] newdays, boolean vib, int a, int b, int c, int d) { objectname = name; Days = newdays; vibrate = vib; starthour = a; startminute = b; stophour = c; stopminute = d; } @Override public String toString() { String startampm = "am"; String stopampm = "am"; if (starthour &gt; 11) //turns into am/pm { startampm = "pm"; starthour -= 12; } if (stophour &gt; 11) //turns into am/pm { stopampm = "pm"; stophour -= 12; } String daysstring = null ; //initializes string representing days activated for(int i = 0; i &lt; 7; i++) //add into string the correct days { if (Days[i] == true) { if(i == 0) daysstring += "M, "; else if (i == 1) daysstring += "Tu, "; else if (i == 2) daysstring += "W, "; else if (i == 3) daysstring += "Th, "; else if (i == 4) daysstring += "F, "; else if (i == 5) daysstring += "Sa, "; else daysstring += "Su, "; } } daysstring = daysstring.substring(0, daysstring.length() - 3); //removes the last ', ' daysstring += ": "; String timestring = starthour + ":" + startminute + " " + startampm + " - " + stophour + ":" + stopminute + " " + stopampm; return objectname + "\n" + daysstring + timestring; } } </code></pre> <p>My main activity sets the layout to mainlayout which is a FrameLayout</p> <pre><code>package com.example.android.fragments; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; public class MainActivity extends FragmentActivity implements MainListFragment.OnListSelectedListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainlayout); // Check whether the activity is using the layout version with // the fragment_container FrameLayout. If so, we must add the first fragment if (findViewById(R.id.fragment_container) != null) { //meaning, if using phone version // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of ExampleFragment MainListFragment firstFragment = new MainListFragment(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } } public void onArticleSelected(int position) { // Capture the article fragment from the activity layout InfoFragment articleFrag = (InfoFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); //article_fragment exists in layout-large if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // phone layout - swap frags // Create fragment and give it an argument for the selected article InfoFragment newFragment = new InfoFragment(); Bundle args = new Bundle(); args.putInt(InfoFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } } } </code></pre> <p>Here is where the problem is, in my ListFragment here which is supposed to be open at the start of the application, I cannot simply create 3 of the objects and put them in the listview defined in my listlayout.xml which has the ListView id of mainlistview</p> <pre><code>package com.example.android.fragments; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ListActivity; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainListFragment extends ListFragment{ //used to extend ListActivity{ OnListSelectedListener mCallback; // The container Activity must implement this interface so the frag can deliver messages public interface OnListSelectedListener { /** Called by ListFragment when a list item is selected */ public void onArticleSelected(int position); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // We need to use a different list item layout for devices older than Honeycomb int layout = Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; //using this now to add properties ListView mainlist = (ListView)getView().findViewById(R.id.mainlistview); //INFO FOR MADE UP OBJECTS boolean[] Days1 = { true, //monday false, //tuesday true, //and soforth false, true, false, false}; boolean[] Days2 = { false, //monday false, //tuesday false, //and soforth false, true, true, true}; boolean[] Days3 = { false, //monday false, //tuesday false, //and soforth true, false, false, false}; //create sample objects ListObject[] scheduleobjs = { new ListObject("Club Practice", Days1, true, 1, 0, 5, 25), new ListObject("Work", Days2, true, 3, 30, 14, 20), new ListObject("Church", Days3, true, 12, 5, 14, 20) }; // String[] liststring = // { // scheduleobjs[0].toString(), // scheduleobjs[1].toString(), // scheduleobjs[2].toString() // }; // ArrayList&lt;ListObject&gt; arraylist; //add objects to arraylist // arraylist.add(scheduleobjs[0]); // arraylist.add(scheduleobjs[1]); // arraylist.add(scheduleobjs[2]); String[] arraylist = new String[] { scheduleobjs[0].toString(), scheduleobjs[1].toString(), scheduleobjs[2].toString() }; ArrayAdapter&lt;String&gt; arrayadapter = new ArrayAdapter&lt;String&gt;(this, layout, arraylist); // setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(), layout, )) mainlist.setAdapter(arrayadapter); //inflate here??? } @Override public void onStart() { super.onStart(); // When in two-pane layout, set the listview to highlight the selected list item // (We do this during onStart because at the point the listview is available.) // if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) { // getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); COMMENTED OUT BECAUSE USED IN 2 PANE LAYOUT // } } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception. try { mCallback = (OnListSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnListSelectedListener"); } } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Notify the parent activity of selected item mCallback.onArticleSelected(position); // Set the item as checked to be highlighted when in two-pane layout getListView().setItemChecked(position, true); } } </code></pre> <p>You can see I have commented out previous attempts at doing this using the object classes' overridden toString(), where am I going wrong and what is the best way to implement this? I am wanting to eventually create a better listview since this only displays a string, and I want different sized and colored text with a checkbox for each object, but for now I am just trying to get this working.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload