package com.example.phonefragment;
import java.util.ArrayList;
import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;
import android.os.Bundle;import android.view.Window;import android.widget.ListView;import android.widget.SimpleAdapter;public class AwardActivity extends Activity {
private String[] names = new String[] { "学生A", "学生B", "学生C" };
private String[] says = new String[] { "学生A简介", "学生B简介", "学生C简介" };@Override
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_award);List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 15; i++) { Map<String, Object> showitem = new HashMap<String, Object>(); showitem.put("name", names[i%3]); showitem.put("says", says[i%3]); listitem.add(showitem); }// 创建一个simpleAdapter
SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.listview_award, new String[] { "name", "says" }, new int[] {R.id.tv_award_name, R.id.tv_award_says }); ListView listView = (ListView) findViewById(R.id.lv_award_listview); listView.setAdapter(myAdapter); }}
<?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" ><ListView
android:id="@+id/lv_award_listview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>
package com.example.phonefragment;
import java.io.BufferedReader;
import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;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 org.json.JSONArray;import org.json.JSONObject;import android.app.Fragment;
import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.LayoutInflater;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.ListView;import android.widget.PopupMenu;import android.widget.RadioButton;import android.widget.SimpleAdapter;import android.widget.Toast;public class ClassFragment extends Fragment implements OnClickListener {
Handler handler;
String telString; String nameString;String[] titleString;
String[] descriptionString; int[] cid; int[] aid;RadioButton addButton;
List<Map<String, Object>> al;
ListView listView; SimpleAdapter myAdapter;@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_class, container, false);titleString = new String[20];
descriptionString = new String[20]; cid = new int[20]; aid = new int[20]; handler = new Handler(); addButton = (RadioButton) view.findViewById(R.id.rbtn_class_add); listView = (ListView) view.findViewById(R.id.lv_class_listview); al = new ArrayList<Map<String, Object>>();telString = getActivity().getIntent().getStringExtra("tel");
myAdapter = new SimpleAdapter(getActivity(), al,
R.layout.listview_class, new String[] { "title", "name", "aid" }, new int[] { R.id.tv_class_title, R.id.tv_class_name, R.id.tv_class_aid });listView.setAdapter(myAdapter);
String url = "http://192.168.1.108:3000/users/query_course";
QueryClassThread httpThread = new QueryClassThread(url);
httpThread.start(); // Bind EventaddButton.setOnClickListener(this);
return view;
} @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); listView.setAdapter(myAdapter); }@Override
public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.rbtn_class_add: addButton.setSelected(true); PopupMenu popup = new PopupMenu(getActivity(), v);// 第二个参数是绑定的那个view // 获取菜单填充器 MenuInflater inflater = popup.getMenuInflater(); // 填充菜单 inflater.inflate(R.menu.main, popup.getMenu()); // 绑定菜单项的点击事件 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.addclass: Intent intent = new Intent(); intent.setClass(getActivity(), CreateClassActivity.class); Bundle bundle = new Bundle(); // 创建Bundle对象 bundle.putString("tel", telString); // 装入数据 intent.putExtras(bundle); // 把Bundle塞入Intent里面 startActivityForResult(intent, 2); addButton.setSelected(false); break;case R.id.addexam:
Toast.makeText(getActivity(), "创建考试···", Toast.LENGTH_SHORT).show(); addButton.setSelected(false); break; default: break; } return false; } }); popup.show(); break; } }@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch (resultCode) { case 2: String title = data.getStringExtra("title");Map<String, Object> showitem = new HashMap<String, Object>();
showitem.put("title", title); showitem.put("name", nameString); showitem.put("aid", "1000"); al.add(showitem);myAdapter.notifyDataSetChanged();
break; default: break; } }class QueryClassThread extends Thread {
private String str;
private String url;
public QueryClassThread(String url) {
// TODO Auto-generated constructor stub this.url = url; }public void doGet() {
HttpClient httpClient = new DefaultHttpClient(); // 注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊 String url = this.url + "?tel=" + telString; // 第二步:创建代表请求的对象,参数是访问的服务器地址 HttpGet httpGet = new HttpGet(url); try { // 第三步:执行请求,获取服务器发还的相应对象 HttpResponse response = httpClient.execute(httpGet); // 第四步:检查相应的状态是否正常:检查状态码的值是200表示正常 if (response.getStatusLine().getStatusCode() == 200) { // 第五步:从相应对象当中取出数据,放到entity当中 HttpEntity entity = response.getEntity(); BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent())); StringBuffer sb = new StringBuffer(); while ((str = reader.readLine()) != null) { sb.append(str); } str = sb.toString(); JSONObject res = new JSONObject(str); nameString = res.getString("name"); JSONArray arr = res.getJSONArray("courses"); for (int i = 0; i < arr.length(); i++) { JSONObject temp = (JSONObject) arr.get(i); cid[i] = temp.getInt("cid"); titleString[i] = temp.getString("title"); descriptionString[i] = temp.getString("description"); aid[i] = temp.getInt("aid"); }for (int i = 0; i < arr.length(); i++) {
Map<String, Object> showitem = new HashMap<String, Object>(); showitem.put("title", titleString[i]); showitem.put("name", nameString); showitem.put("aid", aid[i]); al.add(showitem); } myAdapter.notifyDataSetChanged(); } } catch (Exception e) { e.printStackTrace(); } }@Override
public void run() { // TODO Auto-generated method stub doGet(); } }}
<?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" ><RelativeLayout
android:id="@+id/ly_class_topbar" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/white" ><RadioButton
android:id="@+id/rbtn_class_add" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerInParent="true" android:button="@null" android:clickable="true" android:focusableInTouchMode="true" android:drawablePadding="3dp" android:drawableRight="@drawable/tab_menu_recomm" android:gravity="center" android:padding="5dp" android:text="@string/message" android:textSize="18sp" /><View
android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@color/lightgray" /> </RelativeLayout><ListView
android:id="@+id/lv_class_listview" android:layout_width="match_parent" android:layout_height="match_parent" android:dividerHeight="15dip" android:scrollbars="none" android:divider="@color/transparent" android:layout_margin="16dip" > </ListView></LinearLayout>