马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
继续昨天的项目,给增加可以添加删除条目的功能。修改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:id="@+id/idimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@android:drawable/ic_delete" />
<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:textSize="20sp"
android:fontFamily="cursive" />
</LinearLayout>
</LinearLayout>
在layout目录下新建alert_dailog.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">
<EditText
android:id="@+id/idaddtext"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/idaddbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add data"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>
</LinearLayout>
在res目录下新建menu目录,然后再menu目录里新建menu.xml文件:<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/idmenu"
android:icon="@android:drawable/ic_input_add"
android:title="menu"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
修改MainActivity.kt文件:package com.example.xinwei.httpproject
import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.alert_dailog.*
import kotlinx.android.synthetic.main.alert_dailog.view.*
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() {
var adapter:InternetDataAdapter?=null
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))
}
adapter=InternetDataAdapter(context, dataArray)
idlist.adapter=adapter
}
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 ""
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item!!.itemId==R.id.idmenu){
var view=layoutInflater.inflate(R.layout.alert_dailog,null)
var addButton=view.idaddbutton
var addText=view.idaddtext
var alert=AlertDialog.Builder(this)
alert.setView(view)
alert.create()
alert.show()
addButton.setOnClickListener {
if (addText.text.isNotEmpty()){
dataArray.add(InternetData("added data",addText.text.toString()))
dataArray.reverse()
adapter!!.notifyDataSetChanged()
}
}
}
return true
}
}
修改makeAdapter.kt文件: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.ImageView
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
var image=view!!.findViewById<View>(R.id.idimage) as ImageView
title.text=internetData.title
data.text=internetData.data
image.setOnClickListener {
dataArray.remove(internetData)
notifyDataSetChanged()
}
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
}
}
效果图为:
|