0

i have on my Activity1 a ListView with 10 items. Based on which item i click, i want to pass just a part of a StringArray to my next Activity. I want to bind passed StringArray over an ArrayAdapter to a GridView.

First Problem: I don´t understand how can i pass something in the next Activity, DEPENDING on the clicked item in the ListView of my Activity1

Second Problem: How can i get just parts of my StringArray. My String Array has 200 items. Now i want to pass (depending on itemclick in Activity1) just the items i really need.

Here is my code

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);


        }


    });

}

}

Here is the Activity2 where i have to bind my items to a 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);

    }



}

}

UPDATE: Here is my string array

<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>
ando
  • 1
  • 3
  • as far as i can see you are passing the data correctly so whats the problem?? – Illegal Argument Jun 07 '14 at 14:24
  • The Problem is that my R.array.provincedescription has 200 items. So when i click one Item on the ListView i need just a part of these 200 items. Now i pass all 200. How can i search my R.array.provincedescription for the items i need, depending on what item i click in my activity1? And how can i pass it, without switch case statement – ando Jun 07 '14 at 14:28
  • one way to do it would be to make your array static and access it via class name – Illegal Argument Jun 07 '14 at 14:37
  • Please can u show me a code example this can be very helpull. Im very new in java and anroid programming – ando Jun 07 '14 at 14:39

2 Answers2

0

if I understand what you want maybe you should take a look at Singleton, I use and works great for now

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

Credits: https://stackoverflow.com/a/16518088/3714926

Community
  • 1
  • 1
  • but how can i search in my R.array.provincedescription. Is there a possibiliy to get the StringArray and search for the needed items. And how can i bring it in relation with the clicked item? – ando Jun 07 '14 at 14:52
0

For resources like yours that are fixed its better to use string-array in your xml file. From Java I prefer to use static array in your case. Heres a sample:

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" };
}

Then I can access the provinces from anywhere from my class like this:

String iWant = Constants.provinces[0];

Very Important Note Static objects are dangerous in a number of scenarios and they are usually present in memory so use them sparingly.
As for the string array you cannot get a single element directly from the string-array defined in xml. For that you need to first get all the elements from the array:

 Resources res = getResources();
String[] planets = res.getStringArray(R.array.provincedescription);
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • i have updated my string array. For example if i click on the first item of my listview then i want to load just the items from stringarray from a1.1, a1.2, a1.3 ... til a1.14. If i click on the secon item of my listviw i want to load just a2.1, a2.2, a2.3 ... til a2.12 and so on. And put them in my GridView. Is there anyway to solve this problem. Thank u – ando Jun 07 '14 at 15:01
  • just send the position of the click in your intent then in the other activity use String iWant = Constants.provinces[clickedFromPrevActivity]; where clickedFromPrevActivity is the position where the item was clicked – Illegal Argument Jun 07 '14 at 15:03
  • thank u Illegal Argument. But i dont understand how can i take my items from stringarray and populate my gridview. I want let my items in a string array, i won´t make a class with 200 items, if i understand what u mean. – ando Jun 07 '14 at 15:07
  • Constants.provinces will give you all the 200 items you need I was showing you how to get a specific items. A note please try first then understand – Illegal Argument Jun 07 '14 at 15:15
  • Okay now i tried but this isn´t the effect i want. I need parts of my R.array.provincedescription. With your code i get just the provinces not my items from the stringarray – ando Jun 07 '14 at 15:22