November 07, 2013

Android Custom Listview.



main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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


Home.java

public class Home extends Activity{

private ListView HomeItemListView;
private  ListAdapter adapter;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      HomeItemListView = (ListView) findViewById(R.id.Listview);
      adapter = new ListAdapter(this);
       
        HomeItemListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

                ListItem listItem = (ListItem) adapter.getItem(position);
                System.out.println("Selected Item : " +  listItem.getItemID());
              
            }
        });

}

}

ListAdapter.java

import java.util.ArrayList;

import com.strat.stratpromo.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ListAdapter extends ArrayAdapter<ListItem> implements OnClickListener {
    public Context context;
    public ArrayList<HomeItem> HomeItemList;
    public ImageLoader imageLoader;
   
    public HomeListAdapter(Context context, int resource, ArrayList<HomeItem> HomeItemLst) {
        super(context, resource, HomeItemLst);
        this.context = context;
        this.HomeItemList = HomeItemLst;
        imageLoader = new ImageLoader(context);
    }
   
    @Override
    public int getCount() {
        if (HomeItemList != null)
            return HomeItemList.size();

        return 0;
    }

    @Override
    public HomeItem getItem(int position) {
        return HomeItemList.get(position);
    }
   
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View myConvertView = null;
        try {
            final HomeItem HomeItem = HomeItemList.get(position);
            myConvertView = convertView;
            if (myConvertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                myConvertView = inflater.inflate(R.layout.home_list_item_view, null);
            }
            ImageView CatImage = (ImageView) myConvertView.findViewById(R.id.list_image);
            TextView CatHeader = (TextView) myConvertView.findViewById(R.id.title);
            TextView CatDiscount = (TextView) myConvertView.findViewById(R.id.discount);
            TextView CatDescription = (TextView) myConvertView.findViewById(R.id.description);
           
            CatHeader.setText(HomeItem.getTitle());
            CatDiscount.setText(HomeItem.getDiscount());
            CatDescription.setText(HomeItem.getSubTitle());
           
            imageLoader.DisplayImage(HomeItem.getImageUrl(), CatImage);
       
            //    Bitmap bm = Helper.getBitmap(HomeItem.getImageUrl());
            /*String filePath = Helper.saveImageInExternalCacheDir(context, bm,
                        HomeItem.getTitle()+HomeItem.getPromotionID());*/
           
            //CatImage.setImageBitmap(bm);   
           
        } catch (Exception e) {
             e.printStackTrace();
        }
        return myConvertView;
    }
   
    @Override
    public long getItemId(int position) {
        return position;
    }

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

No comments:

Post a Comment