Simple Adapter Example?

Let’s Code!

This Example is Also used to click any specific row that data display in the  Second Activity.
We only need this files to run this sample code successfully.
activity_main.xml -- our main layout. Our trigger to show the ListView


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity" >

  6.     <ListView
  7.         android:id="@+id/listView"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content" >
  10.     </ListView>

  11. </RelativeLayout>
  12.  
MainActivity.java
  1. public class MainActivity extends Activity {
  2. ListView listview;

  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);

  6. String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
  7. "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
  8.    int[] images = new int[] {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
  9. R.drawable.image5, R.drawable.ic_launcher };
  10. listview = (ListView) findViewById(R.id.listView);

  11. // use your own layout
  12. MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, R.layout.rowlayout, values, images);
  13. listview.setAdapter(adapter);
  14. listview.setOnItemClickListener(new OnItemClickListener() {
  15. @Override
  16. public void onItemClick(AdapterView<?> arg0, View view, int id, long position) {
  17. Object  ob = (Object) view.getTag();  
  18. if(ob != null)  {
  19. TotalFavouriteInfo history_title = (TotalFavouriteInfo) view.getTag();  
  20. String title = history_title.getPage_name();
  21. Bitmap image = history_title.getPage_date();
  22.   Toast.makeText(MainActivity.this, "position ==> " +position + title + image , Toast.LENGTH_SHORT).show();
  23. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  24. intent.putExtra("position", title);
  25. intent.putExtra("mage_bit", image);
  26. startActivity(intent);
  27.   }
  28. }
  29. });
  30. }
  31. } 
MySimpleArrayAdapter.java
  1. package com.example.adapterexample;

  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;

  10. public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  11.   private final Context context;
  12.   private final String[] values;
  13.   private final int[] images;

  14.   public MySimpleArrayAdapter(Context context,int _names, String[] values, int[] images) {
  15.     super(context, R.layout.rowlayout, values);
  16.     this.context = context;
  17.     this.values = values;
  18.     this.images = images;
  19.   }

  20.   @Override
  21.   public View getView(int position, View convertView, ViewGroup parent) {  
  22.  if(convertView == null) {
  23.  LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  24.  convertView = inflater.inflate(R.layout.rowlayout, parent, false);    
  25.  }
  26.  
  27.  TextView textView = (TextView) convertView.findViewById(R.id.textView1);
  28.  ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
  29.  
  30.  textView.setText(values[position]);  
  31.  
  32.  String string = values[position];  
  33.  if(string.equals("WindowsMobile")) {
  34.  imageView.setImageResource(R.drawable.image2);
  35.  } else if (string.equals("Windows7")){
  36.  imageView.setImageResource(R.drawable.image3);
  37.  } else if (string.equals("Ubuntu")){
  38.  imageView.setImageResource(R.drawable.image1);
  39.  } else if (string.equals("WebOS")){
  40.  imageView.setImageResource(R.drawable.image4);
  41.  } else if (string.equals("Linux")){
  42.  imageView.setImageResource(R.drawable.image5);
  43.  } else {
  44.  imageView.setImageResource(R.drawable.ic_launcher);
  45.  } 
  46.  
  47.  TotalFavouriteInfo total_acc = new TotalFavouriteInfo();
  48.  total_acc.setPage_name(string);
  49.  
  50.  convertView.setTag(total_acc);   
  51.  return convertView;
  52.   }
  53.  
rowlayout.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="horizontal" >

  6.     <LinearLayout
  7.         android:layout_width="match_parent"
  8.         android:layout_height="50dp"
  9.         android:orientation="horizontal" >

  10.         <ImageView
  11.             android:id="@+id/imageView1"
  12.             android:layout_marginLeft="5dp"
  13.             android:layout_width="50dp"
  14.             android:layout_height="50dp"
  15.             android:src="@drawable/ic_launcher" />

  16.         <TextView
  17.             android:id="@+id/textView1"
  18.             android:layout_marginLeft="10dp"
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:layout_gravity="center"
  22.             android:text="TextView" />
  23.     </LinearLayout>

  24. </LinearLayout>
TotalFavouriteInfo.java
  1. public class TotalFavouriteInfo {
  2. private String page_name;
  3. private Bitmap page_date;

  4. public Bitmap getPage_date() {
  5. return page_date;
  6. }
  7. public void setPage_date(Bitmap bmap) {
  8. this.page_date = bmap;
  9. }
  10. public String getPage_name() {
  11. return page_name;
  12. }
  13. public void setPage_name(String acc_info) {
  14. this.page_name = acc_info;
  15. }
  16. }
  17.  
SecondActivity.java
  1. public class SecondActivity extends Activity {

  2. protected void onCreate(Bundle saveInstanteState) {
  3. super.onCreate(saveInstanteState);
  4. setContentView(R.layout.second);

  5. TextView button = (TextView) findViewById(R.id.textView1);
  6. ImageView image = (ImageView) findViewById(R.id.imageView1);
  7. Intent intent = getIntent();
  8. Bitmap bitmap = (Bitmap) intent.getParcelableExtra("mage_bit");
  9. String title = getIntent().getExtras().getString("position");
  10.   Toast.makeText(SecondActivity.this, title, Toast.LENGTH_SHORT).show();
  11. button.setText(title);
  12.   image.setImageBitmap(bitmap);
  13. }
  14. }
second.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:id="@+id/textView1"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="TextView" />

  11.     <ImageView
  12.         android:id="@+id/imageView1"
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:src="@drawable/ic_launcher" />

  16. </LinearLayout>

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...