马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
你如果想在listview里表现更多的东西,比如图片,多几个标题跟文字的话就要用到以下这种方法。修改昨天的项目,在layout目录下新建data_item.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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:background="@drawable/background">
<TextView
android:id="@+id/idtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaa"
android:layout_marginBottom="10dp"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/idtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbbbbbbbbbbbbbbbbb"
android:fontFamily="cursive" />
</LinearLayout>
</LinearLayout>
这里我给第二个linearlayout设置了个自定义的背景色,所以还要在drawable目录下创建个background.xml文件:<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#D3FF93" />
<corners android:radius="10dp"/>
<gradient android:startColor="#D3FF93"/>
</shape>
</item>
</selector>
在这里定义了背景的颜色,边角,渐变,这种xml方式使用起来非常方便。然后我们在MainActivity.kt旁边新建个makeAdapter.kt的文件,在这个文件里我们给listview自定义一个adapter,而不用系统给的ArrayAdapter了,代码为:package com.example.xinwei.httpproject
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
data class InternetData(val title:String,val data:String)
var dataArray=ArrayList<InternetData>()
class InternetDataAdapter(val context:Context, val dataArray:ArrayList<InternetData>):BaseAdapter() {
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
var internetData=dataArray[p0]
var view:View?
if (p1==null){
view=LayoutInflater.from(context).inflate(R.layout.data_item,p2,false)
}else{
view=p1
}
var title=view!!.findViewById<View>(R.id.idtext1) as TextView
var data=view!!.findViewById<View>(R.id.idtext2) as TextView
title.text=internetData.title
data.text=internetData.data
return view
}
override fun getItem(p0: Int): Any {
return dataArray[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return dataArray.size
}
}
第一行是一个数据类,因为是复杂数据,最好的方式是把数据打包成数据类再把这个类的多个对象放进一个数组里,以便于使用。所以在底下又新建了个数组。在我们自定义的adapter里复写了继承自BaseAdapter的4个方法,分别作用是:获取布局文件然后把数据放进去,获取每一项数组里数据,获取数组里每一项的id,获取数组长度。最后还要修改MainActivity.kt:package com.example.xinwei.httpproject
import android.annotation.SuppressLint
import android.content.Context
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.*
import java.net.HttpURLConnection
import java.net.URL
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getInternetData()
}
fun getInternetData(){
val url="https://query.yahooapis.com/v1/public/yql?q=show%20tables&diagnostics=true"
val myAsyncTask=MyAsyncTask(this)
myAsyncTask.execute(url)
}
@SuppressLint("StaticFieldLeak")
inner class MyAsyncTask(val context:Context) : AsyncTask<String, String, String>() {
override fun onProgressUpdate(vararg values: String?) {
super.onProgressUpdate(*values)
}
override fun doInBackground(vararg p0: String?):String {
try {
val url=URL(p0[0])
val urlConnect=url.openConnection() as HttpURLConnection
urlConnect.connectTimeout=7000
urlConnect.requestMethod="GET"
urlConnect.readTimeout=7000
var inString=convertToString(urlConnect.inputStream)
var parsedData=parseXML(inString)
publishProgress(parsedData)
urlConnect.disconnect()
return parsedData
}catch (e:Exception){
e.printStackTrace()
}
return "网站连接失败"
}
private fun convertToString(inputStream: InputStream?): String {
var bufferedReader=BufferedReader(InputStreamReader(inputStream))
var allString=StringBuilder()
bufferedReader.forEachLine {
allString.append(it)
}
bufferedReader.close()
inputStream?.close()
return allString.toString()
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
var data=result!!.split(" ")
data.map {
dataArray.add(InternetData("title",it))
}
idlist.adapter=InternetDataAdapter(context, dataArray)
}
private fun parseXML(result: String): String {
try {
var factory=XmlPullParserFactory.newInstance()
var xmlParser=factory.newPullParser()
xmlParser.setInput(StringReader(result))
var eventType=xmlParser.eventType
var str= ArrayList<String>()
while (eventType!=XmlPullParser.END_DOCUMENT){
var nodeName=xmlParser.name
when(eventType){
XmlPullParser.START_TAG->{
if ("table".equals(nodeName)){
str.add(xmlParser.nextText())
}
}
}
eventType=xmlParser.next()
}
return str.joinToString(" ")
}catch (e:Exception){
e.printStackTrace()
}
return ""
}
}
}
在这里主要是给数组初始化,和把我们新建的adapter赋值给listview。效果图为:
|