KeyPoints:
- uses-permission
- Intent Bundle
- AsyncTask
- SimpleAdapter
AndroidManifest.xml
Book
public class Book extends HashMap {
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 readBook(String url)
{ List book = new ArrayList();
try {
JSONArray a = JSONParser.getJSONArrayFromUrl(url);
for (int i = 0; i
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>() {
@Override
protected List doInBackground(String... params) {
return Book.readBook(params[0]);
}
@Override
protected void onPostExecute(List 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 book = (HashMap) 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 adapter = new ArrayAdapter(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 {
private List items;
int resource; //layout
public MyAdapter(Context context, int resource, List 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() {
@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>() {
@Override
protected List doInBackground(Void... params) {
return Employee.list();
}
@Override
protected void onPostExecute(List 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);
}
}