Android Development Notes - ListView

For better attribute process, start with creating a new package and fill in the code manually (before directly creating an Activity, the system will automatically help fill in the code and create the layout)
Create a new package listview, and create a new Java Class under the package,

Create a new Layout resource file under layout

In listviewactivity Complete the code in Java

Classes written by ourselves are not available in androidmainfest If it is declared automatically in XML, it needs to be declared manually:

<activity android:name=".listview.ListViewActivity"></activity>

Then it will be the same as the previous operation! In activity_ Main XML to jump to the ListView presentation interface, and then in mainactivity Java. After that, you can set the activity in the layout file_ List_ View XML.

Common attributes

In the layout file activity_ List_ View The layout in XML is as follows:

You can see the initial appearance of the list.

Next, create a new layout resource filer under the layout file to design the contents to be displayed in the list:

The general layout is as follows:

Adapter interface

Create a new class under the ListView package that inherits from the methods in the BaseAdapter override:

public class MyListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MyListAdapter(Context context){

    public int getCount() {
        return 10; //Number of rows in the list

    public Object getItem(int position) {
        return null;

    public long getItemId(int position) {
        return 0;

    static class ViewHolder{
        public ImageView imageView;
        public TextView tvTitle,tvTime,tvContent;

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
            holder=new ViewHolder();
        }else {
            holder= (ViewHolder) convertView.getTag();
        //Assign values to controls
        holder.tvTitle.setText("This is the title");
        holder.tvContent.setText("This is the content");

        return convertView;

Here, the getView function is mainly rewritten to control the style and data of each row.

In listviewactivity Java to declare the ListView control:

setAdapter is used to set an adapter. MyListAdapter is customized and inherited from BaseAdapter.

Current effect:

Customize the selected effect:

Create a new resource file in drawable, as follows:

Set to light green when selected:

In activity_ List_ View Add a code to the XML:

Run it. The effect is as follows:

Click event and long press event

In listviewactivity Add an event in Java as follows:

Operation effect:

Here, both events are handled when long pressing. Just change the return false in setOnItemLongClickListener to return ture.

Here is just a Toast event, which may jump to other pages in actual development.

Tags: Android

Posted by cofey12681 on Mon, 30 May 2022 15:43:25 +0530