Tuesday, September 1, 2015

MyBuddies02 ListView SetAdapter


.
MyBuddies02 ListView SetAdapter
Development Environment:
Android Studio 1.5.1
Build #AI-141.2456560, built on December 1,2015
JRE: 1.7.0_25-b17 amd64

This project replaces the XML setting for List Entries and uses the ArrayAdapter to load Entries to List.
The adapter uses String Array data type for its sources.

0) Starting Up

1) Edit Layout (activity_main.xml)

Delete the android:entries statement .
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.notarazi.mybuddies01.MainActivity">
   <Button
       android:id="@+id/btn_add_buddy"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Add Buddy"/>
   <ListView
       android:id="@+id/lvw_buddies"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
      android:entries="@array/buddies_array"
        />
</LinearLayout>

2) Edit Controller (MainActivity.java)

Add Adapter.
File: MainActivity.java
package com.notarazi.mybuddies01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   ListView lvwBuddies;
   Button btnAddBuddy;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       setLvwBuddies();
       setBtnAddBuddy();
   }
   private void setLvwBuddies() {
       lvwBuddies = (ListView) findViewById(R.id.lvw_buddies);
       // get String Array from Array Resources
       String[] strBuddies = getResources().getStringArray(R.array.buddies_array);
       //Create Adapter containing strBuddies array
       final ArrayAdapter adpBuddies = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strBuddies);
       //Plug adpBuddies to lvwBuddies
       lvwBuddies.setAdapter(adpBuddies);
       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 setBtnAddBuddy() {
       btnAddBuddy=(Button) findViewById(R.id.btn_add_buddy);
       btnAddBuddy.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               addBuddy();
           }
       });
   }
   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();
   }
}

3) Alternative Approach


Delete String Resources and Write String Values in Controller
package com.notarazi.mybuddies01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   ListView lvwBuddies;
   Button btnAddBuddy;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       setLvwBuddies();
       setBtnAddBuddy();
   }
   private void setLvwBuddies() {
       lvwBuddies = (ListView) findViewById(R.id.lvw_buddies);
       // create String Array
       String[] strBuddies ={"Superman","Batman","Spiderman","Ultraman","Ironman"};
       //Create Adapter containing strBuddies array
       final ArrayAdapter adpBuddies = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strBuddies);
       //Plug adpBuddies to lvwBuddies
       lvwBuddies.setAdapter(adpBuddies);
       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 setBtnAddBuddy() {
       btnAddBuddy=(Button) findViewById(R.id.btn_add_buddy);
       btnAddBuddy.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               addBuddy();
           }
       });
   }
   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();
   }
}

DOWNLOAD


REFERENCES


.

No comments:

Post a Comment