PowenKo > Android > UI > ListView

March 26, 2012   ·   0 Comments

Screen Shot 2012-03-26 at 1.27.28 AM

package com.powenko;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Tutorial_Listview_CustomListViewItemsActivity 
 extends Activity{
 
	private ArrayList<Order>  m_orders;
	private OrderAdapter m_adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getOrders();
        
      
        ListView listView = (ListView) findViewById(R.id.listView1);
        
        
     //   m_orders = new ArrayList<Order>();
        m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
      //  listView.setAdapter(this.m_adapter);
     // Assign adapter to ListView
        listView.setAdapter(m_adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view,
		int position, long id) {
		Toast.makeText(getApplicationContext(),
			"Click ListItem Number " + position, Toast.LENGTH_LONG)
			.show();
		}
		});
 
    }
    
    
    private void getOrders(){
      
            m_orders = new ArrayList<Order>();
            Order o1 = new Order();
            o1.Name="Apple";
            o1.Status="Buy";
            Order o2 = new Order();
            o2.Name="Banana";
            o2.Status="Completed";
            m_orders.add(o1);
            m_orders.add(o2);
           
      }
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    //super.onListItemClick(l, v, position, id);
    String selection = l.getItemAtPosition(position).toString();
    Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
    }
    
    
    private class OrderAdapter extends ArrayAdapter<Order> {
    	 
        private ArrayList<Order> items;
 
        public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);
                }
                Order o = items.get(position);
                if (o != null) {
                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                        if (tt != null) {
                              tt.setText("Name: "+o.Name);                            }
                        if(bt != null){
                              bt.setText("Status: "+ o.Status);
                        }
                }
                return v;
        }
}
    
    
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/ic_launcher" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        />
    </LinearLayout>
</LinearLayout>

package com.powenko;

public class Order {
	public String Status="";
	public String Name="";
	

}

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon