0

no android studio nos componente achei o seguinte TwoLineListItem, pelo nome imaginei que poderia colocar duas linhas de textos sem precisar criar um xml personalizado, alguém sabe se é possível ?

2 Respostas 2

2

Isso mesmo, é um listView pré formatado com duas linhas, os valores devem ser acessados usando as IDs text1 e text2. Só que essa classe ficou obsoleta a partir da API 17, onde recomendam usar um XML com RelativeLayout ou LinearLayout, não é tão difícil de fazer e é mais flexivel.

2
  • tem como vc me ajudar dando um exemplo de como setar os valores nessa listView, agradeço
    – Vitor Hugo
    19/09/2016 às 19:58
  • Veja se o exemplo ajuda. Boa sorte 19/09/2016 às 21:49
0

Veja se o exemplo ajuda:

    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 da 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 dos itens da listview

<?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>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .