0

Alright , heres the code

Heres Java file

public class MainActivity extends Activity implements OnClickListener {
private String array1;
private String [] arraymain;    
TextView tv1,tv2;
Button btn; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        //getFragmentManager().beginTransaction()
            //  .add(R.id.container, new PlaceholderFragment()).commit();
    }       
    tv1=(TextView)findViewById(R.id.maintv1);
    tv2=(TextView)findViewById(R.id.maintv2);       
    btn=(Button)findViewById(R.id.mainbutton1);
    btn.setOnClickListener(this);       
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

and heres the activity_main.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


<TextView 
    android:id="@+id/maintv1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:text="Text here"
    android:textSize="24dp"
    />

<TextView 
    android:id="@+id/maintv2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:text="Text here"
    android:textSize="24dp"
    />

<Button
    android:id="@+id/mainbutton1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="Click here" 
    />

   </LinearLayout>

heres string.xml . the array contains key value pairs.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">KeyValuePairDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

    <string-array name="array1" >
        <item name="a1">Hello</item>
        <item name="a2">Hola</item>
        <item name="a2">Helios</item>
    </string-array>

</resources>

and heres the image . Key will be in Text1 , Value will be in image Text2

Now, what I need to do is, When I click on the Button, It will cycle through array key-value pairs defined in strings.xml and will display key in textview1 and corresponding value in textview2.

If you know how to do it, please paste code here. Or may be give me instructions.

Thanks in advance

1

1 Answer 1

1

This is how you get the value from the defined in xml:

String[] arrayXml= yourContext.getResources().getStringArray(R.array.my_array);
textView.setText(arrayXml[0]);//setText
Sign up to request clarification or add additional context in comments.

2 Comments

how do I extract the "name field from item array defined in string into the Java file ?
@Ajinkya : The 'key' of the string array is effectively just <item>. If you want a key/value pair you'll need to concatenate them perhaps using CSV format - example <item>Name,Squonk<\item>. You can then use the string split() method to separate the key and the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.