如何将StringArray的一部分传递给基于ListItem点击的下一个活动



我在我的Activity1上有一个包含10个项目的ListView。基于哪个项目我点击,我想传递只是一个StringArray的一部分到我的下一个活动。我想把传入的StringArray通过ArrayAdapter绑定到GridView。

第一个问题:我不明白我怎么能在下一个活动中传递一些东西,这取决于我的Activity1的ListView中单击的项目

第二个问题:我怎样才能得到我的StringArray的部分。我的字符串数组有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>

如果我明白你想要什么,也许你应该看看Singleton,我现在使用和工作得很好

https://gist.github.com/Akayh/5566992

学分:https://stackoverflow.com/a/16518088/3714926

对于像您这样固定的资源,最好在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);

相关内容

  • 没有找到相关文章

最新更新