0

This is my list.java class

package com.FoodOrderApp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class list extends Activity
{
    private ListView m_listview;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_listview = (ListView) findViewById(R.id.listView);

        String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

        m_listview.setAdapter(adapter);
        Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
    }
}

This is my method to call class

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView;
    rootView = inflater.inflate(R.layout.fragment_main, container, false);
    initDishRestaurantView(rootView);
    list l = new list();
}

What is the proper way to call/construct list class (to add items to list)? I know that this question is kinda noob one, but I havent found an answer ;/

Rokas
  • 1,712
  • 2
  • 18
  • 35
  • 1
    Somewhat unrelated, but you should change your class name to be capital `List`. This follows Java conventions. Keep variables and method names lowercase, but capitalize class names. – loeschg Jan 30 '14 at 16:46
  • You're thinking in totally wrong direction. You should read a book about java and a book about Android first. – Yaroslav Mytkalyk Jan 30 '14 at 16:50
  • Do you fragment and is it associated with list? – Raghunandan Jan 30 '14 at 17:12
  • Looks like I really need to dive into java. Been developing c++/c# for years, but java is kinda different. Yes I do use fragments, its not assosiated tho – Rokas Jan 30 '14 at 17:36

3 Answers3

1

You don't instantiate Activity classes like this

list l = new list();

you start them with an Intent and startActivity. You can create a class that doesn't extends Activity and have getters/setters or whatever methods you need. One could even be called addToList(Object foo) which adds to a List object.

You will probably want to take a little time and go through The Java Docs to understand how to create classes and how to use them.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

You have

  public class list extends Activity

Its a activity class. And you have

  list l = new list(); // wrong

Check the answer by Raghav Sood

Can i Create the object of a activity in other class?

Use

 startActivity(new Intent(getActivity(),list.class); 
 // use getActivity() if in fragment or ActivityName.this in activity class

You can pass values using intents.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

startActivity and putStringExtra to Intent

Intent intent = new Intent(this, list.class);
String[] myStrings = new String[] {"item1", "item2"};
intent.putExtra("strings", myStrings);
startActivity(intent);

then at your list class list.java

get argument by use getStringExtra()

Nut Tang
  • 66
  • 5