Saturday, 29 June 2013

Change Images in Base Adapter

Demo:

1)MainActivity.java:

package com.venky.imageupdatinginbaseadapter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

      int i = 0;
      ImageView ivImage = null;
      TextView tvText = null;
      private Handler handler = new Handler();

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView lvList = (ListView) findViewById(R.id.lvList);
            MyAdapter adapter = new MyAdapter();
            lvList.setAdapter(adapter);

      }

      class MyAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                  // TODO Auto-generated method stub
                  return 3;
            }

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

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

            @Override
            public View getView(int pos, View v, ViewGroup arg2) {

                  if (pos == 0) {
                        v = getLayoutInflater().inflate(R.layout.style1, null);

                        TextView tvText1 = (TextView) v.findViewById(R.id.tvText1);
                        TextView tvText2 = (TextView) v.findViewById(R.id.tvText2);

                        tvText1.setText(names[pos]);
                        tvText2.setText(names[pos]);
                  } else if (pos == 1) {
                        v = getLayoutInflater().inflate(R.layout.style2, null);

                        ivImage = (ImageView) v.findViewById(R.id.ivImage);
                        tvText = (TextView) v.findViewById(R.id.tvText);

                        handler.postDelayed(runnable, 40);

                  } else if (pos == 2) {
                        v = getLayoutInflater().inflate(R.layout.style3, null);

                        TextView tvText = (TextView) v.findViewById(R.id.tvText3);
                        tvText.setText(names[pos]);
                  }

                  return v;
            }
      }

      int[] img = { R.drawable.rose, R.drawable.jasmine, R.drawable.mango,
                  R.drawable.graps, R.drawable.pineapple, R.drawable.orange };
      String[] names = { "ROSE", "JASMINE", "MANGO", "GRAPS", "PINEAPPLE",
                  "ORANGE" };

      Runnable runnable = new Runnable() {

            @Override
            public void run() {

                  if (i == 0) {
                        ivImage.setImageResource(img[i]);
                        tvText.setText(names[i]);
                  } else if (i == 1) {
                        ivImage.setImageResource(img[i]);
                        tvText.setText(names[i]);
                  } else if (i == 2) {
                        ivImage.setImageResource(img[i]);
                        tvText.setText(names[i]);
                  } else if (i == 3) {
                        ivImage.setImageResource(img[i]);
                        tvText.setText(names[i]);
                  } else if (i == 4) {
                        ivImage.setImageResource(img[i]);
                        tvText.setText(names[i]);
                  } else if (i == 5) {
                        ivImage.setImageResource(img[i]);
                        tvText.setText(names[i]);
                  }

                  i++;

                  if (i == 6) {
                        i = 0;
                  }

                  handler.postDelayed(this, 3000);
            }

      };
}


2)activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>


3)style1.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="110dp" >

       <TextView
            android:id="@+id/tvText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="Medium Text"
            android:gravity="center"
            android:textSize="20dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Medium Text"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>


4)style2.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="110dp" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="Medium Text"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>


5)style3.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="110dp" >

        <TextView
            android:id="@+id/tvText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Medium Text"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

6)Download this code Click Here

No comments:

Post a Comment