我的Activity1上有一个包含10个项目的ListView。根据我点击的项目,我想将StringArray的一部分传递给我的下一个Activity。我想通过ArrayAdapter将传递的StringArray绑定到GridView。
第一个问题: 我不明白如何在下一个Activity中传递一些内容,依赖于我的Activity1的ListView中单击的项目
第二个问题: 我怎样才能得到我的StringArray的一部分。我的String Array有200个项目。现在我想传递(取决于Activity1中的itemclick)我真正需要的项目。
这是我的代码
public class MainActivity extends Activity {
// ListView items
String[] provinces = new String[]{
"Prozentrechnung, Terme und Brüche",
"Gleichungen",
"Ungleichungen und Beträge",
"Geraden, Parabeln und Kreise",
"Trigonometrie",
"Potenzen, Wurzeln und Polynome",
"Exponentialfunktionen und Logarithmen",
"Trigonometrische Funktionen",
"Differenzialrechnung",
"Integralrechnung"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView provincelist = (ListView)findViewById(R.id.lvProvinceNames);
//add header to listview
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listheader, provincelist, false);
provincelist.addHeaderView(header, null, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, provinces);
provincelist.setAdapter(adapter);
provincelist.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
String province = provinces[position-1];
//we retrieve the description of the juices from an array defined in arrays.xml
String[] provincedescription = getResources().getStringArray(R.array.provincedescription);
//List<String> aufgabenListe = new ArrayList<String>(Arrays.asList(provincedescription));
//final String provincedesclabel = provincedescription[position-1];
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("position",position);
intent.putExtra("province", province); //aktualisieren der Titel in der DetailActivity
intent.putExtra("provincedescription", provincedescription); //befüllen der GridView
startActivity(intent);
}
});
}
}
这是Activity2,我必须将我的项目绑定到GridView。
public class DetailActivity extends Activity {
String title;
String[] array;
int position;
//int image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailactivity);
TextView tvTitleLabel = (TextView)findViewById(R.id.tvTitleLabel);
GridView gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter;
Bundle extras = getIntent().getExtras();
position = extras.getInt("position");
if (extras != null) {
title = extras.getString("province");
tvTitleLabel.setText(title);
/////Fehlermeldung: array = null --> NullPointerException
array = extras.getStringArray("provincedescription");
gridView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
});
//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
//gridView.setAdapter(adapter);
}
}
}
更新:这是我的字符串数组
<string-array name="provincedescription">
<item>A1.1</item>
<item>A1.2</item>
<item>A1.3</item>
<item>A1.4</item>
<item>A1.5</item>
<item>A1.6</item>
<item>A1.7</item>
<item>A1.8</item>
<item>A1.9</item>
<item>A1.10</item>
<item>A1.11</item>
<item>A1.12</item>
<item>A1.13</item>
<item>A1.13</item>
<item>A1.14</item>
<item>A2.1</item>
<item>A2.2</item>
<item>A2.3</item>
<item>A2.4</item>
<item>A2.5</item>
<item>A2.6</item>
<item>A2.7</item>
<item>A2.8</item>
<item>A2.9</item>
<item>A2.10</item>
<item>A2.11</item>
<item>A2.12</item>
<item>A3.1</item>
<item>A3.2</item>
<item>A3.3</item>
<item>A3.4</item>
<item>A3.5</item>
<item>A3.6</item>
<item>A3.7</item>
<item>A3.8</item>
<item>A3.9</item>
<item>A3.10</item>
<item>A3.11</item>
<item>A3.12</item>
</string-array>
答案 0 :(得分:0)
如果我明白你想要什么,也许你应该看看Singleton,我现在使用并且效果很好
答案 1 :(得分:0)
对于像你这样固定的资源,最好在xml文件中使用字符串数组。从Java开始,我更喜欢在你的情况下使用静态数组。下面是一个样本:
public class Constants {
public static final String[] provinces = new String[] {
"Prozentrechnung, Terme und Brüche", "Gleichungen",
"Ungleichungen und Beträge", "Geraden, Parabeln und Kreise",
"Trigonometrie", "Potenzen, Wurzeln und Polynome",
"Exponentialfunktionen und Logarithmen",
"Trigonometrische Funktionen", "Differenzialrechnung",
"Integralrechnung" };
}
然后我可以从我班级的任何地方访问各省:
String iWant = Constants.provinces[0];
非常重要的注意事项 静态对象在许多情况下都很危险,它们通常存在于内存中,因此请谨慎使用它们 对于字符串数组,您无法直接从xml中定义的字符串数组中获取单个元素。为此,您需要首先从数组中获取所有元素:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.provincedescription);