Rokas
Rokas

Reputation: 1712

calling another class in android (java)

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 ;/

Upvotes: 0

Views: 96

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

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.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

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.

Upvotes: 1

Nut Tang
Nut Tang

Reputation: 76

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()

Upvotes: 0

Related Questions