listview练习

java代码

 1 package com.example.myapplication.listview;
 2   
 3 import androidx.appcompat.app.AppCompatActivity;
 4   
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.AdapterView;
 8 import android.widget.ListView;
 9 import android.widget.Toast;
10   
11 import com.example.myapplication.R;
12   
13 public class ListViewActivity extends AppCompatActivity {
14     private ListView List1;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_list_view);
19         List1=findViewById(R.id.List1);
20         List1.setAdapter(new MylistAdapter(ListViewActivity.this));
21         List1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
22             @Override
23             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
24                 Toast.makeText(ListViewActivity.this,"你选的商品为"+(i+1)+"号",Toast.LENGTH_SHORT).show();
25             }
26         });
27     }
28 }

适配器

 1 package com.example.myapplication.listview;
 2   
 3 import android.content.Context;
 4 import android.view.LayoutInflater;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 import android.widget.BaseAdapter;
 8 import android.widget.TextView;
 9   
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 import java.util.Locale;
13 import java.util.TimeZone;
14   
15 import com.example.myapplication.R;
16   
17 public class MylistAdapter extends BaseAdapter{
18      private Context context1;
19      private LayoutInflater layoutInflater1;
20   
21      public MylistAdapter (Context context){
22       this.context1=context;
23       layoutInflater1=LayoutInflater.from(context);
24      }
25     public int getCount() {
26         return 5;
27     }
28   
29     @Override
30     public Object getItem(int i) {
31         return null;
32     }
33   
34     @Override
35     public long getItemId(int i) {
36         return 0;
37     }
38     static class ViewHolder{
39          public TextView tv_title,tv_wupin,tv_time;
40     }
41     @Override
42     public View getView(int i, View v, ViewGroup viewGroup) {
43          ViewHolder holder =null;
44          if (v==null){
45               v = layoutInflater1.inflate(R.layout.activity_list_item,null);
46               holder = new ViewHolder();
47               holder.tv_time=v.findViewById(R.id.tv_time);
48              holder.tv_wupin=v.findViewById(R.id.tv_wupin);
49              holder.tv_title=v.findViewById(R.id.tv_title);
50               v.setTag(holder);
51          }else {
52              holder= (ViewHolder) v.getTag();
53          }
54         SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
55         String date = sDateFormat.format(new java.util.Date());
56          holder.tv_title.setText("用户你好");
57          holder.tv_time.setText(date);
58          holder.tv_wupin.setText("请选择你喜爱的物品");
59         return v;
60     }
61 }

xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal"
 6     android:padding="15dp">
 7       
 8     <ImageView
 9         android:layout_width="100dp"
10         android:layout_height="100dp"
11         android:scaleType="fitXY"
12         android:background="@drawable/xiaohuli"/>
13     <LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:orientation="vertical">
17         <TextView
18             android:id="@+id/tv_title"
19             android:layout_width="match_parent"
20             android:layout_height="wrap_content"
21             android:text="用户你好"
22             android:gravity="center"
23             android:textSize="30dp"
24             android:textColor="#000000"
25             android:layout_marginLeft="15dp"/>
26         <TextView
27             android:id="@+id/tv_time"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:layout_marginLeft="15dp"
31             android:text="现在时间为:2000-11-5"
32             android:textSize="26dp"
33             />
34         <TextView
35             android:id="@+id/tv_wupin"
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:layout_marginLeft="15dp"
39             android:gravity="center"
40             android:text="请选择你喜爱的物品"
41             android:textSize="26dp"
42             />
43           
44     </LinearLayout>
45   
46 </LinearLayout>

 

posted @ 2020-12-20 16:41  RED-DEAD  阅读(52)  评论(0编辑  收藏  举报