【Android】能跳转且显示图片的ListView

安卓 专栏收录该内容
17 篇文章 0 订阅

效果

在这里插入图片描述

实现

将贴图放置在drawable目录下
在这里插入图片描述

com/example/listview/MainActivity.java

package com.example.listview;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity {


    private ArrayList<String> mTitleList;
    private ArrayList<String> mUrlList;
    private ArrayList<Integer> mImageList;

    private ListView mListView;

    private ArrayList<HashMap<String, Object>> mDataList;

    private SimpleAdapter mAdapter;

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

            public void run() {
                String site = "https://v.juhe.cn/toutiao/index?key=bc781b290edc7cdfbdc7a9dec2a6fb87";
                try {
                    URL url = new URL(site);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    int httpCode = conn.getResponseCode();
                    if(httpCode == 200) {
                        InputStream is = conn.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        String data = new String();
                        String buf;
                        while((buf = br.readLine()) != null) {
                            data += buf;
                        }
                        br.close();
                        Log.d("MainActivity", data);


                    }else {
                        Log.d("MainActivity", "http请求失败,状态码:"+httpCode);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                        Toast.makeText(MainActivity.this, "刷新成功!", Toast.LENGTH_LONG).show();
                    }
                });
            }

        }.start();



        mTitleList = new ArrayList<String>();
        mDataList.add("某人通电话");
        mDataList.add("实时更新");
        mDataList.add("飞机现身!");
        mDataList.add("齐抛债");
        mDataList.add("允许合作制定5G标准");
        mDataList.add("两人双双卸任");
        mDataList.add("男子深夜驾车连撞多车?");
        mDataList.add("一人拒戴口罩");
        mDataList.add("允许与华为合作制定5G标准 华为回应");
        mDataList.add("公司高层变动");
        mDataList.add("济南青岛新房价格环比上涨");
        mDataList.add("拒绝韩方向请求");
        mDataList.add("判官");
        mDataList.add("新冠事件");
        mDataList.add("5G套餐用户数超5G手机出货量:你被5G了吗?");
        mDataList.add("孩子注意力不集中?");
        mDataList.add("暂时关闭场所");
        mDataList.add("李宇春彩绘妆容前卫个性 许魏洲摇滚范儿十足");
        mDataList.add("宣布在海峡活动");
   
        mUrlList = new ArrayList<String>();
        mUrlList.add("https://news.sina.com.cn/c/xl/2020-06-17/doc-iirczymk7378319.shtml");
        mUrlList.add("https://news.sina.cn/zt_d/yiqing0121");
        mUrlList.add("https://news.sina.com.cn/c/2020-06-16/doc-iirczymk7332848.shtml");
        mUrlList.add("https://news.sina.com.cn/c/2020-06-16/doc-iircuyvi8787691.shtml");
        mUrlList.add("https://news.sina.com.cn/c/2020-06-16/doc-iirczymk7377056.shtml");
        mUrlList.add("https://news.sina.com.cn/c/2020-06-16/doc-iirczymk7374937.shtml");
        mUrlList.add("https://news.sina.com.cn/s/2020-06-16/doc-iirczymk7371281.shtml");
        mUrlList.add("https://news.sina.com.cn/w/2020-06-16/doc-iircuyvi8834509.shtml");
        mUrlList.add("https://news.sina.com.cn/c/2020-06-16/doc-iirczymk7377056.shtml");
        mUrlList.add("http://sd.sina.com.cn/news/2020-06-16/detail-iircuyvi8762291.shtml");
        mUrlList.add("http://sd.sina.com.cn/news/2020-06-15/detail-iircuyvi8518047.shtml");
        mUrlList.add("https://news.sina.com.cn/w/2020-06-17/doc-iirczymk7401317.shtml");
        mUrlList.add("https://news.sina.com.cn/w/2020-06-17/doc-iirczymk7384474.shtml");
        mUrlList.add("https://news.sina.com.cn/w/2020-06-16/doc-iircuyvi8778613.shtml");
        mUrlList.add("https://finance.sina.com.cn/china/gncj/2020-06-17/doc-iircuyvi8854225.shtml");

//放置图片
        mImageList = new ArrayList<Integer>();
        mImageList.add(R.drawable.news0);
        mImageList.add(R.drawable.news1);
        mImageList.add(R.drawable.news2);
        mImageList.add(R.drawable.news3);
        mImageList.add(R.drawable.news4);
        mImageList.add(R.drawable.news5);
        mImageList.add(R.drawable.news6);
        mImageList.add(R.drawable.news7);
        mImageList.add(R.drawable.news8);
        mImageList.add(R.drawable.news9);
        mImageList.add(R.drawable.news10);
        mImageList.add(R.drawable.news11);
        mImageList.add(R.drawable.news12);
        mImageList.add(R.drawable.news13);
        mImageList.add(R.drawable.news14);


        mDataList = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < mTitleList.size(); i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("T", mTitleList.get(i));
            map.put("I", mImageList.get(i));

            mDataList.add(map);
        }

        mListView = (ListView) findViewById(R.id.list_view);
        mAdapter = new SimpleAdapter(this, mDataList, R.layout.item_layout, new String[]{"T", "I"}, new int[]{R.id.text, R.id.img});
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = mUrlList.get(position);
                Intent intent = new Intent(MainActivity.this, NewsActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
            }
        });


        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {


            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                mTitleList.remove(position);
                mUrlList.remove(position);
                mImageList.remove(position);
                mDataList.remove(position);
                mAdapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "删除了第" + position + "条!", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }



}

com/example/listview/NewsActivity.java

package com.example.listview;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class NewsActivity extends Activity {

    private TextView mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_news);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");

        mText = (TextView) findViewById(R.id.text_news);

        mText.setText(url);
    }

}

layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

layout/activity_news.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="vertical" >

    <TextView
        android:id="@+id/text_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:autoLink="web" />

</LinearLayout>

Tip: android:autoLink=“web” 设置TextView为超链接

layout/item_layout.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="vertical" >

    <TextView
        android:id="@+id/text_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:autoLink="web" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.listview">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NewsActivity">

        </activity>

    </application>

</manifest>
  • 1
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值