4.10

所花时间(包括上课):1

打码量(行):150

博客量(篇):1

了解到知识点:学习ListView条目时间冲突

 

 import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

 

import java.util.ArrayList;

import java.util.Arrays;

 

public class MainActivity extends AppCompatActivity {

 

    private ListView mListView;

    private ArrayAdapter<String> mAdapter;

    private ArrayList<String> mData;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mListView = findViewById(R.id.listView);

        mData = new ArrayList<>(Arrays.asList("Item 1", "Item 2", "Item 3", "Item 4", "Item 5"));

        mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mData);

 

        mListView.setAdapter(mAdapter);

 

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // 处理条目的点击事件

                String item = mAdapter.getItem(position);

                Toast.makeText(MainActivity.this, "Clicked item: " + item, Toast.LENGTH_SHORT).show();

            }

        });

    }

}

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <ListView

        android:id="@+id/listView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

posted @ 2024-04-10 21:18  赵千万  阅读(7)  评论(0编辑  收藏  举报