Question

I have create a very simple app which shows a ListView which in turn shows the contents of a ListArray. I have also added a button which when clicked, adds another item to the ListView. Now here's the problem: After reading other people's code I have noticed their MainActivity file tends to be the default file and all their classes and created in separate files. I am trying to put my ArrayAdapter, Button and ListArray code into a separate file.. just to make it all look nicer! Does anyone have any ideas as to why when I run my app (it compiles fine/ no errors) My ListView no longer populates or adds an item on the button click. Thanks for your time! Here's my code:

MainActivity.Java

package com.example.shoppingapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

ArrayAdapterClass

package com.example.shoppingapp;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ArrayAdapterClass extends Activity {

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

    final ArrayList<String> myStringArray = new ArrayList<String>();
    myStringArray.add("One");

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         android.R.layout.simple_list_item_multiple_choice, myStringArray);

    ListView listView = (ListView) findViewById(R.id.listview);
    listView.setAdapter(adapter);

    final Button button = (Button) findViewById(R.id.addnote);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             // Perform action on click
             // Toast textPopup = Toast.makeText(getApplicationContext(), "Hello",     Toast.LENGTH_SHORT);
             // textPopup.show();
                myStringArray.add("Two");
                adapter.notifyDataSetChanged();  
                }
             });    
}
}

edit: I should also add... When all of the code is in a single file (the MainActivity one) it all runs fine.

Was it helpful?

Solution

If you're using stock API classes (no methods to override or implement), there isn't a huge need to create individual files for each. Typically new class files are only created when you new to define a new class or extend an existing one.

It seems the ArrayAdapterClass extends an Activity. An Activity is an individual app screen. You would typically move between Activities by using an Intent. It seems that your MainActivity has no way to start the ArrayAdapterClass.

OTHER TIPS

Wow - there's a lot here to explain. First, your ArrayAdapter should not extend "Activity" - it should extend "BaseAdapter." And unless you are actually extending it (e.g. overriding methods, etc.) there isn't much need. But here is a good tutorial:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

There are others. Maybe looking at some of those will help you see when and why you would need to separate the class files.

Specifically, you should also read up on how "findViewById" works and what "setContentView" does. That should help explain why your code compiles even though it doesn't really make sense. Also, read up quickly on the Activity lifecycle, if you haven't already.

It will show nothing. There is no relation between main activity and array adapter class. Main activity is called first, but you do nothing on main activity. Nothing will happen.

Yes you should not extends activity in array adapter unless you want to make a new screen in your Android app

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top