Skip to main content

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
       
    }
   
}

Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout 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:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout 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:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …