2

I'm new in Android Studio so I would appreciate if you have patience with me and (my noob question).

I created a ListView with a custom adapter which displays the name of the country and value. I want that when you click on a Country it will start a new activity and display on it the name of the country, its value and the population.

My problem is how I'm suppose to get the corresponding data from the string array that is in the resource file? For example, when I click on Argentina, it will put its name,note and money(which is not showing in the listview) in (different?) key(s) and send it to the new activity where I will get it back and display.

Here is my code.

String.xml:
    <string-array name="r_arrayPais">
        <item>Argentina</item>
        <item>Bolívia</item>
        <item>Brasil</item>
        <item>Chile</item>
        <item>Colômbia</item>
        <item>Equador</item>
        <item>Guiana Francesa</item>
        <item>Guiana</item>
        <item>Paraguai</item>
        <item>Peru</item>
        <item>Surinami</item>
        <item>Uruguai</item>
        <item>Venezuela</item>
    </string-array>
<string-array name="r_arrayNota">
    <item>Pop 10kk</item>
    <item>Pop 11kk</item>
    <item>Pop 12kk</item>
    <item>Pop 13kk</item>
    <item>Pop 14kk</item>
    <item>Pop 15kk</item>
    <item>Pop 16kk</item>
    <item>Pop 17kk</item>
    <item>Pop 18kk</item>
    <item>Pop 19kk</item>
    <item>Pop 20kk</item>
    <item>Pop 21kk</item>
    <item>Pop 22kk</item>
</string-array>

<string-array name="r_arrayMoney">
    <item>Money 10kk</item>
    <item>Money 11kk</item>
    <item>Money 12kk</item>
    <item>Money 13kk</item>
    <item>Money 14kk</item>
    <item>Money 15kk</item>
    <item>Money 16kk</item>
    <item>Money 17kk</item>
    <item>Money 18kk</item>
    <item>Money 19kk</item>
    <item>Money 20kk</item>
    <item>Money 21kk</item>
    <item>Money 22kk</item>
</string-array>

MainActivity.class

public class MainActivity extends ActionBarActivity {
public final static String DADOS = "patrick.flags.dados";
ListView j_listview;
String[] j_paises;
String[] j_nota;
String[] j_money
int[] j_flags  =
    {
        R.drawable.flag_argentina,
                R.drawable.flag_bolivia,
                R.drawable.flag_brazil,
                R.drawable.flag_chile,
                R.drawable.flag_colombia,
                R.drawable.flag_equador,
                R.drawable.flag_france,
                R.drawable.flag_guyana,
                R.drawable.flag_paraguay,
                R.drawable.flag_peru,
                R.drawable.flag_suriname,
                R.drawable.flag_uruguay,
                R.drawable.flag_venezuela
    };
FlagAdapter j_adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    j_listview = (ListView)findViewById(R.id.l_lView);
    j_paises = getResources().getStringArray(R.array.r_arrayPais);
    j_nota = getResources().getStringArray(R.array.r_arrayNota);
    j_money = getResources().getStringArray(R.array.r_arrayMoney);
    j_adapter = new FlagAdapter(getApplicationContext(),R.layout.row);
    j_listview.setAdapter(j_adapter);
    j_listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
    int i = 0;
    for(String paises : j_paises)
    {
    FlagDataProvider dataProvider = new FlagDataProvider(j_flags[i], paises, j_nota[i]);
        j_adapter.add(dataProvider);
        i++;


    }

    j_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // When I click ,for example, on Argentina, it will put its 
            //name,note and money(which is not showing in the listview) in (different?) key(s) and 
            //send it to the new activity where I will get it back and display.



            Intent j_intent = new Intent (getApplicationContext(), ThirdActivity.class);
            j_intent.putExtra(DADOS,j_dados);
            startActivity(j_intent);


        }
    });
}
Patrick R.
  • 41
  • 3

2 Answers2

1

to access to a string value directly from the resources (i.e: string.xml) you can directly get it by calling its ID:

 String mess = getResources().getString(R.string.mess_1);

(Example obtained from this previous answered question) how to read value from string.xml in android?

If you're trying to get a specific value from a specific field inside your listview programatically with the index, something like this could help:

 String selectedFromList = (lv.getItemAtPosition(position)); 

(Example obtained from this previous answered question) How to extract the text from the selected item on the listView

If you're trying to do it directly via an event, for example OnClick() event:

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
 {
     String data=(String)arg0.getItemAtPosition(arg2);

 }}); 

(Example obtained from this previous answered question) How to retrieve the clicked string from a listview using OnItemClick?

In other words, there are many ways of obtaining the data, and a little bit of research can go a long way and understanding the methods are key to programming in Android.

Hope this helps!

Community
  • 1
  • 1
Viralwarrior012
  • 185
  • 1
  • 14
0

I did this and worked for me, maybe It is not the best way...

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String j_dados = j_nota[position];
            Intent j_intent = new Intent (getApplicationContext(), ThirdActivity.class);
            j_intent.putExtra(DADOS,j_dados);
            startActivity(j_intent);

        }
    });
Patrick R.
  • 41
  • 3
  • Looks good, like i said, there are plenty of ways to get the selected items value. If you want your post to return a value, use startActivityForResult(). – Viralwarrior012 Jul 03 '15 at 12:46