Search code examples
androidlistcustom-lists

need tutorials on Android Custom-List


last days I was browsing one of the apps in Android and I found out an amazing list.

Custom-List

I Found that Custom ListView Interesting. When we click on the Text it shows me some Activity and when we click on the Arrow on Right it shows me a dialog.

I want to learn this. Can anybody go through some Tutorials where this Custom List is explained. Please Friends. guide me.. Thanks alot


Solution

  • Hi Below Is Custom ListView Example

    First Create test.Java File below is code

    package com.test;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;
    
    public class test extends Activity {
        /** Called when the activity is first created. */
        ArrayList<String> arrayString = new ArrayList<String>();
        test_adapter adapter;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            arrayString.add("TextView1");
            arrayString.add("TextView2");
            arrayString.add("TextView3");
            arrayString.add("TextView4");
            arrayString.add("TextView5");
            ListView list = (ListView) findViewById(R.id.LIST);
            adapter = new test_adapter(this, arrayString);
            list.setAdapter(adapter);
        }
    
    }
    

    Also Used below Class file test_adapter.java

    package com.test;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class test_adapter extends BaseAdapter {
        private Activity activity;
        ArrayList<String> data = new ArrayList<String>();
        private static LayoutInflater inflater = null;
    
        public test_adapter(Activity a, ArrayList<String> d) {
            activity = a;
            data = d;
            inflater = LayoutInflater.from(activity);
        }
    
        public int getCount() {
            return data.size();
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public static class ViewHolder {
            public TextView txt1;
            public Button btn1;
            public RelativeLayout rel1;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            final ViewHolder holder;
            if (convertView == null) {
                vi = inflater.inflate(R.layout.listview, null);
                holder = new ViewHolder();
                holder.txt1 = (TextView) vi.findViewById(R.id.txt1);
                holder.btn1 = (Button) vi.findViewById(R.id.btn1);
                holder.rel1 = (RelativeLayout) vi.findViewById(R.id.rel1);
                vi.setTag(holder);
            } else
                holder = (ViewHolder) vi.getTag();
    
            holder.txt1.setText(data.get(position));
            holder.rel1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast
                            .makeText(activity, "Click On TextView",
                                    Toast.LENGTH_LONG).show();
                }
            });
            holder.btn1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(activity, "Click On Button", Toast.LENGTH_LONG)
                            .show();
                }
            });
            return vi;
        }
    
    }
    

    Used below layout files main.xml

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
            <ListView android:layout_width="fill_parent" android:id="@+id/LIST"
        android:layout_height="fill_parent" />
        </LinearLayout>
    

    Used listview.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal">
        <RelativeLayout android:id="@+id/rel1" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/txt1"
            android:text="Test Description"></TextView>
    </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
            <Button android:id="@+id/btn1" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="ClickHere"
            android:layout_alignParentRight="true"></Button>
                </RelativeLayout>
        </LinearLayout>
    

    Used Above Code and you will get display toast message and you can changed as per you requirement.