- package com.example.plantsai.myapplicationlistview;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import org.w3c.dom.Text;
- public class MainActivity extends AppCompatActivity {
- private TextView tv;
- String data[] = {"AAA","BBB","CCC","DDD","EEE"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_multiple_choice,data);
- ListView listView = findViewById(R.id.list);
- listView.setAdapter(adapter);
- listView.setTextFilterEnabled(true);
- listView.setSelector(R.color.colorPrimaryDark);
- ListView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- tv = findViewById(R.id.textView);
- tv.setText(data[position]);
- }
- };
- listView.setOnItemClickListener(listener);
- }
- }
複製代碼- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <ListView
- android:id="@+id/list"
- android:layout_width="368dp"
- android:layout_height="309dp"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.033" />
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="TextView"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.834" />
- </android.support.constraint.ConstraintLayout>
複製代碼 |