zoukankan      html  css  js  c++  java
  • 将数据显示在listView上并且点击listView时携带数据跳转到其他界面

    代码如下:

    activity_main.xml

    1 <ListView
    2             android:id="@+id/account"
    3             android:layout_width="match_parent"
    4             android:layout_height="match_parent"
    5             android:padding="10dp"
    6             android:divider="@null"
    7             android:dividerHeight="6dp"
    8             android:layout_below="@id/btn_add"
    9             android:scrollbars="none" />

    layout_account.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="wrap_content"
     5     android:layout_marginTop="10dp"
     6     android:padding="10dp"
     7     android:background="#999999">
     8 
     9     <RelativeLayout
    10         android:layout_width="wrap_content"
    11         android:layout_height="wrap_content">
    12         <TextView
    13             android:id="@+id/account_name"
    14             android:layout_width="wrap_content"
    15             android:layout_height="wrap_content"
    16             android:text="姓名"
    17             android:textStyle="bold"
    18             android:textSize="20sp"/>
    19         <TextView
    20             android:id="@+id/gang"
    21             android:layout_width="wrap_content"
    22             android:layout_height="wrap_content"
    23             android:text="-"
    24             android:layout_toRightOf="@id/account_name"
    25             android:textStyle="bold"
    26             android:textSize="20sp"/>
    27         <TextView
    28             android:id="@+id/account_class"
    29             android:layout_width="wrap_content"
    30             android:layout_height="wrap_content"
    31             android:layout_toRightOf="@id/gang"
    32             android:text="班级"
    33             android:textSize="20sp"
    34             android:textStyle="bold" />
    35     </RelativeLayout>
    36     <LinearLayout
    37         android:layout_width="wrap_content"
    38         android:layout_height="wrap_content"
    39         android:layout_alignParentRight="true"
    40         android:orientation="vertical">
    41 
    42         <TextView
    43             android:id="@+id/account_id"
    44             android:layout_width="wrap_content"
    45             android:layout_height="wrap_content"
    46             android:text="学号"
    47             android:textStyle="bold"/>
    48         <TextView
    49             android:id="@+id/account_phone"
    50             android:layout_width="wrap_content"
    51             android:layout_height="wrap_content"
    52             android:text="电话"
    53             android:layout_marginTop="5dp"/>
    54     </LinearLayout>
    55 
    56 </RelativeLayout>

    此界面效果图为:

     
    Adapter.java
     1 import android.content.Context;
     2 import android.view.LayoutInflater;
     3 import android.view.View;
     4 import android.view.ViewGroup;
     5 import android.widget.ArrayAdapter;
     6 import android.widget.TextView;
     7 
     8 import java.util.List;
     9 
    10 public class Adapter extends ArrayAdapter {
    11     private final int resourceId;
    12 
    13     public Adapter(Context context, int textViewResourceId, List<User> objects){
    14         super(context,textViewResourceId,objects);
    15         resourceId=textViewResourceId;
    16     }
    17 
    18     @Override
    19     public View getView(int position, View convertView, ViewGroup parent){
    20         User user = (User) getItem(position); // 获取当前项的Account/实例
    21         View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
    22         TextView nametv,classtv,idtv,phonetv;
    23         nametv=view.findViewById(R.id.account_name);
    24         classtv=view.findViewById(R.id.account_class);
    25         idtv=view.findViewById(R.id.account_id);
    26         phonetv=view.findViewById(R.id.account_phone);
    27         nametv.setText(user.getUserName());
    28         classtv.setText(user.getUserClass());
    29         idtv.setText(user.getUserID());
    30         phonetv.setText(user.getUserPhone());
    31         return view;
    32     }
    33 }

    MainActivity.java

     1 public class MainActivity extends AppCompatActivity{
     2     //ListView
     3     private List<User> accountList=new ArrayList<User>();
     4     private ListView listView;
     5     @Override
     6     protected void onCreate(Bundle savedInstanceState) {
     7         super.onCreate(savedInstanceState);
     8         setContentView(R.layout.activity_main);
     9         //ListView
    10         initAccount();//初始化
    11         Adapter adapter=new Adapter(MainActivity.this,R.layout.layout_account,accountList);
    12         listView=findViewById(R.id.account);//绑定listview
    13         listView.setAdapter(adapter);//设置adapter
    14 
    15         //点击listView,跳转到统计信息界面
    16         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    17             @Override
    18             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    19                 User user=accountList.get(position);
    20                 Toast.makeText(MainActivity.this,user.toString(),Toast.LENGTH_LONG).show();
    21                 Intent intent=null;
    22                 intent=new Intent(MainActivity.this,InformationActivity.class);
    23                 String user_name=user.getUserName();
    24                 String user_ID=user.getUserID();
    25                 String user_class=user.getUserClass();
    26                 String user_phone=user.getUserPhone();
    27                 Toast.makeText(MainActivity.this,user_name,Toast.LENGTH_LONG).show();
    28                 Toast.makeText(MainActivity.this,user_ID,Toast.LENGTH_LONG).show();
    29                 Toast.makeText(MainActivity.this,user_class,Toast.LENGTH_LONG).show();
    30                 Toast.makeText(MainActivity.this,user_phone,Toast.LENGTH_LONG).show();
    31                 intent.putExtra("User_Name",user_name);
    32                 intent.putExtra("User_ID",user_ID);
    33                 intent.putExtra("User_Class",user_class);
    34                 intent.putExtra("User_Phone",user_phone);
    35                 startActivity(intent);
    36             }
    37         });
    38     }
    39 
    40     private void initAccount(){
    41         //从数据库里面把数据取出来
    42         MyDatabaseHelper databaseHelper=new MyDatabaseHelper(this);
    43         SQLiteDatabase db=databaseHelper.getWritableDatabase();
    44         Cursor cursor=db.query("user",null,null,null,null,null,null);
    45         while(cursor.moveToNext()){
    46             String userName=cursor.getString(cursor.getColumnIndex("userName"));
    47             String userId=cursor.getString(cursor.getColumnIndex("userID"));
    48             String userPhone=cursor.getString(cursor.getColumnIndex("userPhone"));
    49             String userClass=cursor.getString(cursor.getColumnIndex("userClass"));
    50             User user=new User(userId,userClass,userPhone,userName);
    51             accountList.add(user);
    52         }
    53     }
    54 }

    activity_information,xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context=".InformationActivity">
     8 
     9     <TextView
    10         android:id="@+id/Info1"
    11         android:textColor="#000000"
    12         android:textSize="16sp"
    13         android:layout_width="match_parent"
    14         android:layout_height="50dp"
    15         android:padding="5dp"
    16         android:digits="1234567890."
    17         android:layout_below="@id/back"
    18         android:layout_marginTop="20dp"
    19         android:background="#FDDEDE"
    20         />
    21 
    22     <TextView
    23         android:id="@+id/Info2"
    24         android:textColor="#000000"
    25         android:textSize="16sp"
    26         android:layout_width="match_parent"
    27         android:layout_height="50dp"
    28         android:padding="5dp"
    29         android:layout_below="@id/Info1"
    30         android:layout_marginTop="20dp"
    31         android:background="#FDDEDE"
    32         />
    33 
    34     <TextView
    35         android:id="@+id/Info3"
    36         android:layout_width="match_parent"
    37         android:layout_height="50dp"
    38         android:layout_below="@id/Info2"
    39         android:layout_marginTop="20dp"
    40         android:background="#FDDEDE"
    41         android:digits="1234567890"
    42         android:padding="5dp"
    43         android:textColor="#000000"
    44         android:textSize="16sp" />
    45 
    46     <TextView
    47         android:id="@+id/Info4"
    48         android:textColor="#000000"
    49         android:textSize="16sp"
    50         android:layout_width="match_parent"
    51         android:layout_height="50dp"
    52         android:padding="5dp"
    53         android:layout_below="@id/Info3"
    54         android:layout_marginTop="20dp"
    55         android:background="#FDDEDE"
    56         android:clickable="true"
    57         />
    58 
    59 
    60 </RelativeLayout>

    InformationActivity.java

     1 public class InformationActivity extends AppCompatActivity {
     2 
     3     private TextView name;
     4     private TextView ID;
     5     private TextView Classs;
     6     private TextView Phone;
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         setContentView(R.layout.activity_information);
    12 
    13         name=findViewById(R.id.Info1);
    14         ID=findViewById(R.id.Info2);
    15         Classs=findViewById(R.id.Info3);
    16         Phone=findViewById(R.id.Info4);
    17 
    18         Intent intent = getIntent();
    19         name.setText(intent.getStringExtra("User_Name"));
    20         ID.setText(intent.getStringExtra("User_ID"));
    21         Classs.setText(intent.getStringExtra("User_Class"));
    22         Phone.setText(intent.getStringExtra("User_Phone"));
    23 
    24     }
    25 }

    以上即为将数据显示在listView上并且点击listView时携带数据跳转到其他界面的代码

  • 相关阅读:
    4.计算机启动过程的简单介绍 计算机启动流程 计算机BIOS作用 POST 开机自检 计算机启动顺序 分区表 操作系统启动
    3.操作系统简单介绍 操作系统发展历史 批处理分时系统 操作系统是什么 操作系统对文件的抽象 进程 虚拟内存是什么 操作系统作用 操作系统功能
    2.计算机组成-数字逻辑电路 门电路与半加器 异或运算半加器 全加器组成 全加器结构 反馈电路 振荡器 存储 D T 触发器 循环移位 计数器 寄存器 传输门电路 译码器 晶体管 sram rom 微处理 计算机
    1.计算机发展阶段 计算机发展历史 机械式计算机 机电式计算机 电子计算机 逻辑电路与计算机 二极管 电子管 晶体管 硅 门电路 计算机 电磁学计算机二进制
    如何解决svn清理失败 不能更新 cleanup失败 cleanup乱码 更新乱码 svn更新提示清理 清理乱码不能清理 svn故障修复SVN cleanup 陷入死循环 svn cleanup时遇到错误怎么办
    eclipse svn插件卸载 重新安装 Subclipse卸载安装 The project was not built since its build path is incomplete This client is too old to work with the working copy at
    java for循环里面执行sql语句操作,有效结果只有一次,只执行了一次sql mybatis 循环执行update生效一次 实际只执行一次
    windows资源管理器多标签打开 windows文件夹多标签浏览 浏览器tab页面一样浏览文件夹 clover win8 win10 报错 无响应问题怎么解决 clover卡死 clover怎么换皮肤
    批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务
    不能ssh连接ubuntu linux 服务器 secureCRT不能ssh连接服务器 不能远程ssh连接虚拟机的ubuntu linux
  • 原文地址:https://www.cnblogs.com/miao-com/p/14491631.html
Copyright © 2011-2022 走看看