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);
}
}
网友评论