編輯:關於Android編程
package com.example.rootpeople1; public interface HttpGetData { void getDataUrl(String data); }
package com.example.rootpeople1; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.os.AsyncTask; public class HttpData extends AsyncTask三:創建ListData.java{ private HttpClient httpClient; private HttpGet httpGet; private String url; private HttpResponse httpResponse; private HttpEntity httpEntity; private InputStream in; private HttpGetData listener; //請求的urL public HttpData(String url,HttpGetData listener) { this.url=url; this.listener=listener; } @Override protected String doInBackground(String... params) { try { httpClient=new DefaultHttpClient(); httpGet=new HttpGet(url); httpResponse=httpClient.execute(httpGet); httpEntity=httpResponse.getEntity(); in=httpEntity.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) { // TODO: handle exception } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub6 listener.getDataUrl(result); super.onPostExecute(result); } }
package com.example.rootpeople1; public class ListData { private String content; public static final int send=1; public static final int receiver=2; private int flag; private String time; public String getTime() { return time; } public void setTime(String time) { this.time = 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 ListData(String content, int flag,String time) { super(); setContent(content); setFlag(flag); setTime(time); } }四:TextAdapter.java
package com.example.rootpeople1; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.RelativeLayout; import android.widget.TextView; public class TextAdapter extends BaseAdapter{ private List五:測試MainActivity.javalists; private Context mContext; private RelativeLayout layout; public TextAdapter(List lists, Context mContext) { this.lists = lists; this.mContext = mContext; } public int getCount() { // TODO Auto-generated method stub return lists.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return lists.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater=LayoutInflater.from(mContext); if(lists.get(position).getFlag()==ListData.receiver){ layout=(RelativeLayout) inflater.inflate(R.layout.leftitem, null); } if(lists.get(position).getFlag()==ListData.send){ layout=(RelativeLayout) inflater.inflate(R.layout.rightem, null); } TextView tv = (TextView) layout.findViewById(R.id.tv); TextView time= (TextView) layout.findViewById(R.id.time); time.setText(lists.get(position).getTime()); tv.setText(lists.get(position).getContent()); return layout; } }
package com.example.rootpeople1; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends Activity implements HttpGetData,OnClickListener { private HttpData httpdata; private ListUI布局文件list; private ListView lv; private Button send_btn; private EditText sendtext; private String content_str; private TextAdapter adapter; private String [] welcomeArray; private double currenttime,oldTime=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initeview(); } private void initeview(){ list=new ArrayList (); lv=(ListView) findViewById(R.id.lv); send_btn=(Button) findViewById(R.id.send_btn); sendtext=(EditText) findViewById(R.id.senText); send_btn.setOnClickListener(this); adapter=new TextAdapter(list,this); lv.setAdapter(adapter); ListData listData = null; listData=new ListData(getRandomWelcomeTips(), listData.receiver,getTime()); System.out.println("時間"+listData); list.add(listData); } public void getDataUrl(String data) { // TODO Auto-generated method stub parseText(data); } public void parseText(String str){ try { JSONObject jb=new JSONObject(str); /*System.out.println(jb.getString("code")); System.out.println(jb.getString("text"));*/ ListData listData = null; listData=new ListData(jb.getString("text"),listData.receiver,getTime()); System.out.println("時間"+listData); list.add(listData); adapter.notifyDataSetChanged(); } catch (Exception e) { // TODO: handle exception } } public void onClick(View v) { content_str=sendtext.getText().toString(); sendtext.setText(""); String dropk=content_str.replace(" ", ""); String droph=dropk.replace("\n", ""); ListData listdata = null; listdata=new ListData(content_str,listdata.send,getTime()); System.out.println("sfds"+listdata); list.add(listdata); if(list.size()>30){ for(int i=0;i<list.size();i++){ list.remove(i);="" }="" adapter.notifydatasetchanged();="" httpdata="(HttpData)" new="" httpdata(="" "http:="" www.tuling123.com="" openapi="" api?key="5a4b5c8bbf2c8a9dd02861999fa0d45c&info="+droph,this).execute();" private="" string="" getrandomwelcometips(){="" welcome_tipe="null;" welcomearray="this.getResources().getStringArray(R.array.welcome_tips);" int="" index="(int)" (math.random()*(welcomearray.length-1));="" return="" welcome_tipe;="" gettime(){="" currenttime-="System.currentTimeMillis();" simpledateformat="" format="new" simpledateformat("yyyy年mm月dd日="" hh:mm:ss");="" simpledateformat("hh:mm");="" date="" curdata="new" date();="" str="format.format(curdata);" if(currenttime="" -="" oldtime="">=5*60*1000){ oldTime=currenttime; return str; }else{ return ""; } } }
leftitem.xml
rightem.xml
最終運行界面:
Activity的啟動;Intent intent = new Intent(this,A.class);startActivity(intent);startActiv
package com.example.administrator.newstop;import android.os.Bundle;import
先給大家展示效果圖:1.新建TestFragmen繼承Fragmentpublic class TestFragment extends Fragment { priv
Android Studio安裝更新終極解決方式之前寫過一篇Android SDK無法更新的博文,其實該方式對Android Studio同樣有效,大伙可以下載網盤中分享