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
- <RelativeLayout 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"
- tools:context=".MainActivity" >
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </RelativeLayout>
MainActivity.java
- public class MainActivity extends Activity {
- ListView listview;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
- "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
- int[] images = new int[] {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
- R.drawable.image5, R.drawable.ic_launcher };
- listview = (ListView) findViewById(R.id.listView);
- // use your own layout
- MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, R.layout.rowlayout, values, images);
- listview.setAdapter(adapter);
- listview.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View view, int id, long position) {
- Object ob = (Object) view.getTag();
- if(ob != null) {
- TotalFavouriteInfo history_title = (TotalFavouriteInfo) view.getTag();
- String title = history_title.getPage_name();
- Bitmap image = history_title.getPage_date();
- Toast.makeText(MainActivity.this, "position ==> " +position + title + image , Toast.LENGTH_SHORT).show();
- Intent intent = new Intent(MainActivity.this, SecondActivity.class);
- intent.putExtra("position", title);
- intent.putExtra("mage_bit", image);
- startActivity(intent);
- }
- }
- });
- }
- }
MySimpleArrayAdapter.java
- package com.example.adapterexample;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class MySimpleArrayAdapter extends ArrayAdapter<String> {
- private final Context context;
- private final String[] values;
- private final int[] images;
- public MySimpleArrayAdapter(Context context,int _names, String[] values, int[] images) {
- super(context, R.layout.rowlayout, values);
- this.context = context;
- this.values = values;
- this.images = images;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if(convertView == null) {
- LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = inflater.inflate(R.layout.rowlayout, parent, false);
- }
- TextView textView = (TextView) convertView.findViewById(R.id.textView1);
- ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
- textView.setText(values[position]);
- String string = values[position];
- if(string.equals("WindowsMobile")) {
- imageView.setImageResource(R.drawable.image2);
- } else if (string.equals("Windows7")){
- imageView.setImageResource(R.drawable.image3);
- } else if (string.equals("Ubuntu")){
- imageView.setImageResource(R.drawable.image1);
- } else if (string.equals("WebOS")){
- imageView.setImageResource(R.drawable.image4);
- } else if (string.equals("Linux")){
- imageView.setImageResource(R.drawable.image5);
- } else {
- imageView.setImageResource(R.drawable.ic_launcher);
- }
- TotalFavouriteInfo total_acc = new TotalFavouriteInfo();
- total_acc.setPage_name(string);
- convertView.setTag(total_acc);
- return convertView;
- }
- }
rowlayout.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="horizontal" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/imageView1"
- android:layout_marginLeft="5dp"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/ic_launcher" />
- <TextView
- android:id="@+id/textView1"
- android:layout_marginLeft="10dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="TextView" />
- </LinearLayout>
- </LinearLayout>
TotalFavouriteInfo.java
- public class TotalFavouriteInfo {
- private String page_name;
- private Bitmap page_date;
- public Bitmap getPage_date() {
- return page_date;
- }
- public void setPage_date(Bitmap bmap) {
- this.page_date = bmap;
- }
- public String getPage_name() {
- return page_name;
- }
- public void setPage_name(String acc_info) {
- this.page_name = acc_info;
- }
- }
SecondActivity.java
- public class SecondActivity extends Activity {
- protected void onCreate(Bundle saveInstanteState) {
- super.onCreate(saveInstanteState);
- setContentView(R.layout.second);
- TextView button = (TextView) findViewById(R.id.textView1);
- ImageView image = (ImageView) findViewById(R.id.imageView1);
- Intent intent = getIntent();
- Bitmap bitmap = (Bitmap) intent.getParcelableExtra("mage_bit");
- String title = getIntent().getExtras().getString("position");
- Toast.makeText(SecondActivity.this, title, Toast.LENGTH_SHORT).show();
- button.setText(title);
- image.setImageBitmap(bitmap);
- }
- }
second.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" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher" />
- </LinearLayout>
No comments:
Post a Comment