Tuesday, January 20, 2015

MyBuddies102: ListView ArrayAdapter and OnClick Listener


.
MyBuddies102: ListView ArrayAdapter and OnClick Listener
ListView must implement ArrayAdapter to get data from sources and convert them into view that are placed in a list.

0) Starting Up

1) Delete String Resources

File: res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string name="app_name">MyBuddies</string>  
         <string-array name="buddies_array">
        <item>Superman</item>
        <item>Spiderman</item>
        <item>Batman</item>
        <item>Ironman</item>
        <item>Ultraman</item>
        <item>Pacman</item>
        <item>Roboman</item>
    </string-array>
</resources>  
2) Add String Resources to Controller File (MyBuddies.java)
File: MyBuddies.java
package com.example.MyBuddies;      
       
import android.app.Activity;      
import android.os.Bundle;      
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
       
public class MyBuddies extends Activity      
{      
    Button btnAddBuddy;
    ListView lvwBuddies;
    String[] strBuddies;
    ArrayAdapter adpBuddies;
   
    /** Called when the activity is fit created. */      
    @Override      
    public void onCreate(Bundle savedInstanceState)      
    {      
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.main);
        setBtnAddBuddy();
        setLvwBuddies();
    }  
     
    private void setBtnAddBuddy(){
        btnAddBuddy=(Button) findViewById(R.id.btn_addbuddy);
       
        //set onlicklistener for button
        btnAddBuddy.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                addBuddy();
            }
        });
       
    }
    private void setLvwBuddies(){
        lvwBuddies = (ListView) findViewById(R.id.lvw_buddies);
       
        //Add new values to array
        strBuddies =new String[] {"Superman","Spiderman","Batman","Ironman","Ultraman", "Pacman", "Roboman"};
       
        //Create ArrayList from strBuddies
        List<String> values = new ArrayList<String>(Arrays.asList(strBuddies));
       
        //Create Adapter to load ArrayList
        adpBuddies = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
       
        //Plug adpBuddies to lvwBuddies
        lvwBuddies.setAdapter(adpBuddies);
       
        //set onclick listener for listview
        lvwBuddies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                editBuddy(position, ((TextView) view).getText().toString());
            }
        });
    }
    private void addBuddy(){
        Toast.makeText(getApplicationContext(),"Add Buddy.",
                Toast.LENGTH_SHORT).show();        
    }
    private void editBuddy(int position, String text){
        Toast.makeText(getApplicationContext(),"Edit Buddy.\nPosition:"+position+"\nText:"+text+"\n",
                Toast.LENGTH_SHORT).show();        
    }
}        
OUTCOME.

DOWNLOAD


.

No comments:

Post a Comment