这里先给大家一个地址http://www.jikexueyuan.com/course/249_9.html?ss=2,我可不是给这个极客学院做宣传的,但是这个demo,绝对的精品;值得大家去学习。
这个demo的主要内容是:
1.异步通信。
2.Json数据解析。
3.布局的实现。
4.数据的封装。
异步通讯这是一个点,
看里面的代码
public class HttpDate extends AsyncTask<String, Void, String> {
private HttpClient mHttpClient;
private HttpGet mHttpGet;
private HttpResponse mHttpResponse;
private HttpEntity mHttpEntity;
private InputStream in;
private HttpClientUrl listener;
private String url;
public HttpDate(String url,HttpClientUrl listener) {
this.url = url;
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
try {
mHttpClient = new DefaultHttpClient();
mHttpGet = new HttpGet(url);
mHttpResponse = mHttpClient.execute(mHttpGet);
mHttpEntity = mHttpResponse.getEntity();
in = mHttpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
**listener.getDateUrl(result);**
super.onPostExecute(result);
}
}
实现一个借口,用来传递信息。HttpClientUrl接口:
public interface HttpClientUrl {
void getDateUrl(String date);
}
在主程序里面实现这个接口,重写这个方法:
在主程序里面:
public void getDateUrl(String date) {
//这个是数据解析类
praseTest(date);
}
再说一个重点,就是数据封装;
public class ListDate {
public static final int SEND = 1;
public static final int RECEIVER = 2;
private String content;
private int flag;
private String time;
public ListDate(String content,int flag,String time) {
setContent(content);
setFlag(flag);
setTime(time);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
你可以看到要调用的地方,在这个adapter中可以看到:
public class TextAdapter extends BaseAdapter{
private List<ListDate> list;
private Context content;
private RelativeLayout layout;
public TextAdapter(List<ListDate> list,Context content){
this.list=list;
this.content=content;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LayoutInflater inflater=LayoutInflater.from(content);
if(list.get(arg0).getFlag()==ListDate.RECEIVER){
layout=(RelativeLayout) inflater.inflate(R.layout.leftitem, null);
}
if(list.get(arg0).getFlag()==ListDate.SEND)
{
layout=(RelativeLayout) inflater.inflate(R.layout.righttitem, null);
}
TextView tc=(TextView) layout.findViewById(R.id.tc);
TextView time=(TextView) layout.findViewById(R.id.time);
time.setText(list.get(arg0).getTime());
tc.setText(list.get(arg0).getContent());
return layout;
}
}
另外主要是主程序里面了;也可以看到主要的调用过程;
这个demo对于你学习数据封装,是一个很好的例子;
下面是主程序代码:
public class MainActivity extends Activity implements HttpClientUrl,
OnClickListener {
private HttpDate mDate;
private List<ListDate> list;
private ListView lv;
private EditText tex;
private Button but;
private String content_str;
private TextAdapter madapter;
private String [] welcometip;
private double currtime,oldtime=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView() {
lv = (ListView) findViewById(R.id.lv);
tex = (EditText) findViewById(R.id.test);
but = (Button) findViewById(R.id.but);
list = new ArrayList<ListDate>();
but.setOnClickListener(this);
madapter=new TextAdapter(list, this);
lv.setAdapter(madapter);
ListDate li;
li=new ListDate(randomWelcome(),ListDate.RECEIVER,getTime());
list.add(li);
}
@Override
public void getDateUrl(String date) {
praseTest(date);
}
public void praseTest(String str) {
try {
JSONObject js = new JSONObject(str);
ListDate con = null;
con = new ListDate(js.getString("text"),con.RECEIVER,getTime());
list.add(con);
madapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String randomWelcome(){
String welcom=null;
welcometip=this.getResources().getStringArray(R.array.welcome);
int index=(int) (Math.random()*(welcometip.length-1));
welcom=welcometip[index];
return welcom;
}
@Override
public void onClick(View arg0) {
getTime();
content_str=tex.getText().toString();
tex.setText("");
String drop =content_str.replace(" ","");
String droph=drop.replace("\n", "");
ListDate listdata = null;
listdata=new ListDate(content_str,ListDate.SEND,getTime());
list.add(listdata);
if(list.size()>=30){
for(int i = 0;i<=10;i++){
list.remove(i);
}
}
madapter.notifyDataSetChanged();
mDate = (HttpDate) new HttpDate(
"http://www.tuling123.com/openapi/api?key=6af9822f5491fadfc142b53818bbd63a&info="+content_str,
this).execute();
}
private String getTime(){
currtime=System.currentTimeMillis();
SimpleDateFormat time=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curr=new Date();
String str=time.format(curr);
if(currtime-oldtime>500){
oldtime=currtime;
return str;
}else{
return "";
}
}
}
对于这个程序我敲完之后,对我受益匪浅;你也可以尝试的敲一边;
对于里面的布局,你可以敲一下,对你你对布局,会有一个重新的认识:
布局代码:leftitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
<ImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@+id/time"
android:padding="10dp"
android:src="@drawable/robot" />
<TextView
android:id="@+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time"
android:layout_marginRight="50dp"
android:layout_toRightOf="@+id/iv"
android:background="@drawable/aio_friend_bg_nor_11"
android:gravity="center" />
</RelativeLayout>
righttitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
<ImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/time"
android:padding="10dp"
android:src="@drawable/visitor" />
<TextView
android:id="@+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time"
android:layout_marginLeft="50dp"
android:layout_toLeftOf="@+id/iv"
android:background="@drawable/aio_user_bg_nor_11"
android:gravity="center" />
</RelativeLayout>
对于整个demo.敲完,我感觉android ,学的技巧太多了。
下面是下地址:http://download.csdn.net/detail/u012922981/8689263
视频学习地址:http://www.jikexueyuan.com/course/249_9.html?ss=2