10.ListView
Java
package com.example.firstapp.ListView; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.example.firstapp.R; public class ListViewActivity extends AppCompatActivity { private ListView mLv1; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); mLv1 = findViewById(R.id.lv_1); mLv1.setAdapter(new MyListAdapter(ListViewActivity.this)); mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ListViewActivity.this,"点击"+position,Toast.LENGTH_SHORT).show(); } }); mLv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ListViewActivity.this,"长按"+position,Toast.LENGTH_SHORT).show(); return true; } }); } }