AndroidSamplePaper

KeyPoints:

  • uses-permission
  • Intent Bundle
  • AsyncTask
  • SimpleAdapter

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.leo.myandroidexam1">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
……

Book

public class Book extends HashMap<String,String> {
    private static final long serialVersionUID = 7701355780902549134L;
    public Book(String title, String author, String review, String price)
    { put("title", title);
        put("author", author);
        put("review", review);
        put("price", price);
    }
    public static List<Book> readBook(String url)
    { List<Book> book = new ArrayList<Book>();
        try {
            JSONArray a = JSONParser.getJSONArrayFromUrl(url);
            for (int i = 0; i<a.length(); i++) {
                JSONObject b = a.getJSONObject(i);
                book.add(new Book(b.getString("Title"),
                        b.getString("Author"),
                        b.getString("Review"),
                        Double.toString(b.getDouble("Price"))));
            }
        } catch (Exception e) {
            Log.e("JSONArray error", e.toString()); }
        return book;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);
        final EditText et = (EditText) findViewById(R.id.editText1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(this,ListBooks.class);
                Intent intent = new Intent(getApplicationContext(), ListBooks.class);
                String target = et.getText().toString();
                intent.putExtra("target", target);
                startActivity(intent);
            }

        });
    }
}

ListBooks

public class ListBooks extends ListActivity {

    static String URL = "http://172.17.251.222/books/Search/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String target = getIntent().getExtras().getString("target");

        new AsyncTask<String, Void, List<Book>>() {
            @Override
            protected List<Book> doInBackground(String... params) {
                return Book.readBook(params[0]);
            }

            @Override
            protected void onPostExecute(List<Book> result) {
                SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), result,
                        android.R.layout.simple_list_item_2,
                        new String[]{"title", "author"},
                        new int[]{android.R.id.text1, android.R.id.text2});
                setListAdapter(adapter);
            }
        }.execute(URL + target);

    }

    protected void onListItemClick(ListView l, View v,
                                   int position, long id) {
        Book item = (Book) getListAdapter().getItem(position);
        Intent i = new Intent(this, Review.class);
        i.putExtra("Book", item);
        startActivity(i);
    }
}

Review

public class Review extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review);

        TextView tv = (TextView)findViewById(R.id.textView1);
        HashMap<String, String> book = (HashMap<String, String>) getIntent().getExtras().get("Book");

        tv.setText((String) book.get("decription"));
    }
}

拓展

ArrayAdapter

public class MainActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] values = {"Humour", "History", "Children", "Fiction", "Romance", "Cooking"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }
    
    protected void onListItemClick(ListView l, View v,
                                   int position, long id) {
        String category = (String) getListAdapter().getItem(position);
        Intent intent = new Intent(this, BooklistActivity.class);
        intent.putExtra("Category", category);
        startActivity(intent);
    }
}

MyAdapter

public class MyAdapter extends ArrayAdapter<String> {

    private List<String> items;
    int resource; //layout

    public MyAdapter(Context context, int resource, List<String> items) {
        super(context, resource, items);
        this.resource = resource;  //for every row
         this.items = items;
    }

    @Override  //?time to form list/row adapter for list ot use
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(resource, null);  //inflator take layout
        final String eid = items.get(position);
        if (eid != null) {
            TextView e = (TextView) v.findViewById(R.id.textView); //hold id
            e.setText(eid);
            //set
            final ImageView image = (ImageView) v.findViewById(R.id.imageView2); //hold tomnier of the pic
            new AsyncTask<Void, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(Void... params) {
                    return Employee.getPhoto(false,eid);
                }
                @Override
                protected void onPostExecute(Bitmap result) {
                    image.setImageBitmap(result);
                }
            }.execute();
        }//what you have in the row
        //evevry things else is generic based on that your layout is definde
        return v;  //return this view have in side is filled above
    }
}

MainActivity

public class MainActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);  //

        new AsyncTask<Void, Void, List<String>>() {
            @Override
            protected List<String> doInBackground(Void... params) {
                return Employee.list();
            }
            @Override
            protected void onPostExecute(List<String> result) {
                MyAdapter adapter = new MyAdapter(MainActivity.this, R.layout.row, result);
                setListAdapter(adapter);
            }
        }.execute();

    }

    @Override
    protected void onListItemClick(ListView l, View v,
                                   int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        Intent intent = new Intent(this, EmpDetailsActivity.class);
        intent.putExtra("eid", item);  //pass id
        startActivity(intent);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末者祖,一起剝皮案震驚了整個(gè)濱河市印颤,隨后出現(xiàn)的幾起案子到踏,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件即碗,死亡現(xiàn)場離奇詭異男应,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)嗤朴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門配椭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人雹姊,你說我怎么就攤上這事股缸。” “怎么了吱雏?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵敦姻,是天一觀的道長。 經(jīng)常有香客問我歧杏,道長镰惦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任犬绒,我火速辦了婚禮旺入,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凯力。我一直安慰自己茵瘾,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布咐鹤。 她就那樣靜靜地躺著拗秘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪祈惶。 梳的紋絲不亂的頭發(fā)上雕旨,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天扮匠,我揣著相機(jī)與錄音,去河邊找鬼奸腺。 笑死餐禁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的突照。 我是一名探鬼主播帮非,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼讹蘑!你這毒婦竟也來了末盔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤座慰,失蹤者是張志新(化名)和其女友劉穎陨舱,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體版仔,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡游盲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蛮粮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片益缎。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖然想,靈堂內(nèi)的尸體忽然破棺而出莺奔,到底是詐尸還是另有隱情,我是刑警寧澤变泄,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布令哟,位于F島的核電站,受9級特大地震影響妨蛹,放射性物質(zhì)發(fā)生泄漏屏富。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一蛙卤、第九天 我趴在偏房一處隱蔽的房頂上張望狠半。 院中可真熱鬧,春花似錦表窘、人聲如沸典予。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至衣摩,卻和暖如春昂验,著一層夾襖步出監(jiān)牢的瞬間捂敌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工既琴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留占婉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓甫恩,卻偏偏與公主長得像逆济,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子磺箕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容