Listview with two lines

Asked

Viewed 602 times

0

in android studio in the component I found the following Twolinelistitem, by the name I figured I could put two lines of text without needing to create a custom xml, someone knows if it is possible ?

2 answers

2

That’s right, it is a pre-formatted two-line listview, the values must be accessed using the text1 and text2 Ids. Only this class has been deprecated since API 17, where they recommend using an XML with Relativelayout or Linearlayout, it is not so difficult to do and is more flexible.

  • have as you help me by giving an example of how to set the values in this listview, I thank you

  • See if the example helps. Good luck

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

Layout of Activity - 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.