Create several buttons or switches with ListView in android studio

-1

hello my question is how to create buttons or switches in android studio, I have seen examples but only with text and that is clear to me, what I need is to put buttons or switches with ListView and to be added to the list thanks!

java code

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Switch;
    import android.widget.TextView;

 public class MainActivity extends AppCompatActivity {

TextView txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt = (TextView)findViewById(R.id.txt);

    final String[] datos = new String[];
    super.onCreate(savedInstanceState);
    Switch mySwitch = new Switch(this);
    mySwitch.setText("Press me");
    ArrayAdapter<String> adaptador = new ArrayAdapter<String>
    (this,android.R.layout.simple_list_item_2, datos);
    ListView lista = (ListView)findViewById(R.id.lista);
    lista.setAdapter(adaptador);

  }
}

and this is the xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:padding="10px"
android:orientation="vertical">

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:textColor="@color/colorAccent" />


<ListView
    android:id="@+id/lista"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="26dp" />
</LinearLayout>
    
asked by Luis Islas 26.07.2017 в 20:48
source

1 answer

1

What you have to do is customize the items in your ListView to your liking (add any widget you need), create an Adapter > and assign it to your ListView .

Search on the internet customize item ListView and you will get a lot of tutorials, personally I recommend those of Beautiful Programming , they are very complete.

    
answered by 26.07.2017 / 23:16
source