NewsList核心

    protected void Page_Load(object sender, EventArgs e)
    {
        bind();
    }
    string strStyle;
    protected void bind()
    { 
        int n = Convert.ToInt32(Request.QueryString["id"]);
        switch (n)
        {
            case 1: strStyle = "时政要闻";
                this.labTitle.Text = "新闻网络中心->时政要闻";
                this.imageLag.ImageUrl = "~/Images/图片时政要闻.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页时政要闻.jpg";
                break;
            case 2: strStyle = "经济动向";
                this.labTitle.Text = "新闻网络中心->经济动向";
                this.imageLag.ImageUrl = "~/Images/经济动向图片.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页经济动向.jpg";
                break;
            case 3: strStyle = "世界军事";
                this.labTitle.Text = "新闻网络中心->世界军事";
                this.imageLag.ImageUrl = "~/Images/世界军事图片.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页世界军事.jpg";
                break;
            case 4: strStyle = "科学教育";
                this.labTitle.Text = "新闻网络中心->科学教育";
                this.imageLag.ImageUrl = "~/Images/图片科学教育.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页科学教育.jpg";
                break;
            case 5: strStyle = "法治道德";
                this.labTitle.Text = "新闻网络中心->法治道德";
                this.imageLag.ImageUrl = "~/Images/法制道德图片.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页法制道德.jpg";
                break;
            case 6: strStyle = "社会现象";
                this.labTitle.Text = "新闻网络中心->社会现象";
                this.imageLag.ImageUrl = "~/Images/社会现象图片.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页社会现象.jpg";
                break;
            case 7: strStyle = "体育世界";
                this.labTitle.Text = "新闻网络中心->体育世界";
                this.imageLag.ImageUrl = "~/Images/体育世界图片.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页体育世界.jpg";
                break;
            case 8: strStyle = "时尚娱乐";
                this.labTitle.Text = "新闻网络中心->时尚娱乐";
                this.imageLag.ImageUrl = "~/Images/时尚娱乐图片.jpg";
                this.imageTitle.ImageUrl = "~/Images/二级页时尚娱乐.jpg";
                break;
        }
        //取得当前页的页码
        int curpage = Convert.ToInt32(this.labPage.Text);
        //使用PagedDataSource类实现DataList控件的分页功能
        PagedDataSource ps = new PagedDataSource();
        //获取数据集
        DataSet ds = CC.GetDataSet("select * from tb_News where style='" + strStyle + "' order by  issueDate Desc", "tbNews");
        ps.DataSource = ds.Tables["tbNews"].DefaultView;
        //是否可以分页
        ps.AllowPaging = true;
        //显示的数量
        ps.PageSize =16;
        //取得当前页的页码
        ps.CurrentPageIndex = curpage - 1;
        this.lnkbtnUp.Enabled = true;
        this.lnkbtnNext.Enabled = true;
        this.lnkbtnBack.Enabled = true;
        this.lnkbtnOne.Enabled = true;
        if (curpage == 1)
        {
            //不显示第一页按钮
            this.lnkbtnOne.Enabled = false;
            //不显示上一页按钮
            this.lnkbtnUp.Enabled = false;
        }
        if (curpage == ps.PageCount)
        {
            //不显示下一页
            this.lnkbtnNext.Enabled = false;
            //不显示最后一页
            this.lnkbtnBack.Enabled = false;
        }
        //显示分页数量
        this.labBackPage.Text = Convert.ToString(ps.PageCount);
        //绑定DataList控件
        this.dlNews.DataSource = ps;
        this.dlNews.DataKeyField = "id";
        this.dlNews.DataBind();
   
    }
   //第一页
    protected void lnkbtnOne_Click(object sender, EventArgs e)
    {
        this.labPage.Text = "1";
        this.bind();
    }
    //上一页
    protected void lnkbtnUp_Click(object sender, EventArgs e)
    {
        this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1);
        this.bind();
    }
    //下一页
    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1);
        this.bind();
    }
    //最后一页
    protected void lnkbtnBack_Click(object sender, EventArgs e)
    {
        this.labPage.Text = this.labBackPage.Text;
        this.bind();
    }
    protected void dlNews_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(dlNews.DataKeys[e.Item.ItemIndex].ToString());
        Response.Write("<script language=javascript>window.open('showNews.aspx?id=" + id + "','','width=520,height=260')</script>");
    }

}

转载于:https://www.cnblogs.com/JohnFeller/articles/1507765.html

