• 将数据显示在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时携带数据跳转到其他界面的代码

  • 相关阅读:
    html5中利用FileReader来读取文件。
    在响应式布局中,碰到图片不会自动缩放,因此需要一段js脚本
    简单的游戏副本刷新倒计时制作
    在webpack4 中利用Babel 7取消严格模式方法
    vue中按需引入mint-UI报Error: .plugins[3][1] must be an object, false, or undefined
    如何在webpack开发中利用vue框架使用ES6中提供的新语法
    Module not found: Error: Can't resolve '@babel/runtime/helpers/classCallCheck' and Module not found: Error: Can't resolve '@babel/runtime/helpers/defineProperty'
    DOTNET CORE源码分析之IOC容器结果获取内容补充
    DOTNET CORE源码分析之IServiceProvider、ServiceProvider、IServiceProviderEngine、ServiceProviderEngine和ServiceProviderEngineScope
    DOTNET CORE源码分析之IServiceCollection、ServiceCollection和ServiceCollectionDescriptorExtensions
  • 原文地址:https://www.cnblogs.com/miao-com/p/14491631.html
Copyright © 2020-2023  润新知