java - Not sure how to reference stringArray in main thread -


as can see i've been chopping , altering snippets of code i've found on place, , it's taken ages!

i'm stuck on context menu isn't capturing touch selection when item selected, , eclipse showing error i'm not sure how correct.

i'm not sure how reference array list in main thread, know how if it's array string, not on main thread.

the line * 1 gives error.

just wondering if take peek @ it?

import java.util.arraylist; import android.app.listactivity; import android.media.mediaplayer; import android.os.bundle; import android.view.contextmenu; import android.view.contextmenu.contextmenuinfo; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.adapterview.adaptercontextmenuinfo; import android.widget.listview; import android.widget.toast; public class mainactivity extends listactivity { private arraylist<sound> msounds = null; private soundadapter madapter = null; static mediaplayer mmediaplayer = null; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); registerforcontextmenu(getlistview()); this.getlistview().setselector(r.drawable.selector); msounds = new arraylist<sound>(); msounds.add(s); s = new sound(); s.setdescription("echoex"); s.setsoundresourceid(r.raw.echoex); msounds.add(s); s = new sound(); s.setdescription("edge"); s.setsoundresourceid(r.raw.edge); msounds.add(s); s = new sound(); s.setdescription("enterprise"); s.setsoundresourceid(r.raw.enterprise); msounds.add(s); s = new sound(); s.setdescription("envy"); s.setsoundresourceid(r.raw.envy); msounds.add(s); s = new sound(); s.setdescription("etcher"); s.setsoundresourceid(r.raw.etcher); msounds.add(s); madapter = new soundadapter(this, r.layout.list_row, msounds); setlistadapter(madapter); } @override public void onlistitemclick(listview parent, view v, int position, long id){ sound s = (sound) msounds.get(position); mediaplayer mp = mediaplayer.create(this, s.getsoundresourceid()); mp.start();  }@override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) {     super.oncreatecontextmenu(menu, v, menuinfo);     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.context_menu, menu);   } @override public boolean oncontextitemselected(menuitem item) {     adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo();     ********string[] names = getresources().getstring(r.array.msounds);     switch(item.getitemid()) {     case r.id.setasnotification:           toast.maketext(this, "applying " + getresources().getstring(r.string.setas) +                       " context menu option " + names[(int)info.id],                       toast.length_short).show();           return true;     default:           return super.oncontextitemselected(item);     } }} 

sound.java:

public class sound { private string mdescription = ""; private int msoundresourceid = -1; private int miconresourceid = -1; public void setdescription(string description) { mdescription = description; } public string getdescription() { return mdescription; } public void setsoundresourceid(int id) { msoundresourceid = id; } public int getsoundresourceid() { return msoundresourceid; } public void seticonresourceid(int id) { miconresourceid = id; } public int geticonresourceid() { return miconresourceid; } } 

your target parameter might incorrect.

********string[] names = getresources().getstring(r.array.msounds); 

i have feeling targeting msound, of arraylist<sound> type. method doesn't work way. have create xml-file if want use above method. here below example,

<?xml version="1.0" encoding="utf-8"?>   <resources>       <array name="myarray">           <item>first song</item>           <item>second song</item>           <item>third song</item>           <item>fourth song</item>           <item>fifth song</item>       </array> </resources> 

this saved xml file, below soundarray.xml. can use above method as

string[] names = getresources().getstringarray(r.array.soundarray); 

this should work.

or if want use sound objects

(it's not android class?) have loop through list , return string of said sound object.

int length = msounds.size(); // length of msounds object string[] names = new string[length]; // creates fixed array strings for(int = 0; < length; i++) {      // add sound name string array      names[i] = msounds.get(i).getdescription(); // returns string name } 

but if sound class custom class you, have see if contains method returns name of said sound item. if doesn't (or if don't have overwritten tostring()-method, return reference address, don't want see...


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -