67811

android how to create a multiline list view

Question:

I am trying to create a list item which consists of two list item on above the other. The code I am using is this:

package com.example.list2; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv1=(ListView)findViewById(R.id.listView1); Level data[] = new Level[] { new Level("Heading 1", "Subheading 1"), new Level("Heading 2", "Subheading 2"), new Level("Heading 3", "Subheading 3") }; LevelAdapter adp=new LevelAdapter(this, R.layout.list_item, data); lv1.setAdapter(adp); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

the level.java file is this:

package com.example.list2; public class Level { //public int icon; public String title; public String title2; public Level() { super(); } public Level(String title,String title2) { super(); //this.icon = icon; this.title = title; this.title2=title2; } }

the leveladapter is this

package com.example.list2; import android.app.Activity; import android.content.Context; import android.graphics.Typeface; 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 LevelAdapter extends ArrayAdapter<Level> { static Context context; static int layoutResourceId; Level data[] = null; public LevelAdapter(Context context, int layoutResourceId, Level[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; WeatherHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); //row.setMinimumHeight(200); holder = new WeatherHolder(); // holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); holder.txtTitle2 = (TextView)row.findViewById(R.id.txtTitle2); row.setTag(holder); } else { holder = (WeatherHolder)row.getTag(); } Level weather = data[position]; holder.txtTitle.setText(weather.title); // holder.imgIcon.setImageResource(weather.icon); return row; } static class WeatherHolder { // ImageView imgIcon; TextView txtTitle; TextView txtTitle2; // ImageView imgIcon2; } }

the list item layout is this

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <TextView android:id="@+id/txtTitle" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:textStyle="bold" android:textSize="22sp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:padding="50dip" android:textColor="#736F6E" android:layout_alignParentLeft="true" android:textAppearance="@android:attr/textAppearanceLarge" /> <TextView android:id="@+id/txtTitle2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:layout_below="@id/txtTitle" android:textStyle="bold" android:textSize="12sp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:padding="50dip" android:textColor="#736F6E" android:layout_alignParentLeft="true" android:textAppearance="@android:attr/textAppearanceLarge" /> </RelativeLayout>

and finally the layout for the main activity is this

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:dividerHeight="1dp" /> </RelativeLayout>

In the output, all i get is a list item which has a very large height, but only the headings get displayed and not the subheadings. Where am i going wrong? Thanks

Answer1:

Well, I narrowed the list item XML dimensions to:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginBottom="2dp" android:layout_marginTop="2dp" android:gravity="center_vertical" android:padding="2dip" android:textAppearance="@android:attr/textAppearanceLarge" android:textColor="#736F6E" android:textSize="22sp" android:textStyle="bold" /> <TextView android:id="@+id/txtTitle2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/txtTitle" android:layout_marginBottom="2dp" android:layout_marginTop="2dp" android:gravity="center_vertical" android:padding="2dip" android:textAppearance="@android:attr/textAppearanceLarge" android:textColor="#736F6E" android:textSize="12sp" android:textStyle="bold" /> </RelativeLayout>

and in LevelAdapter#getView, just before return row I've added: holder.txtTitle2.setText(weather.title2);

The result is below image file: <img alt="Result image after below two changes" class="b-lazy" data-src="https://i.stack.imgur.com/Fuqkn.png" data-original="https://i.stack.imgur.com/Fuqkn.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Is that what you're trying to achieve? I am not mentioning other improvements as removing static fields from LevelAdapter.

Recommend

  • How to enable/disable context menu items on runtime
  • PopupMenu dismiss without checking the selected checkbox item
  • Android app error NullPointerException? [duplicate]
  • how to use to send data in android
  • List view crashes when scrolling “The application may be doing too much work on its main thread.”
  • Listview with multiple strings
  • Activity overlaying. WindowManager.LayoutParams
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • Validate child input components on submit with Vee-Validate and vue js 2
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • javaw.exe and eclipse startup problems
  • Can Jackson SerializationFeature be overridden per field or class?
  • output of program is not same as passed argument
  • Cross-Platform Protobuf Serialization
  • Redux, normalised entities and lodash merge
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Akka Routing: Reply's send to router ends up as dead letters
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • Android Studio and gradle
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • Android Google Maps API OnLocationChanged only called once
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?