第一种写法:BaseAdapter的使用
1.布局中添加listview
main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"2.定义listview条目布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>3.主activity中获取控件,添加适配器
<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/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是标题"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是文章"/>
</LinearLayout>
ListViewlistView;
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new MyAdapter());4.写适配类继承于BaseAdapter
(1).第一种常规写法
class MyAdapter extends BaseAdapter {(2).第二种优化写法,把臃肿的getView的代码移动到holdview中
View vv;
//listview长度
@Override
public int getCount() {
return result.size();
}
//当前的listview
@Override
public Object getItem(int i) {
return result.get(i);
}
//listview的id
@Override
public long getItemId(int i) {
return i;
}
//listview 的条目
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
vv = View.inflate(getApplicationContext(), R.layout.item_fre, null);
holder = new ViewHolder();
holder.textView = (TextView) vv.findViewById(R.id.tv1);
holder.textView2 = (TextView) vv.findViewById(R.id.tv2);
vv.setTag(holder);
} else {
vv = view;
holder = (ViewHolder) vv.getTag();
}
String title = (String) result.get(i).get("title");
String title2 = (String) result.get(i).get("url");
holder.textView.setText(title);
holder.textView2.setText(title2);
return vv;
}
//承载控件的viewholder
class ViewHolder {
TextView textView;
TextView textView2;
}
}
class MyAdapter extends BaseAdapter {(3),第三种代码提取,减少代码的复用,此多用于项目需要写多个listview的
//listview长度
@Override
public int getCount() {
return result.size();
}
//当前的listview
@Override
public Object getItem(int i) {
return result.get(i);
}
//listview的id
@Override
public long getItemId(int i) {
return i;
}
//listview 的条目
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
} else {
holder = (ViewHolder) view.getTag();
}
String title = (String) result.get(i).get("title");
String title2 = (String) result.get(i).get("url");
holder.textView.setText(title);
holder.textView2.setText(title2);
return holder.getVv();
}
//承载控件的viewholder
class ViewHolder {
View vv;
TextView textView;
TextView textView2;
public ViewHolder() {
initView();
}
private void initView() {
vv = View.inflate(getApplicationContext(), R.layout.item_fre, null);
this.textView = (TextView) vv.findViewById(R.id.tv1);
this.textView2 = (TextView) vv.findViewById(R.id.tv2);
vv.setTag(this);
}
public View getVv() {
return vv;
}
}
}
--建立BaseHolder用于提取许多viewHolder中相似或者重复的代码
package googlestory.siqiyan.com.csdntest;
import android.view.View;
/**
* 泛型T代表list里面的数据类型
* Created by threes on 2016/2/6.
*/
public abstract class BaseHolder<T> {
protected View vv;
protected T data;
public BaseHolder() {
vv = initView();
vv.setTag(this);//在此位置设置标签
}
/**
* 为listveiw设置数据
* @param data 条目数据
*/
public void setData(T data) {
this.data=data;
refreshView(data);
}
/**
* 当Holder中的view对象显示到界面上的时候调用
* @return
*/
public View getVview() {
return vv;
}
/**
* 初始化View对象 及其控件
* @return
*/
public abstract View initView();
/**
* 当setData调用的时候 会调用该方法 根据数据 显示界面
*/
public abstract void refreshView(T data);
}
--建立DefaultAdapter用于提取许多BaseAdapter中相似或者重复的代码
package googlestory.siqiyan.com.csdntest;此时mainActivity的代码如下
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* 泛型T代表list里面的数据类型
* Created by threes on 2016/2/6.
*/
public abstract class DefaultAdapter<T> extends BaseAdapter {
List<T> datas;
//创建构造函数初始化的时候给数据赋值
public DefaultAdapter(List<T> datas) {
this.datas = datas;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int i) {
return datas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
BaseHolder<T> holder;
if (view == null) {
holder = getHolder();
} else {
holder = (BaseHolder<T>) view.getTag();
}
T string = datas.get(i);//获取listview条目的数据
holder.setData(string);
return holder.getVview();
}
protected abstract BaseHolder<T> getHolder();
}
package googlestory.siqiyan.com.csdntest;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
List<Map<String, Object>> datas ;
ListView listView;
String path = "http://www.youkuaiyun.com/";
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datas = new ArrayList<>();
getIntepubntnet();
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new MyAdapter(datas));
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://googlestory.siqiyan.com.csdntest/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://googlestory.siqiyan.com.csdntest/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
class MyAdapter extends DefaultAdapter<Map<String, Object>> {
public MyAdapter(List<Map<String, Object>> datas) {
super(datas);
}
@Override
protected BaseHolder<Map<String, Object>> getHolder() {
return new ViewHolder();
}
}
//承载控件的viewholder
class ViewHolder extends BaseHolder<Map<String, Object>>{
TextView textView;
TextView textView2;
public View initView() {
vv = View.inflate(getApplicationContext(), R.layout.item_fre, null);
this.textView = (TextView) vv.findViewById(R.id.tv1);
this.textView2 = (TextView) vv.findViewById(R.id.tv2);
return vv;
}
@Override
public void refreshView(Map<String, Object> datas) {
String title = (String) datas.get("title");
String title2 = (String) datas.get("url");
System.out.println(title);
textView.setText(title);
textView2.setText(title2);
}
}
private void getIntepubntnet() {
HttpUtils utils = new HttpUtils();
utils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(getApplicationContext(), "加载失败",
Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(ResponseInfo<String> res) {
Toast.makeText(getApplicationContext(), "加载成功",
Toast.LENGTH_SHORT).show();
String ss = res.result;
getCsdnNetDate(ss);
}
});
}
private void getCsdnNetDate(String da) {
String csdnString = da;
String pp = " href=\"(.*?)\" data-original.*? alt=\"(.*?)\"";
String p2 = "title=\"(.*?)\" href=\"(.*?)\".*?363";
Pattern p = Pattern.compile(p2);
Matcher m = p.matcher(csdnString);
while (m.find()) {
MatchResult mr = m.toMatchResult();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", mr.group(1));
map.put("url", mr.group(2));
datas.add(map);
}
}
}
修改代码前版本:
package googlestory.siqiyan.com.csdntest;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import java.text.Format;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
List<String> ff = new ArrayList<>();
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
ListView listView;
String path = "http://www.youkuaiyun.com/";
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
getIntepubntnet();
listView.setAdapter(new MyAdapter());
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://googlestory.siqiyan.com.csdntest/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://googlestory.siqiyan.com.csdntest/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
class MyAdapter extends BaseAdapter {
//listview长度
@Override
public int getCount() {
return result.size();
}
//当前的listview
@Override
public Object getItem(int i) {
return result.get(i);
}
//listview的id
@Override
public long getItemId(int i) {
return i;
}
//listview 的条目
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
} else {
holder = (ViewHolder) view.getTag();
}
String title = (String) result.get(i).get("title");
String title2 = (String) result.get(i).get("url");
holder.textView.setText(title);
holder.textView2.setText(title2);
return holder.getVv();
}
//承载控件的viewholder
class ViewHolder {
View vv;
TextView textView;
TextView textView2;
public ViewHolder() {
initView();
}
private void initView() {
vv = View.inflate(getApplicationContext(), R.layout.item_fre, null);
this.textView = (TextView) vv.findViewById(R.id.tv1);
this.textView2 = (TextView) vv.findViewById(R.id.tv2);
vv.setTag(this);
}
public View getVv() {
return vv;
}
}
}
private void getIntepubntnet() {
HttpUtils utils = new HttpUtils();
utils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(getApplicationContext(), "加载失败",
Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(ResponseInfo<String> res) {
Toast.makeText(getApplicationContext(), "加载成功",
Toast.LENGTH_SHORT).show();
String ss = res.result;
getCsdnNetDate(ss);
}
});
}
private void getCsdnNetDate(String datas) {
String csdnString = datas;
String pp = " href=\"(.*?)\" data-original.*? alt=\"(.*?)\"";
String p2 = "title=\"(.*?)\" href=\"(.*?)\".*?363";
Pattern p = Pattern.compile(p2);
Matcher m = p.matcher(csdnString);
while (m.find()) {
MatchResult mr = m.toMatchResult();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", mr.group(1));
map.put("url", mr.group(2));
result.add(map);
}
}
}
第二种写法:ArrayAdapter的使用
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list2);
String[] ff= new String[]{"小名","小挂钩","小高","王热","李四","话哈"};
ArrayAdapter<String > adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,ff);
listView.setAdapter(adapter);
}
}
listview 指定的外观:
android.R.layout.simple_list_item_1 每个列表项都是一个普通的文本
android.R.layout.simple_list_item_2 每个列表都是一个普通的文本(字体略大)
android.R.layout.simple_list_item_checked 每个列表都有一个已勾选的列表
android.R.layout.simple_list_item_multiple_choice 每个列表项都是带多选框的文本
android.R.layout.simple_list_item_single_choice 每个列表项都是带单选按钮的文本