Friday 12 July 2013

Android Custome Listview with images

in this tutorial you just need to create a project and copy all these codes and place them where they are supposed to be and you will have no trouble  the main and custome put them in your layout folder and enjoy
thanx for watching my example
MAIN JAVA CLASS
ListView2Activity .java
package com.listview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListView2Activity extends Activity implements OnItemClickListener
{
    /** Called when the activity is first created. */

ListView lview;
ListViewAdapter lviewAdapter;


private final static String month[] = {"January","February","March","April","May",
"June","July","August","September","October","November","December"};

private final static String number[] = {"Month - 1", "Month - 2","Month - 3",
"Month - 4","Month - 5","Month - 6",
"Month - 7","Month - 8","Month - 9",
"Month - 10","Month - 11","Month - 12"};
private final static String sayings[]={"Hunger","Rain","Love","Sex","Summer","poverty","Autum","what","yelling","News","Terror","Snow"};

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

        lview = (ListView) findViewById(R.id.listView2);
        lviewAdapter = new ListViewAdapter(this, month, number);

        System.out.println("adapter => "+lviewAdapter.getCount());

        lview.setAdapter(lviewAdapter);

        lview.setOnItemClickListener(this);
    }

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), month[position]+"\t is the month of \t"+sayings[position], Toast.LENGTH_LONG).show();
//Toast.makeText(this,"Title => "+month[position]+"=> n Description"+number[position], Toast.LENGTH_SHORT).show();
}
}

here is the list view adapter that extends the Baseadapter class to handle the images
package com.listview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter
{
Activity context;
String title[];
String description[];

public ListViewAdapter(Activity context, String[] title, String[] description) {
super();
this.context = context;
this.title = title;
this.description = description;
}

public int getCount() {
// TODO Auto-generated method stub
return title.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

private class ViewHolder {
        TextView txtViewTitle;
        TextView txtViewDescription;
        ImageView image;
        TextView quotes;
}

public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater =  context.getLayoutInflater();

if (convertView == null)
{
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2); 
holder.image=(ImageView)convertView.findViewById(R.id.imageView1);
holder.quotes=(TextView)convertView.findViewById(R.id.quotes);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}

holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.image.setBackgroundResource(images[position]);
holder.quotes.setText(sayings[position]);
//holder.image.setImageResource(R.drawable.ic_launcher);

return convertView;
}
private final static String sayings[]={"Hunger","Rain","Love","Sex","Summer","poverty","Autum","what","yelling","News","Terror","Snow"};
private Integer[] images={R.raw.brightday,R.drawable.ic_launcher,R.raw.sunlaugh,R.raw.stormcloud,R.raw.hotsun,R.raw.brightday,R.raw.eveningcloud,R.raw.cloudlove,R.raw.springs,R.raw.stormcloud,R.raw.xmas,R.raw.cloudwhite};

}
main.xml
<?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:layout_height="wrap_content"
    android:id="@+id/listView2"
    android:layout_width="match_parent">
    </ListView>
</LinearLayout>

then the custom xml 

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge" >

    </TextView>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" >

    </TextView>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/quotes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:layout_marginLeft="200dp"
        android:layout_marginBottom="100dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

7 comments:

  1. what is that listitem_row why you use and where you used......,
    please can you explain it.....because I'm very new to android

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

  3. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

    angularjs Training in online

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    ReplyDelete
  4. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries. I want to say thanks for great sharing.
    Python training in marathahalli | Python training institute in pune

    ReplyDelete
  5. Well researched article and I appreciate this. The blog is subscribed and will see new topics soon.

    Java training in Chennai | Java training in Bangalore

    Java online training | Java training in Pune

    ReplyDelete
  6. You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.
    Data Science training in kalyan nagar | Data Science training in OMR

    Data Science training in chennai | Data science training in velachery

    Data science training in tambaram | Data science training in jaya nagar

    ReplyDelete