MainActivity
package a.b.c.lelelistview;
import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity{ private List<Bean> data = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savaInstanceState); setContentView(R.layout.activity_main); for(int i = 0;i < 100;i++){ Bean bean = new Bean(); bean.setName("lele"+i); data.add(bean); } ListView listView = findViewById(R.id.lv); listView.setAdapt(new MyAdapter(data,this)); listView.setOnItemClickListenner(new AdapterView.OnItemClickListener()){ @override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Log.e("lele", "onItemClick:" + i ); } } } }
Bean package a.b.c.lelelistview; public class Bean {
String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
MyAdapter package a.b.c.lelelistview;
import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List<Bean> data; private Context context;
public MyAdapter(List<Bean> data, Context context) { this.data = data; this.context = context; }
@Override public int getCount() { return data.size(); }
@Override public Object getItem(int i) { return null; }
@Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, View ViewGroup viewGroup){ if(view == null){ view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false); } TextView textView = view.findViewById(R.id.tv); tevtView.setText(data.get(i).getName()); Log.e("lele","getView: "+i); return view } }
|