38879

Single choice ListView custom Row Layout

Question:

I just want to make a Single Choice ListView with two TextViews (side by side in a row).

Actually I want it to let a user select a Product according to its Size-price values. These values are shown in that ListView with those two TextViews representing Size-Price values.

The problem is that I am not able to make it a single choice list (also showing a radio Button in front of it). Below is the row layout I am using for it:

<?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="fill_parent" android:orientation="horizontal" android:id="@+id/rowLayout" > <TextView android:id="@+id/tv_size" android:layout_width="180dp" android:layout_height="wrap_content" android:text="Size" android:textSize="15sp" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Price" android:textSize="15sp" /> <RadioButton android:id="@+id/radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" /> </LinearLayout>

Please tell how to code with Java for that. If it is possible with simple_list_item_2 layout then how ?

Answer1:

I'm not sure tat we can create radio group in ListView, what i'm doing s simply handle the selections in Java. Example:

@Override public View getView(int position, View view, ViewGroup viewGroup) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = mInflater.inflate(R.layout.customer_invoice_row, null); holder.selectionRB = (RadioButton) view .findViewById(R.id.selectionRB); holder.sizeTV = (TextView) view .findViewById(R.id.sizeTV); holder.priceTV = (TextView) view .findViewById(R.id.priceTV); holder.selectionRB.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { RadioButton rb = (RadioButton) v; if (mSelectedRB != null) { if (!rb.getTag().equals(mSelectedRB.getTag())) mSelectedRB.setChecked(false); } mSelectedRB = rb; } }); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.position = position; holder.selectionRB.setTag(productObj); holder.sizeTV.setText(productObj.getSize()); holder.priceTV.setText(productObj.getPrice()); return view; }

Holder: [it can be a inner class]

class ViewHolder { int position; RadioButton selectionRB; TextView sizeTV; TextView priceTV; }

and mSelectedRB is a global member to your activity.

Answer2:

try to Declare a RadioGroup object in your adapter then in your getView() or bindView() add the RadioButtons to the RadioGroup using RadioGroup.addView()

Recommend

  • Matching consecutive digits REGEXP_REPLACE in Redshift
  • separate day month and year from a date batch file
  • Primefaces datatable filtering not working [duplicate]
  • Converting LLA to XYZ
  • Are Laravel's Polymorphic Relations also suitable for true polymorphism?
  • Training in batches but testing individual data item in Tensorflow?
  • How to trigger a “ng build” from within a dotnetcore web-api project with msbuild but keep the angul
  • adding seconds to a datetime variable in node.js
  • Check for existence of multiple fields in MongoDB document
  • Check if row was changed in a TClientDataset
  • How to keep a date current in Markdown files
  • Is there a way to get the nlargest items per group in dask?
  • TypeError: $(…).val(…) is undefined in jquery [closed]
  • Uncategorized exception for using correct credentials in LDAP authentication
  • AJAX POST and PHP
  • How to change servlet request body in java filter?
  • position absolute gives parent no height
  • Mongoose: find mixed schema type documents with multiple entries
  • Implement clientside token-based authentication using plain Javascript/AJAX
  • Get URL from Opera browser
  • {“code”:101,“error”:“object not found for update”} Parse.com Javascript SDK
  • DoEvents is used yet application is not responsive
  • Hide previous tabs and header react-navigation v3
  • How to reload “index.html” in angularJS without manual intervention?
  • Proguard returned with error code 1, error can't find referenced class android.support.v4.app
  • how to save settings of eclipse?
  • How can I make masonry and Imagesloaded work correct. (wordpress)
  • How to change type datatable column [duplicate]
  • Postgres JSONB: query values in JSON array
  • How to detect a process cpu/memory usage and kill it when it exceeds a certain value? [closed]
  • Clearcase: How to control whether SUID programs work in a view or not?
  • UITextField get focus and then lose focus immediately due to return YES in textFieldShouldReturn
  • Starting activity from phonegap plugin-webIntent
  • How to pass a parameter to an included page with JSF 1.2
  • How to add a progress ring to the splash screen in Windows 8?
  • How do I remove line feed characters when selecting data from SQL Server?
  • ASP.NET: replacing UpdatePanel with Jquery?
  • autotest on ubuntu does nothing
  • WSO2 Identity Server + Rest STS Client (without ESB)
  • Dynamically run java code with Process