私はアンドロイドが初めてで、別のリストを開くリストを作成しようとしています。同様の科目 (数学、科学など) を選択すると、微積分などのサブ科目/トピックの別のリストが開きます。これは私が持っているもので、サブサブジェクトを追加しません。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/contentListLv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
主な活動コード
package com.example.loginreg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContentListActivity extends Activity {
ListView content_list_lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_list);
//final ListView listview = (ListView) findViewById(R.id.contentListLv);
String[] math= new String[] { "Calculus", "Trigonometry", "Algebra" };
final ArrayList<String> list = new ArrayList<String>();
content_list_lv = (ListView) findViewById(R.id.contentListLv);
for (int i = 0; i < math.length; ++i) {
list.add(math[i]);
//list.add(science[i]);
}
String[] science = new String[] { "Anatomy", "Chemistry and Biology", "Space" };
final ArrayList<String> list2 = new ArrayList<String>();
for (int j=0; j<science.length; ++j) {
list2.add(science[j]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
//science
final StableArrayAdapter adapter2 = new StableArrayAdapter(this, android.R.layout.simple_list_item_2, list2);
content_list_lv.setAdapter(adapter);
content_list_lv
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//view.animate().setDuration(2000).alpha(0);
final String item = (String) parent.getItemAtPosition(position);
Intent l = new Intent(getApplicationContext(),
ContentActivity.class);
l.putExtra("ContentName", item);
l.putExtra("ContentId", id);
startActivity(l);
}
});
content_list_lv
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//view.animate().setDuration(2000).alpha(0);
final String item = (String) parent.getItemAtPosition(position);
Intent l = new Intent(getApplicationContext(),
ContentActivity.class);
l.putExtra("ContentName", item);
l.putExtra("ContentId", id);
startActivity(l);
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.content_list, menu);
return true;
}
}