Android之ScrollView


xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ScActivity">
    <LinearLayout
        android:id="@+id/main_ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
   <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="趣味科普"
        android:textSize="30dp"
        android:gravity="center"
        android:textStyle="bold"/>
    <View
        android:id="@+id/main_top_layout"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="@color/grey_EEEEE"/>


        <ListView
            android:id="@+id/sc_lv"
            android:layout_width="match_parent"
            android:layout_height="640dp"
            android:layout_marginTop="20dp"
            android:background="@color/white"
            android:divider="@null"
            android:dividerHeight="6dp"
            android:padding="10dp"
            android:scrollbars="none" />

    </LinearLayout>
</ScrollView>

  对应Java文件:

package com.hui.weblab;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import bean.bilibili;


public class ScActivity extends AppCompatActivity {
    ArrayList<bilibili> bilibiliArrayList=null;

/*
public ArrayList<bilibili> run() {
    String diver = "com.mysql.jdbc.Driver";
    //加入utf-8是为了后面往表中输入中文,表中不会出现乱码的情况
    String url = "jdbc:mysql://10.99.116.217:3306/exercise";
    String user = "qc";//用户名
    String password = "123456";//密码
    /*
     * 连接数据库10.99.116.217
     * */
/*
        try {
            Class.forName(diver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection conn=null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    Statement st=null;
    ResultSet rs=null;
    ArrayList<bilibili> bilibiliArrayList1=null;
    String sql = "select * from bilibili";

    try {
        st = conn.createStatement();
        rs = st.executeQuery(sql);
        while (rs.next()) {
            bilibili bi = new bilibili();
            bi.setAuthor(rs.getString("author"));
            bi.setTitle(rs.getString("title"));
            bi.setId(rs.getInt("id"));
            bilibiliArrayList1.add(bi);
            System.out.print(bi.getId());
        }
        st.close();
        conn.close();
        System.out.println("连接数据库成功");
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("连接数据库失败");
    }
return bilibiliArrayList1;
}*/






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sc);

/*
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;
*/


 //       bilibiliArrayList=run();
        String[] str= {"美丽科学BOS","爆炸实验室BoomLab","宝藏资源库","化学小莫","白小白很不乖","奇奇实验坊","龙虾麻麻的宝藏育儿库","唐唐频道","魔力科学小实验","H2元素实验室","氟氟今天吃什么"};
        String[] url={"https://www.bilibili.com/video/BV1oa4y1x7Wr?spm_id_from=333.337.search-card.all.click","https://www.bilibili.com/video/BV1zW411B7Q1?spm_id_from=333.337.search-card.all.click", "https://www.bilibili.com/video/BV1Qv4y1N7DL?spm_id_from=333.337.search-card.all.click","https://www.bilibili.com/video/BV1c4411r7fB?spm_id_from=333.337.search-card.all.click","https://www.bilibili.com/video/BV1Ei4y1Q75M?spm_id_from=333.337.search-card.all.click","https://www.bilibili.com/video/BV1Hh411B7Sa?spm_id_from=333.337.search-card.all.click","https://www.bilibili.com/video/BV1yF411B7LV?spm_id_from=333.337.search-card.all.click","https://www.bilibili.com/video/BV1bb411E7TY?spm_id_from=333.337.search-card.all.click"};

   //     int i=0;
     /*   for(bilibili it : bilibiliArrayList)
        {
            str[i++]=it.getTitle();
    }*/
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(ScActivity.this,
                android.R.layout.simple_list_item_1,str);
        ListView listView=(ListView)findViewById(R.id.sc_lv);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()   {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {
                Intent intent= new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(url[arg2]);
                intent.setData(content_url);
                startActivity(intent);
            }
        });
        }
        }

  

原创文章,作者:dweifng,如若转载,请注明出处:https://blog.ytso.com/273150.html

(0)
上一篇 2022年7月10日
下一篇 2022年7月10日

相关推荐

发表回复

登录后才能评论