ListView with two lines

0

In the android studio in the component I found the following TwoLineListItem, by the name I imagined that could put two lines of texts without having to create a customized xml, does anyone know if it's possible?

    
asked by anonymous 19.09.2016 / 09:01

2 answers

0

See if the example helps:

    public class CTesteActivity extends AppCompatActivity
{
  protected ListView lstBse;
  protected Button btnAdd;

  protected ArrayList<RowData> m_data;
  protected ListViewAdapter m_adapter;

  //-------------------------------------

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    // Fonte de dados
    m_data = new ArrayList<RowData>();
    // O Adaptador tem a função de ligar a fonte de dados a lista
    m_adapter = new ListViewAdapter();

    setTitle("teste");
    setContentView(R.layout.listview_activity);

    lstBse = (ListView) findViewById(R.id.lstBse);
    // Conteúdo a ser exibido quando a lista estiver vazia
    lstBse.setEmptyView(findViewById(R.id.lstEmpty));
    // Vincula o adaptador a lista
    lstBse.setAdapter(m_adapter);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(new onAdd());
  }

  //-----------------------------------

  // Listener button click
  protected class onAdd implements View.OnClickListener
  {
    protected int count;

    @Override
    public void onClick(View view)
    {
      // Adiciona uma linha aos dados
      m_data.add(new RowData(++count));
      // Notifica o adaptador que a fonte de dados foi modificada
      m_adapter.notifyDataSetChanged();
    }
  }

  //-----------------------------------

  // Adaptador responsável pela ligação entre a fonte de dados e a lista
  protected class ListViewAdapter extends BaseAdapter
  {

    @Override
    public int getCount()
    {
      return m_data.size();
    }

    @Override
    public Object getItem(int i)
    {
      return m_data.get(i);
    }

    @Override
    public long getItemId(int i)
    {
      return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {
      View result = view;
      ListViewItem item;

      if (result == null) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        result = inflater.inflate(R.layout.listview_item, viewGroup, false);

        item = new ListViewItem(result);

        result.setTag(item);
      }
      else {
        item = (ListViewItem) view.getTag();
      }

      item.setData(i);

      return result;
    }

  } // end ListViewAdapter


  //-----------------------------------
  // Mesmo qua a lista tenha,por exemplo, 100 itens o adaptador vai criar
  // apenas 10 linhas,  esta classe é responsável por guardar
  // estes 10 lugares e setar o dado conforme solicitado pelo adaptador
  protected class ListViewItem
  {
    protected TextView text1;
    protected TextView text2;

    public ListViewItem(View view)
    {
      super();

      text1 = (TextView) view.findViewById(R.id.text1);
      text2 = (TextView) view.findViewById(R.id.text2);
    }

    public void setData(int i)
    {
      text1.setText(m_data.get(i).line1);
      text2.setText(m_data.get(i).line2);
    }

  } // end ListViewItem

  //---------------------------------
  // Linha de dado
  protected class RowData
  {
    protected String line1;
    protected String line2;

    public RowData(int i)
    {
      super();

      line1 = "Tíulo " + i;
      line2 = "Descrição do item  " + i;
    }

  } // end RowData

  //---------------------------------

} // end class

Activity Layout - listview_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lstBse"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#1A000000"
        android:paddingTop="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:scrollbars="none"
        android:dividerHeight="1px"/>

    <TextView
        android:id="@+id/lstEmpty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center"
        android:textSize="18sp"
        android:text="lista vazia"/>

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_gravity="center|bottom"
        android:text="adicionar item"/>

</FrameLayout>

Layout of listview items

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="5dp">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="#CC182E4D"/>


    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
    
19.09.2016 / 23:48
2

That's right, it's a pre-formatted listview with two rows, the values should be accessed using the text1 and text2 IDs. But this class has become obsolete from API 17, where they recommend using XML with RelativeLayout or LinearLayout, it's not that hard to do and it's more flexible.

    
19.09.2016 / 13:51