AndroidListView练习

1.首先点击ListView练习 进入 Listview 界面

 

 2.进入Listview 界面  

 

 3.用户可以在里面点击 喜爱的东西

 

4.当东西被选中的时候 会出现蓝色

 

 并且会弹出:

 

 

 

  5.这里面显示的时间 是电脑当前的时间 用的一个方法:

 

 代码:

package com.example.myapplication.listview;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
 
import com.example.myapplication.R;
 
public class ListViewActivity extends AppCompatActivity {
    private ListView List1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        List1=findViewById(R.id.List1);
        List1.setAdapter(new MylistAdapter(ListViewActivity.this));
        List1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(ListViewActivity.this,"你选的商品为"+(i+1)+"号",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

适配器:

package com.example.myapplication.listview;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
 
import com.example.myapplication.R;
 
public class MylistAdapter extends BaseAdapter{
     private Context context1;
     private LayoutInflater layoutInflater1;
 
     public MylistAdapter (Context context){
      this.context1=context;
      layoutInflater1=LayoutInflater.from(context);
     }
    public int getCount() {
        return 5;
    }
 
    @Override
    public Object getItem(int i) {
        return null;
    }
 
    @Override
    public long getItemId(int i) {
        return 0;
    }
    static class ViewHolder{
         public TextView tv_title,tv_wupin,tv_time;
    }
    @Override
    public View getView(int i, View v, ViewGroup viewGroup) {
         ViewHolder holder =null;
         if (v==null){
              v = layoutInflater1.inflate(R.layout.activity_list_item,null);
              holder = new ViewHolder();
              holder.tv_time=v.findViewById(R.id.tv_time);
             holder.tv_wupin=v.findViewById(R.id.tv_wupin);
             holder.tv_title=v.findViewById(R.id.tv_title);
              v.setTag(holder);
         }else {
             holder= (ViewHolder) v.getTag();
         }
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = sDateFormat.format(new java.util.Date());
         holder.tv_title.setText("用户你好");
         holder.tv_time.setText(date);
         holder.tv_wupin.setText("请选择你喜爱的物品");
        return v;
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="15dp">
     
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:background="@drawable/xiaohuli"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="用户你好"
            android:gravity="center"
            android:textSize="30dp"
            android:textColor="#000000"
            android:layout_marginLeft="15dp"/>
        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:text="现在时间为:2000-11-5"
            android:textSize="26dp"
            />
        <TextView
            android:id="@+id/tv_wupin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:gravity="center"
            android:text="请选择你喜爱的物品"
            android:textSize="26dp"
            />
         
    </LinearLayout>
 
</LinearLayout>

 

posted @ 2020-12-02 14:10  崔哥会花手  阅读(61)  评论(0编辑  收藏  举报