获取listview item 中的数据

获取listview item中的数据

image

点击商品代码“021025001” 获取商品详情

image

代码实现ViewInfoListActivity

1   package   com . stelcom . tcpos;
2  
3   import   java . util . ArrayList;
4   import   java . util . HashMap;
5  
6   import   android . app . ListActivity;
7   import   android . content . Intent;
8   import   android . database . Cursor;
9   import   android . database . sqlite . SQLiteDatabase;
10  import   android . os . Bundle;
11  import   android . util . Log;
12  import   android . view . View;
13 
14  import   android . widget . ListView;
15  import   android . widget . SimpleAdapter;
16 
17  public   class   ViewInfoListActivity   extends   ListActivity   {
18      private   ArrayList < HashMap < String,   String > >   mData;
19 
20      /* *   Called   when   the   activity   is   first   created.   */
21 
22      @Override
23      public   void   onCreate(Bundle   savedInstanceState)   {
24          super . onCreate(savedInstanceState);
25          /*   加载数据   */
26          mData   =   getData();
27          setContentView(R . layout . viewinfolist_activity);
28          SimpleAdapter   listAdapter   =   new   SimpleAdapter( this ,   mData,
29                  R . layout . viewinfolistactivity_list_item,   new   String[]   {
30                          " TOB_CODE " ,   " INPRINCE "   } ,   new   int []   {
31                          R . id . listitem_title,   R . id . listitem_content   } );
32 
33          setListAdapter(listAdapter);
34 
35      }
36 
37      private   ArrayList < HashMap < String,   String > >   getData()   {
38 
39     
40          Tcpos   tcpos   =   (Tcpos)   getApplication();
41          SQLiteDatabase   db   =   tcpos . getTobDatabase();
42 
43          ArrayList < HashMap < String,   String > >   list   =   new   ArrayList < HashMap < String,   String > > ();
44          //   读取数据库    
45 
46          Cursor   cursor   =   db . query( " tob_info " ,   new   String[]   {   " tob_code " ,   " name " ,
47                  " inprice " ,   " manufacture " ,   " outprice "   } ,   null ,   null ,   null ,   null ,
48                  null );
49 
50          while   (cursor . moveToNext())   {
51 
52     
53              String   tob_code   =   cursor . getString(cursor
54                      . getColumnIndex( " tob_code " ));
55              String   name   =   cursor . getString(cursor . getColumnIndex( " name " ));
56              String   manufacture   =   cursor . getString(cursor
57                      . getColumnIndex( " manufacture " ));
58              String   inprice   =   cursor . getString(cursor . getColumnIndex( " inprice " ));
59              int   inprice_int   =   Integer . parseInt(inprice);
60              String   outprice   =   cursor . getString(cursor
61                      . getColumnIndex( " outprice " ));
62              int   outprice_int   =   Integer . parseInt(outprice);
63              System . out . println( " query-inprice-> "   +   inprice);
64             
65              HashMap < String,   String >   map   =   new   HashMap < String,   String > ();
66         
67              map . put( " TOB_CODE " ,   tob_code);
68              map . put( " NAME " ,   name);
69              map . put( " MANUFACTURE " ,   manufacture);
70              double   du_inprince   =   inprice_int   /   100 ;
71              map . put( " INPRINCE " ,   du_inprince   +   " " );
72              double   du_outprince   =   outprice_int   /   100 ;
73              map . put( " OUTPRICE " ,   du_outprince   +   " " );
74              //   库存需要从库存表获取TODO
75              // TODO
76              map . put( " STORE " ,   " 0 " );
77              list . add(map);
78              //   ///
79          }
80          /*   主要cursor使用完成后,需要关闭,否则希望会出LOG   err   */
81          cursor . close();
82 
83          return   list;
84 
85      }
86 
87      @Override
88      protected   void   onListItemClick(ListView   l,   View   v,   int   position,   long   id)   {
89          //   TODO   Auto-generated   method   stub
90          super . onListItemClick(l,   v,   position,   id);
91     
92          //   需要传递当前的数据到显示页面
93          String   tobcode   =   new   String((String)   mData . get(position)
94                  . get( " TOB_CODE " ));
95         
96          //   根据用户点击列表当中的位置来得到响应的tobcode对象
97          Intent   intent   =   new   Intent();
98          intent . setClass(ViewInfoListActivity . this ,
99                  MoreInformationActivity . class );
100         Bundle   bundle   =   new   Bundle();
101         bundle . putString( " KEY_TOBCODE " ,   tobcode);
102         intent . putExtras(bundle);
103
104         startActivity(intent);
105         super . onListItemClick(l,   v,   position,   id);
106
107     }
108
109     @Override
110     protected   void   onDestroy()   {
111         //   TODO   Auto-generated   method   stub
112
113         super . onDestroy();
114     }
115
116 }

代码实现详细信息:

1   package   com . stelcom . tcpos;
2  
3   import   java . util . ArrayList;
4   import   java . util . HashMap;
5  
6  
7   import   android . app . ListActivity;
8   import   android . database . Cursor;
9   import   android . database . sqlite . SQLiteDatabase;
10  import   android . os . Bundle;
11  import   android . util . Log;
12  import   android . view . View;
13  import   android . widget . ListView;
14  import   android . widget . SimpleAdapter;
15 
16 
17 
18 
19 
20 
21  public   class   MoreInformationActivity   extends   ListActivity   {
22      /* *   Called   when   the   activity   is   first   created.   */
23      ArrayList < HashMap < String,   String > >   mList   = null ;
24      @Override
25      public   void   onCreate(Bundle   savedInstanceState)   {
26          super . onCreate(savedInstanceState);
27          setContentView(R . layout . moreinformation_activity);
28          Bundle   bunde   =   this . getIntent() . getExtras();
29          String   tobcode   = new   String(   bunde . getString( " KEY_TOBCODE " ));
30          System . out . println( " more   info   tob: " + tobcode);
31          showmMoreInfo();
32          mList = getData(tobcode);
33 
34          SimpleAdapter   listAdapter   =   new   SimpleAdapter( this ,   mList,
35                  R . layout . moreinfomationlistactivity_list_item,   new   String[]   {
36                          " NAME " ,   " TOB_CODE " ,   " MANUFACTURE " ,   " INPRINCE " ,
37                          " OUTPRICE " ,   " STORE "   } ,   new   int []   {   R . id . tobname,
38                          R . id . tobcode,   R . id . manufacture,   R . id . inprice,
39                          R . id . outprice,   R . id . store   } );
40          setListAdapter(listAdapter);
41     
42 
43         
44 
45      }
46      /* 显示详细信息 */
47      private   void   showmMoreInfo() {
48         
49          /* 通过tobcode   获取信息 */
50         
51         
52      }
53      private   ArrayList < HashMap < String,   String > >   getData(String   querykey)   {
54 
55          //  
56          Tcpos   tcpos   =   (Tcpos)   getApplication();
57          SQLiteDatabase   db   =   tcpos . getTobDatabase();
58          System . out . println( " open   a   database   succ " );
59 
60          ArrayList < HashMap < String,   String > >   list   =   new   ArrayList < HashMap < String,   String > > ();
61          //   读取数据库
62              /* 根据tobcode   =   querykey   条件获取数据 */
63          Cursor   cursor   =   db . query( " tob_info " ,   new   String[] { " tob_code " , " name " , " inprice " , " manufacture " , " outprice " } ,   " tob_code=@1 " , new   String[] { querykey } ,   null ,   null ,   null );
64         
65 
66          while   (cursor . moveToNext())   {
67              String   tob_code   =   cursor . getString(cursor
68                      . getColumnIndex( " tob_code " ));
69              String   name   =   cursor . getString(cursor . getColumnIndex( " name " ));    
70              String   manufacture   =   cursor . getString(cursor . getColumnIndex( " manufacture " ));    
71              // String   name   =   cursor.getString(cursor.getColumnIndex("name"));    
72             
73              String   inprice   =   cursor . getString(cursor . getColumnIndex( " inprice " ));
74              int   inprice_int   =   Integer . parseInt(inprice);
75              String   outprice   =   cursor . getString(cursor . getColumnIndex( " outprice " ));
76              int   outprice_int   =   Integer . parseInt(outprice);
77              System . out . println( " query-inprice-> "   +   inprice);
78 
79              HashMap < String,   String >   map   =   new   HashMap < String,   String > ();
80              //   map.put("PIC",   R.drawable.pic);
81              System . out . print( " tob_code "   +   tob_code);
82              System . out . print( " inprice "   +   inprice);
83 
84              map . put( " TOB_CODE " ,   tob_code);            
85              map . put( " NAME " ,   name);
86              map . put( " MANUFACTURE " ,   manufacture);            
87              double   du_inprince   =   inprice_int   /   100 ;
88              map . put( " INPRINCE " ,     du_inprince   +   " " );
89              double   du_outprince   =   outprice_int   /   100 ;
90              map . put( " OUTPRICE " ,     du_outprince   +   " " );
91              // 库存
92              map . put( " STORE " ,   " 0 " );
93              list . add(map);
94             
95          }
96          /*   主要cursor使用完成后,需要关闭,否则希望会出LOG   err   */
97          cursor . close();
98 
99          return   list;
100
101     }
102    
103
104     @Override
105     protected   void   onListItemClick(ListView   l,   View   v,   int   position,   long   id)   {
106         //   TODO   Auto-generated   method   stub
107         super . onListItemClick(l,   v,   position,   id);
108         System . out . println( " id---------------- "   +   id);
109         System . out . println( " position---------- "   +   position);
110    
111     }
112
113     @Override
114     protected   void   onDestroy()   {
115         //   TODO   Auto-generated   method   stub
116
117         super . onDestroy();
118     }
119
120 }
121

 

中文乱码问题没有解决,从数据库中获取的数据含中文乱码,知道朋友指导一下!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值