Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hi Below Is Custom ListView Example </p> <h2>First Create test.Java File below is code</h2> <pre><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&lt;String&gt; arrayString = new ArrayList&lt;String&gt;(); 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); } } </code></pre> <hr> <h2>Also Used below Class file test_adapter.java</h2> <pre><code>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&lt;String&gt; data = new ArrayList&lt;String&gt;(); private static LayoutInflater inflater = null; public test_adapter(Activity a, ArrayList&lt;String&gt; 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; } } </code></pre> <hr> <h2>Used below layout files main.xml</h2> <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:layout_width="fill_parent" android:id="@+id/LIST" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <hr> <h2>Used listview.xml file</h2> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;RelativeLayout android:id="@+id/rel1" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt1" android:text="Test Description"&gt;&lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; &lt;Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ClickHere" android:layout_alignParentRight="true"&gt;&lt;/Button&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre> <hr> <p>Used Above Code and you will get display toast message and you can changed as per you requirement.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload