リストビューのアクティビティがあります。リスト ビューのアイテムをクリックすると、クリックされたアイテムの値を含む次のリストにインテントが送信されます。リストは、xml ファイルを使用して生成されています。
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);
}
次のアクティビティでは、再度 xml ファイルを読み取る必要がありますが、このファイルは前のリストでクリックされた項目によって異なります。
// 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);
String[] itemList = getResources().getStringArray(R.array.cat);
R.array.variableのようなことをしたかったのですが、これはうまくいきません。私はJavaとAndroidが初めてなので、あらゆる種類のヘルプ(理解しやすく実装しやすいもの)を歓迎します。
また、この 2 番目のアクティビティの名前は、クリックされた項目ごとに異なるものにしたいと考えました。このために私は何をすべきですか?
編集:
これは私の更新されたコードで、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;
}
}