this is what I am trying to do.
I have a listview that is populated from a xml resource file saved in /values/menu.xml
it has this code in it:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="menuchoices">
<item name="pv">Present Value</item>
<item name="fv">Future Value</item>
<item name="bond">Bond Pricing</item>
</string-array>
</resources>
So far so simple. Then my Main.java file looks like this: (adapted from other listview questions here)
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.menuchoices)
));
}
public void onListItemClick(ListView partent, View v, int position, long id) {
if ("pv".equals(getResources().getStringArray(R.array.menuchoices)[position])){
setContentView(R.layout.presentvalue);
}
}
}
Basically I read that launching new activities all the time is not the best practice so i just told the if statement to change the contentview. The problem is — Nothing happens when i click on the first item in the list. i also tried substituting “pv” with “Present Value” but it did not help either.
I think this i beacuase i took the code from posts like this here List View selection to start a new Activity) but I don’t know how to change it so it works with the external xml resource file.
This should be a simple fix right?
THanks in advance
Max
P.s. all the other stuff works (the presentvalue.xml file is in the layout folder and the list is properly displayed when i run the app)
EDIT //
here is the problematic line
public void onListItemClick(ListView parent, View v, int position, long id) {
if (view.getText().toString().equals("Present Value")){
startActivity(new Intent(Main.this, PresentValue.class));
}
}
The function
onListItemClick()is typically used with a ListActivity. There are a couple fixes to this:extends Activitytoextends ListActivity.@+id/listView1to@android:id/listimplements OnItemClickListenerin your class definition afterextends ActivityonListItemClicktoonItemClickTry moving your
menuchoicesblock:Into your string.xml file, then we can simplify your adapter (assuming you did changes #1 above):
We can also condense how you test the menu choice:
Becomes:
(Minor point, you have a typo in the word “parent”)
How’s that?