Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Advanced Adapter in Android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 338
    Comment on it

    If you want to create Item view function check below example. In the below example I have created a Advanced class. By using Advanced adpater many opretions can be consumed. In Advanced adapter we can easily use view holder method. Adapter call the getView() method which return a view for each item that is within the adapter view. In the below code i have described how to make Advanced adapter.

    Step(1)- MainActivity-

    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"};
               AdvancedAdapter adapter = new AdvancedAdapter(this, str);
               setListAdapter(adapter);
    }
    
    
    }
    

    Step(2)- Create a AdvancedAdapter class-

    public class AdvancedAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] lang;
    
    
        public AdvancedAdapter(Context context, String lang[]) {
            super(context, R.layout.row, lang);
            this.context = context;
            this.lang = lang;
        }
    
        static class Holder {
            public TextView text;
            public ImageView img;
    
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row;
            if (convertView == null) {
                LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = li.inflate(R.layout.row, parent, false);
                Holder holder = new Holder();
                holder.text = (TextView) row.findViewById(R.id.label);
                holder.img = (ImageView) row.findViewById(R.id.icon);
                row.setTag(holder);
            } else
            {
                row = convertView;
            }
            Holder holder = (Holder) row.getTag();
            String str = lang[position];
            holder.text.setText(str);
    
            if (str.startsWith("C")) {
                holder.img.setImageResource(R.drawable.img1);
    
            } else if (str.startsWith("java")) {
                holder.img.setImageResource(R.drawable.img2);
            } else if (str.startsWith("android")) {
                holder.img.setImageResource(R.drawable.img2);
            } else
                holder.img.setImageResource(R.drawable.img1);
    
            return row;
    
        }
    }
    

    Step(3)-Create a new layout (row.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/img1" >
        </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>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: