简体   繁体   中英

Having trouble with new Classes in Android

I'm really rusty with Java and I'm new to Android and I put together some code that works perfectly fine when its all in the MainActivity Class. But now I want to clean up the functions and put them in their own classes to make the code look pretty. Here is an example of what I am trying to do. I am not getting any errors in the IDE, yet my device crashes the second the MainActivity tries to run the setArtist and setAlbum functions.

public class MainActivity extends Activity {

private ListView lv;
String path, selectedFromList;
ArrayAdapter<String> adapter;
public ViewFiller k = new ViewFiller();

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

    lv = (ListView) findViewById(R.id.listView1);

    k.setArtist();
    k.setAlbums();

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k.getAlbums());
    lv.setAdapter(adapter);

    ImageButton albumButton = (ImageButton) findViewById(R.id.albumButton);
    ImageButton artistButton = (ImageButton) findViewById(R.id.artistButton);

    albumButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            k.album(lv, adapter);
        }
    });

    artistButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            k.artist(lv, adapter);
        }
    });

}

}

And heres the class the class I am having trouble with...

public class ViewFiller extends MainActivity  {

public ViewFiller(){
    }

ArrayList<String> Albums = new ArrayList<String>();
ArrayList<String> Artist = new ArrayList<String>();
LinkedHashSet<String> hs = new LinkedHashSet<String>();

public ArrayList<String> getAlbums() {
    return Albums;
}

public void setAlbums() {

    String selection = AudioColumns.IS_MUSIC + " != 0";
    String[] projection = { BaseColumns._ID, AudioColumns.ALBUM };
    Cursor c = this.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
            selection, null, null);

    while (c.moveToNext()) {
        Albums.add(c.getString(1));
    }
    hs.clear();
    hs.addAll(Albums);
    Albums.clear();
    Albums.addAll(hs);
}


public ArrayList<String> getArtist() {
    return Artist;
}

public void setArtist() {

    String selection = AudioColumns.IS_MUSIC + " != 0";
    String[] projection = { BaseColumns._ID, AudioColumns.ARTIST, };
    Cursor c = this.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
            selection, null, null);

    while (c.moveToNext()) {
        Artist.add(c.getString(1));
    }

    hs.clear();
    hs.addAll(Artist);
    Artist.clear();
    Artist.addAll(hs);
}

public void artist(ListView lv, ArrayAdapter<String> s) {
    lv.setAdapter(null);
    s = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Artist);
    lv.setAdapter(s);
}

public void album(ListView lv, ArrayAdapter<String> s) {
    lv.setAdapter(null);
    s = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Albums);
    lv.setAdapter(s);

}

}

I don't think you want ViewFiller to be an activity... but I might be wrong. If it's just a regular class which you're using to create some object used by the main activity, and you need access to the MainActivity just pass the context into the constructor of the object.

Edit:

Try something like this, i'm a little rusty with Android too, so i'm not sure if it'll work (I might be completely wrong, sorry):

Activity activity;
public ViewFiller(Activity activity){
     this.activity = activity;
}

and... instead of this.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null); ,

try:

activity.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);

And in your main activity: ViewFiller k = new ViewFiller(this);

However, i'm not sure if this is what you want. So I apologize if I went wrong somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM