首页 新闻 会员 周边

有两个表book表和bookType表,book表中有一个外键bookTypeId,我想做一个bookType的listView

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-06-29 09:40

有两个表book表和bookType表,book表中有一个外键bookTypeId,我想做一个bookType的listView,并且每一项点击之后都能查到相对应的书籍,应该怎么做啊?需要servlet并且解析XML?急用!

miss_bo的主页 miss_bo | 初学一级 | 园豆:171
提问于:2012-06-27 09:23
< >
分享
所有回答(2)
0

就是把listview中查询出bookType的数据,点击listview中的项时,根据点击的值查询出book表中的数据。

悟行 | 园豆:12559 (专家六级) | 2012-06-27 14:28

原理上这样的,现在呢?我是做出来了,但是出错,总是报无效索引2,大小为0,我现在都纠结了

支持(0) 反对(0) miss_bo | 园豆:171 (初学一级) | 2012-06-27 15:21
0

public class OrdersListActivity extends Activity {
private ListView mListView;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orders_layout);
intent = getIntent();
mListView = (ListView) findViewById(R.id.myList);
new NetworkPostAsyncTask().execute("http://192.168.8.14:8080/MyOrders/booktype");

}

class NetworkPostAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
return getPostResult(params[0]);
}

@Override
protected void onPostExecute(String result) {
if (!result.equals("fail")) {

MyAdapter adapter = new MyAdapter(getDate());
mListView.setAdapter(adapter);
}
super.onPostExecute(result);
}
}

private String getPostResult(String urlStr) {
String result = null;
String id = String.valueOf(intent.getIntExtra("id", 0));
HttpPost request = new HttpPost(urlStr);
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair param1 = new BasicNameValuePair("id", id);
params.add(param1);
try {
HttpEntity paramEntity = new UrlEncodedFormEntity(params, "gbk");
request.setEntity(paramEntity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity resultEntity = response.getEntity();
result = EntityUtils.toString(resultEntity, "gbk");
File file = new File("/mnt/sdcard/book.xml");
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file)));
if (result != null) {
bw.write(result);
}
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
Log.e("TAG", "" + e.toString());
}
return result;
}
public void addbook(){
Book book = new Book();
book.setPic(intent.getStringExtra("pic"));
book.setName(intent.getStringExtra("bookName"));
book.setMiaoshu(intent.getStringExtra("miaoshu"));
MyApplication ma = (MyApplication) this.getApplication();
ma.addbook(book);
}
private List<Book> getDate() {
List<Book> books = new ArrayList<Book>();
XmlPullParser xpp = Xml.newPullParser();
try {
FileInputStream fis = new FileInputStream("/mnt/sdcard/book.xml");
xpp.setInput(fis, "utf-8");
int event = xpp.getEventType();
Book book = null;
String mTagName = "";
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:

break;
case XmlPullParser.START_TAG:
mTagName = xpp.getName();
if ("book".equals(mTagName)) {
book = new Book();
book.setId(Integer.valueOf(xpp.getAttributeValue(0)));
Log.d("TAG", "bookID:----"+String.valueOf(book.getId()));
}
if (book != null) {
if ("price".equals(mTagName)) {
book.setPrice(xpp.getAttributeValue(0));
Log.d("TAG", book.getPrice());
} else if ("pic".equals(mTagName)) {
book.setPic(xpp.getAttributeValue(0));
Log.d("TAG", book.getPic());
} else if("author".equals(mTagName)){
book.setAuthor(xpp.getAttributeValue(0));
Log.d("TAG", book.getAuthor());
}else if ("btype".equals(mTagName)) {
book.setBtype(Integer.valueOf(xpp
.getAttributeValue(0)));
Log.d("TAG", "typeID:+++++"+String.valueOf(book.getBtype()));
}else if("miaoshu".equals(mTagName)){
book.setMiaoshu(xpp.getAttributeValue(0));
Log.i("TAG", "描述:"+book.getMiaoshu());
}
}
break;
case XmlPullParser.END_TAG:
mTagName = xpp.getName();
if ("book".equals(mTagName)) {
books.add(book);
book = null;
}
mTagName = "";
break;
case XmlPullParser.TEXT:
if (book != null) {
if ("name".equals(mTagName)) {
book.setName(xpp.getText());
Log.d("TAG", book.getName());
}
}
break;
}
event = xpp.next();
}
} catch (Exception e) {
Log.e("TAG", "" + e.getMessage());
e.printStackTrace();
}
Log.d("TAG", "dlll books.size: " + books.size());
return books;
}

class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Book> mBooks ;
public MyAdapter(List<Book> books) {
mInflater = getLayoutInflater();
mBooks = books;
}

@Override
public int getCount() {
return mBooks.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView( final int position, View convertView,
ViewGroup parent) {
View view = null;
if (convertView != null) {
view = convertView;
} else {
view = mInflater.inflate(R.layout.orders_item, parent, false);
}
final List<Book> books = mBooks;
Log.d("TAG", "books.size: " + books.size());
final ImageView pics = (ImageView) view.findViewById(R.id.pic);
final TextView booknames = (TextView) view
.findViewById(R.id.bookname);
final TextView price = (TextView) view.findViewById(R.id.price);
final TextView author = (TextView) view.findViewById(R.id.bookauthor);
final TextView miaoshu = (TextView) view .findViewById(R.id.miaoshu);
Button bt1 = (Button) view.findViewById(R.id.xiangxi);
// Button bt2 = (Button) view.findViewById(R.id.dinggou);
class MyAsync extends AsyncTask<String, String, Bitmap> {

@Override
protected Bitmap doInBackground(String... params) {
HttpURLConnection conn = null;
InputStream is = null;
Bitmap result = null;
try {
URL url = new URL(params[0]);
conn = (HttpURLConnection) url.openConnection();
conn.connect();
is = conn.getInputStream();
result = BitmapFactory.decodeStream(is);
} catch (Exception e) {
Log.e("TAG", e.toString());
} finally {
try {
if (is != null) {
is.close();
is = null;
}
conn.disconnect();
} catch (IOException e) {
Log.e("TAG", " " + e.getMessage());
}

}
return result;
}

@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
pics.setImageBitmap(result);
} else {
Toast.makeText(OrdersListActivity.this, "获得异常",
Toast.LENGTH_SHORT).show();
}
super.onPostExecute(result);
}
}
new MyAsync().execute(books.get(position).getPic());
booknames.setText(books.get(position).getName());
price.setText(books.get(position).getPrice());
author.setText(books.get(position).getAuthor());
miaoshu.setText(books.get(position).getMiaoshu());
bt1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
intent = new Intent(OrdersListActivity.this,
XiangXiActivity.class);
intent.putExtra("bookName", booknames.getText().toString());
intent.putExtra("price", price.getText().toString());
intent.putExtra("pic", books.get(position).getPic());
intent.putExtra("author", books.get(position).getAuthor());
intent.putExtra("miaoshu", miaoshu.getText().toString());
startActivity(intent);
}
});
return view;
}

}

miss_bo | 园豆:171 (初学一级) | 2012-06-29 09:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册