Loading ...

Bind data from a listview from a String array | CodeAsp.Net

Bind data from a listview from a String array

 /5
0 (0votes)

We will read the data to bind to a listview from a string array defined in strings.xml file and then perform some function to another string array based on the value selected from the listview.

Steps:

1. Create an android studio project and name it as "ListView_String", and set the company domain as "codeasp.listview_string.com". Click next.

2. Now select the minimum SDK as 18(Jelly Bean 4.3) and leave all other fields as they are and click next.

3. Select the activity as Blank Activity and click next.

4. Leave all the fields as default and click next.

5. Now open your "acitivity_main.xml" file from the layout file. Here we will have to create a basic layout that we will use to read and write to the file.

6. In the "activity_main.xml" file add the following code, to create a list view. That will be used to display the values.

<ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:entries="@array/capitals_list" />

After adding the listview your file will looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:entries="@array/capitals_list" />
</RelativeLayout>

Note: Here we have added all the values in the string array "capitals_list" to the ListView by adding it as entries of the "ListView". 

7. Now open your strings.xml file and add the create two string arrays, e.g.:

<string-array name="capitals_list">
        <item>Delhi</item>
        <item>Islamabad</item>
        <item>Algiers</item>
        <item>Luanda</item>
        <item>Gaborone</item>
        <item>Ouagadougou</item>
        <item>Bujumbura</item>
        <item>Yaoundé</item>
        <item>Praia</item>
        <item>Bangui</item>
    </string-array>
    <string-array name="countires_list">
        <item>India</item>
        <item>Pakistan</item>
        <item>Algeria</item>
        <item>Angola</item>
        <item>Benin</item>
        <item>Botswana</item>
        <item>Burkina Faso</item>
        <item>Burundi</item>
        <item>Cameroon</item>
        <item>Cape Verde</item>
    </string-array>

Now after adding the arrays your file will be like this:

<resources>
    <string name="app_name">ListView_String</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string-array name="capitals_list">
        <item>Delhi</item>
        <item>Islamabad</item>
        <item>Algiers</item>
        <item>Luanda</item>
        <item>Gaborone</item>
        <item>Ouagadougou</item>
        <item>Bujumbura</item>
        <item>Yaoundé</item>
        <item>Praia</item>
        <item>Bangui</item>
    </string-array>
    <string-array name="countires_list">
        <item>India</item>
        <item>Pakistan</item>
        <item>Algeria</item>
        <item>Angola</item>
        <item>Benin</item>
        <item>Botswana</item>
        <item>Burkina Faso</item>
        <item>Burundi</item>
        <item>Cameroon</item>
        <item>Cape Verde</item>
    </string-array>
</resources>

8. Now open your java file and now we have to bind the listview from the xml file, for that first declare the ListView above the "onCreate" method and below the Class name.

ListView listView;

Now under your "onCreate" method bind it to the listview in the "activity_main.xml" file, by adding the following code:

listView = (ListView) findViewById(R.id.listView);

Next, we are going to display value from another array based on the value selected from the ListView. For this we will have to implement a listener that will listen to all the clicks made on the "ListView". We will use the concept of interfaces of java and implement:

AdapterView.OnItemClickListener

This will handle all the clicks and will react accordingly to the user clicks, and be responsible for giving the outputs to the user based on the click.

9. Before implementing the "OnItemClickListener" we will first create a string array that will hold all the values of the array declared in the "strings.xml" i.e. "countries_list", for that we will first declare the array and then we will bind it to the strings file data in the method "onCreate". The code for this is shown below:

To declare the strings array:

String[] countries_IDs;

To bind to the array:

countires_IDs = getResources().getStringArray(R.array.countires_list);

10. Now we will implement the OnItemClickListener by adding the code:

@Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
    }

11. In the above method we will display the countries name based on the capitals name clicked, here we will display a toast which will have the countries names, we will update the method like this:

@Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), countires_IDs[position], Toast.LENGTH_LONG).show();
    }

12. Finally we implement the ItemClickListener with the listView by adding the code:

listView.setOnItemClickListener(this);

13. Now your java file will look like this:

package com.listview_string.codeasp.listview_string;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    ListView listView;
    String[] countires_IDs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        countires_IDs = getResources().getStringArray(R.array.countires_list);
    }

    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), countires_IDs[position], Toast.LENGTH_LONG).show();
    }
}

14. Run your application on your device or emulator.

Application Screen Shots:

     

Comments (1)

   
iam22338
wow exactly what i need. but i want open the 'countires_IDs' in another activity. how to open as another activity. please help me. thank you.
5/15/2016
 · 
by
   
Ravi13
Hi, Ramesh Thank you for your feedback. I have updated the code i.e. : Add a new XML file and name it as: country_details.xml Code for file: Now Create a class and name it as: CountryNameDisplay Code for file: package com.listview_string.codeasp.listview_string; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class CountryNameDisplay extends AppCompatActivity{ TextView countryNameDisplay; String country_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle extras = getIntent().getExtras(); country_name = extras.getString("country_name"); setContentView(R.layout.country_details); countryNameDisplay = (TextView) findViewById(R.id.country_name_selected); countryNameDisplay.setText(country_name); } } Now in class: MainActivity update your method: "onItemClick" to: @Override public void onItemClick(AdapterView adapter, View view, int position, long id) { Toast.makeText(getApplicationContext(), countires_IDs[position], Toast.LENGTH_LONG).show(); Intent intent = new Intent(this, CountryNameDisplay.class); intent.putExtra("country_name", countires_IDs[position]); startActivity(intent); } Also don't forget to update your manifest file add your new class, to do that, add code: This will do the job. Happy to help.
5/15/2016
 · 
by

Top Posts