Blog purpose for android basic example for android app developer. any query please my contact

Monday 27 February 2023

Android Program to Demonstrate an Adapter

 Android Program to Demonstrate an Adapter


This Android Program lets you create an Adapter in an Application using Java.

Here is source code of the Program to create an Adapter in an Application. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.


1.MainActivity.java:


import android.os.Bundle;

import android.app.Activity;

import android.app.ListActivity;

import android.view.Menu;

 

public class MainActivity extends ListActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        String str[] = new String[] { "C", "C++", "Java", "Andorid" };

        MyAdapter adapter = new MyAdapter(this, str);

        setListAdapter(adapter);

    }

 

}


2.MyAdapter.java:


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 MyAdapter extends ArrayAdapter<String> {

 

    private final Context context;

    private final String[] var;

 

    public MyAdapter(Context context, String[] var) {

        super(context, R.layout.rowlayout, var);

        // TODO Auto-generated constructor stub

        this.context = context;

        this.var = var;

    }

 

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        // TODO Auto-generated method stub

        LayoutInflater li = (LayoutInflater) context

                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = li.inflate(R.layout.rowlayout, parent, false);

        TextView text = (TextView) row.findViewById(R.id.label);

        ImageView img = (ImageView) row.findViewById(R.id.icon);

        text.setText(var[position]);

        String s = var[position];

        if (s.startsWith("C")) {

 

            img.setImageResource(R.drawable.c_pic);

        }

        else if(s.startsWith("Java")){

            img.setImageResource(R.drawable.java);

        }

        else if(s.startsWith("Android")){

            img.setImageResource(R.drawable.ic_launcher);

        }

        else{

            //c++

            img.setImageResource(R.drawable.c_prog);

        }

            return row;

    }

 

}


3.RowLayout.xml:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" >

 

    <ImageView

        android:id="@+id/icon"

        android:layout_width="100px"

        android:layout_height="100px"

        android:layout_marginLeft="4px"

        android:layout_marginRight="10px"

        android:layout_marginTop="4px"

        android:src="@drawable/ic_launcher" >

    </ImageView>

 

    <TextView

        android:id="@+id/label"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@+id/label"

        android:textSize="80px" >

    </TextView>

 

</LinearLayout>


No comments:

Post a Comment