0

I am trying to display fields of these objects in a listview which I have working and is in a fragment. My issue is with any example I have that is working it appears to be for arraylists not linkedlists. I will then convert to arraylist to puplate listview or find a way of using linkedlist.

In my code I create a new linked list and populate it in mainactivity with objects. I have a getter within mainactivity to retrieve the list. I have a fragment listview (created after parsing data and populating linkedlist). I am trying to populate the listview items with fields of these objects but cant figure out how to do this. I am at a stage where before i even attempt to do anything with the linked list I am unable to access it in any way without my app crashing.

As below I am creating and populating my linked list etc. and then in listviewfragment trying to access it and eventually afterwards use/change it to populate my listview. Please excuse the mess of code I have been trying multiple different ways to do this and have the same issue every time of crashing upon startup with no error messages.

From what I understand I am creating a list of items within my fragment which is being set to the returned list from main activity and I have tried a simple for each loop to get the description but no matter what changes i make to any piece of code the app crashes instantly on startup without any error message.

MainActivity public class MainActivity extends AppCompatActivity implements OnClickListener { private TextView rawDataDisplay;

private Button startButton;
private String result = "";
private String url1="http://www.bgs.ac.uk/images/logos/bgs_c_w_227x50.gif";
private String[] items;
Map<String, List<ItemClass>> mylist;
LinkedList <ItemClass> alist = null;
ItemClass[] arrayofitems = null;
List <ItemClass> Testlist = null;
List<ItemClass> list = null;
ItemClass item = null;
private String urlSource="http://quakes.bgs.ac.uk/feeds/MhSeismology.xml";
private IncidentFragment incidentfragment;
private DateRefineFragment daterefinefragment;
private SearchFragment searchfragment;
private ViewFragment viewfragment;
//private LogoFragment logofragment;
private ListviewFragment listviewfragment;
private ListtextviewFragment listtextviewfragment;
private BtntextviewFragment btntextviewfragment;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    LinkedList <ItemClass> alist = null;
    items = new String[50];
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startProgress();//Parse data and prepare for user selection on startup of app
    // Set up the raw links to the graphical components
    daterefinefragment = new DateRefineFragment();
    searchfragment = new SearchFragment();
    viewfragment = new ViewFragment();
    listviewfragment = new ListviewFragment();
    listtextviewfragment = new ListtextviewFragment();
    btntextviewfragment = new BtntextviewFragment();


    //fragments
    FragmentManager manager1 = getSupportFragmentManager();
    FragmentTransaction transaction1 = manager1.beginTransaction();
    transaction1.replace(R.id.listtextviewFragment, listtextviewfragment);
    transaction1.commit();

    FragmentManager manager2 = getSupportFragmentManager();
    FragmentTransaction transaction2 = manager2.beginTransaction();
    transaction2.replace(R.id.btntextviewFragment, btntextviewfragment);
    transaction2.commit();

    FragmentManager manager3 = getSupportFragmentManager();
    FragmentTransaction transaction3 = manager3.beginTransaction();
    transaction3.replace(R.id.daterefineFragment, daterefinefragment);
    transaction3.commit();

    FragmentManager manager4 = getSupportFragmentManager();
    FragmentTransaction transaction4 = manager4.beginTransaction();
    transaction4.replace(R.id.searchFragment, searchfragment);
    transaction4.commit();

    FragmentManager manager5 = getSupportFragmentManager();
    FragmentTransaction transaction5 = manager5.beginTransaction();
    transaction5.replace(R.id.viewFragment, viewfragment);
    transaction5.commit();

    FragmentManager manager6 = getSupportFragmentManager();
    FragmentTransaction transaction6 = manager6.beginTransaction();
    transaction6.replace(R.id.listviewFragment, listviewfragment);
    transaction6.commit();

    // More Code goes here
}

public void onClick(View aview) //probs dont need this with button fragments
{
    //startProgress();
}

public void startProgress()
{
    // Run network access on a separate thread;
    new Thread(new Task()).start();

} //



// Need separate thread to access the internet resource over network
// Other neater solutions should be adopted in later iterations.
private class Task implements Runnable
{
    @Override
        public void run()
        {

            AccessWeb aw = new AccessWeb();//Create object to make use of class
            result = aw.getResult(urlSource);//Runs code within class to retrieve resulting string of data
            new Thread(new dataparsing()).start();//runs parsing of data on seperate thread
        }

        }

public class dataparsing implements Runnable
{
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void run() { //creates new ParseData object and sets alist to returned templist.
        ParseData dp = new ParseData();
        alist = dp.parseData(result);
        //arrayofitems = alist.toArray();
        list = new ArrayList<ItemClass>(alist);
        //Log.d("UI thread", "ARRAYOFITEMS " +  arrayofitems[0].);
         mylist = alist.stream().collect(Collectors.groupingBy(ItemClass::getlocation)); //Set mylist to returned list grouped by location
        Testlist = mylist.get("HIGHLAND");//set test list to list  returned by get with location of passed string ("GWENT")
        item = Testlist.get(1); //set item to object returned in first index of previously returned list (Testlist)
         Log.d("UI thread", "List test = " + list.get(0) + " " + item.getdescription());//prints location of first item in
        // previously returned list
    }
}


public  void loggingMethod()  //Method used for logging information.
{

        Log.d("UI thread", "I am the UI thread");
        //rawDataDisplay.setText(result); doesnt work as its not being run in main thread
        Log.d("UI thread", "first in list printed is " + alist.getFirst() + " and last in list printed is " + alist.getLast());

// Log.d("UI thread", "url1 is " + url1); FUUUUUCK THIS! CREATE A NEW PROJECT TO GET THIS WORKING MAN }

public List<ItemClass> getList() {
    return alist;
}

Listview Fragment public class ListviewFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener {

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


    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.frag_listviewfragment, container, false);








    return v;
}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{


    super.onViewCreated(view, savedInstanceState);
    ListView listView = (ListView) view.findViewById(R.id.list);
    listView.setOnItemClickListener((AdapterView.OnItemClickListener) this);

    List<ItemClass> items = ((MainActivity)getActivity()).getList();

    for(ItemClass i : items){
        Log.d("UI thread", "I am the UI thread " + i.getdescription());
    }
    //UserAdapter<ItemClass> adapter = new UserAdapter<ItemClass>(users);



}

1 Answer 1

0

Your app crash seems to be from getList() value.

It seems to return null at the beginning.

Please try to setup callback method to notify ListviewFragment class when alist value is not null value.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have any guidance on how to go about this?
I have attempted these changes from research and nothing is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.