三、简答题 1、使用SharedPreferences读写数据的步骤: ① 获取SharedPreferences对象:SharedPreferences sharedPreferences = getSharedPreferences("name", Context.MODE_PRIVATE); ② 获取SharedPreferences.Editor对象:SharedPreferences.Editor editor = sharedPreferences.edit(); ③ 写入数据:editor.putString("key", value); ④ 提交数据:editor.commit(); ⑤ 读取数据:sharedPreferences.getString("key", defaultValue); 2、Handler消息机制的工作原理: Handler是Android中消息机制的基础,它可以实现不同线程之间的通信。Handler包括两个主要部分:消息队列和消息处理器。消息队列是一个存储消息的FIFO队列,消息处理器则是用来处理消息的对象。当Handler创建时,它会自动与当前线程的消息队列关联起来。当调用Handler的sendMessage()方法,会向消息队列中添加一条消息。当Handler处理消息时,会从消息队列中取出一条消息,交给消息处理器处理,如果消息处理器处理完消息后需要更新UI,可以使用Handler的post()或者postDelayed()方法将消息发送到UI线程中执行。 3、使用HttpURLConnection访问网络的步骤: ① 创建URL对象:URL url = new URL("http://www.example.com"); ② 打开URLConnection连接:HttpURLConnection connection = (HttpURLConnection) url.openConnection(); ③ 设置请求方式:connection.setRequestMethod("GET"); ④ 设置连接超时时间:connection.setConnectTimeout(5000); ⑤ 设置读取超时时间:connection.setReadTimeout(5000); ⑥ 发送请求:connection.connect(); ⑦ 获取响应码:int responseCode = connection.getResponseCode(); ⑧ 获取输入流:InputStream inputStream = connection.getInputStream(); ⑨ 读取数据:BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } String result = stringBuilder.toString(); ⑩ 关闭连接:connection.disconnect(); 四、编程题 1、UserinfoActivity中的代码: ``` public void onClick(View view) { Intent intent = new Intent(UserinfoActivity.this, ShowActivity.class); intent.putExtra("id", 1); intent.putExtra("username", "eric"); intent.putExtra("state", true); startActivity(intent); } ``` ShowActivity中的代码: ``` Intent intent = getIntent(); int id = intent.getIntExtra("id", 0); String username = intent.getStringExtra("username"); boolean state = intent.getBooleanExtra("state", false); Toast.makeText(ShowActivity.this, "id=" + id + " username=" + username + " state=" + state, Toast.LENGTH_SHORT).show(); ``` 2、代码如下: ``` public void getData() { new Thread(new Runnable() { @Override public void run() { try { URL url = new URL("http://192.168.1.100:8080/news/getnewslist"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } String result = stringBuilder.toString(); Gson gson = new Gson(); List<News> newsList = gson.fromJson(result, new TypeToken<List<News>>(){}.getType()); for (News news : newsList) { // 将数据保存到SQLite数据库中 dbHelper.insertNews(news.getId(), news.getTitle(), news.getDetail(), news.getComment(), news.getImage()); } // 更新UI Message message = Message.obtain(); message.what = UPDATE_UI; message.obj = newsList; handler.sendMessage(message); reader.close(); inputStream.close(); } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } private void updateUI(List<News> newsList) { // 将数据显示到界面的控件上 } // News类 public class News { private int id; private String title; private String detail; private int comment; private String image; public News(int id, String title, String detail, int comment, String image) { this.id = id; this.title = title; this.detail = detail; this.comment = comment; this.image = image; } public int getId() { return id; } public String getTitle() { return title; } public String getDetail() { return detail; } public int getComment() { return comment; } public String getImage() { return image; } } // DBHelper类 public class DBHelper extends SQLiteOpenHelper { private static final String DB_NAME = "news.db"; private static final int DB_VERSION = 1; private static final String TABLE_NAME = "news"; private static final String COLUMN_ID = "id"; private static final String COLUMN_TITLE = "title"; private static final String COLUMN_DETAIL = "detail"; private static final String COLUMN_COMMENT = "comment"; private static final String COLUMN_IMAGE = "image"; private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY, " + COLUMN_TITLE + " TEXT, " + COLUMN_DETAIL + " TEXT, " + COLUMN_COMMENT + " INTEGER, " + COLUMN_IMAGE + " TEXT)"; public DBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void insertNews(int id, String title, String detail, int comment, String image) { SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_ID, id); values.put(COLUMN_TITLE, title); values.put(COLUMN_DETAIL, detail); values.put(COLUMN_COMMENT, comment); values.put(COLUMN_IMAGE, image); db.insert(TABLE_NAME, null, values); db.close(); } public List<News> getNewsList() { List<News> newsList = new ArrayList<>(); SQLiteDatabase db = getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null); if (cursor.moveToFirst()) { do { int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID)); String title = cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)); String detail = cursor.getString(cursor.getColumnIndex(COLUMN_DETAIL)); int comment = cursor.getInt(cursor.getColumnIndex(COLUMN_COMMENT)); String image = cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE)); News news = new News(id, title, detail, comment, image); newsList.add(news); } while (cursor.moveToNext()); } cursor.close(); db.close(); return newsList; } } // Handler类 private static final int UPDATE_UI = 1; private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message message) { switch (message.what) { case UPDATE_UI: List<News> newsList = (List<News>) message.obj; updateUI(newsList); break; } return false; } });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值