I have an activity with list view. On click of an item of the list view an intent is send to the next list containing the value of the clicked item. The list is being generated using an xml file.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Selected item
String cat = ((TextView) view).getText().toString();
// Launching new Activity on selecting single Item List
Intent intent = new Intent(getApplicationContext(), ContentListing.class);
// Sending data to new activity
intent.putExtra("cat", cat);
startActivity(intent);
}
On the next activity I again have to read an xml file but this file will vary with respect to the item clicked on previous list.
// Creating a handle to capture data sent from previous activity
Intent intent = getIntent();
// Storing the category into a variable
String cat = intent.getStringExtra("cat");
// Storing string resources into Array
String[] itemList = getResources().getStringArray(R.array.itemList);
I wanted to do something like String[] itemList = getResources().getStringArray(R.array.cat); i.e. R.array.variable which doesn't work for me. I am new to java and android so any kind of help(that is easy to understand and implement) is welcome.
Also, I wanted the name of this second activity to be different each time with respect to the item clicked. What should I be doing for this?
EDIT:
This is my updated code which give the error about getContext()
public class ContentListing extends ListActivity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_listing);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// Creating a handle to capture data sent from previous activity
Intent intent = getIntent();
// Storing the category into a variable
String cat = intent.getStringExtra("cat");
setTitle(cat);
// Line that shows error
int resourceId = Resources.getSystem().getIdentifier(cat, "array", getContext().getPackageName());
Log.d("Print message1: ", String.valueOf(resourceId)+"\n");
if(resourceId != 0) {
Log.d("Print message: ", String.valueOf(resourceId)+"\n");
// Storing string resources into Array
//String[] itemList = getResources().getStringArray(R.array.itemList);
String[] itemList = getResources().getStringArray(resourceId);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, itemList));
ListView lv = getListView();
// Listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single Item List
Intent intent = new Intent(getApplicationContext(), ContentListing.class);
// Sending data to new activity
intent.putExtra("product", product);
startActivity(intent);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_content_listing, menu);
return true;
}
}
R.array.categoryto get the category values, wouldn't I?R.esource. You would have to structure your XML in a categorical hierarchy.