Android實現(xiàn)購物商城
本文實例為大家分享了Android實現(xiàn)購物商城的具體代碼,供大家參考,具體內(nèi)容如下
activity_main.xml
<ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:listSelector="#B5DCFA"> </ListView>
listview2.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv" android:layout_width="120dp" android:layout_height="90dp" android:background="@drawable/table" android:layout_marginRight="10dp"> </ImageView> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/iv" android:text="桌子" android:textSize="20dp" android:layout_marginTop="10dp"> </TextView> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv" android:layout_below="@id/title" android:text="價格: " android:textSize="15dp" android:textColor="#FF8F03" android:layout_marginTop="15dp"> </TextView> <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/price" android:layout_toRightOf="@id/price" android:textSize="15dp" android:text="1000" android:textColor="#FF8F03"> </TextView> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { private ListView listView; //ListView控件 //商品名稱、價格、圖片集合 private String[] titles={"桌子","蘋果","蛋糕","線衣","獼猴桃","圍巾"}; private String[] prices={"1800元","10元/kg","300元","350元","10元/kg","280元"}; private int[] icons={R.drawable.table,R.drawable.apple,R.drawable.cake,R.drawable.wireclothes,R.drawable.kiwifruit,R.drawable.scarf}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=findViewById(R.id.lv); //獲取ListView控件 MallAdapter adapter=new MallAdapter(); //創(chuàng)建一個Adapter實例 listView.setAdapter(adapter); //設(shè)置adapter,將適配器指定給ListView對象 } //創(chuàng)建一個MallAdapter類繼承自BaseAdapter類,并重寫類中的一些方法 public class MallAdapter extends BaseAdapter { @Override public int getCount() { //獲取item條數(shù) return titles.length; //返回ListView Item條目的總數(shù) } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override /* position:當(dāng)前的item的位置 convertView:指定的單元格布局 parent:用于加載xml布局 */ public View getView(int position, View convertView, ViewGroup parent) { if (convertView==null){ //通過inflate()方法加載列表條目的布局文件 convertView=View.inflate(MainActivity.this,R.layout.listview2,null); } //獲取列表條目上的控件 TextView title=convertView.findViewById(R.id.title); TextView price=convertView.findViewById(R.id.count); ImageView iv=convertView.findViewById(R.id.iv); //設(shè)置界面上的文本圖片和數(shù)據(jù)信息 title.setText(titles[position]); price.setText(prices[position]); iv.setBackgroundResource(icons[position]); return convertView; } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
flutter監(jiān)聽app進入前后臺狀態(tài)的實現(xiàn)
在開發(fā)app的過程中,我們經(jīng)常需要知道app處于前后臺的狀態(tài),本文主要介紹了flutter監(jiān)聽app進入前后臺狀態(tài)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Android設(shè)計模式之代理模式Proxy淺顯易懂的詳細(xì)說明
Android設(shè)計模式之代理模式也是平時比較常用的設(shè)計模式之一,代理模式其實就是提供了一個新的對象,實現(xiàn)了對真實對象的操作,或成為真實對象的替身2018-03-03Android自定義View實現(xiàn)跟隨手指移動的小兔子
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)跟隨手指移動的小兔子,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11Android開發(fā)使用HttpURLConnection進行網(wǎng)絡(luò)編程詳解【附源碼下載】
這篇文章主要介紹了Android開發(fā)使用HttpURLConnection進行網(wǎng)絡(luò)編程的方法,結(jié)合實例形式分析了Android基于HttpURLConnection實現(xiàn)顯示圖片與文本功能,涉及Android布局、文本解析、數(shù)據(jù)傳輸、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android開發(fā)基礎(chǔ)實現(xiàn)最簡單的視頻播放示例
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實現(xiàn)最簡單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02強制Android應(yīng)用使用某個Locale的方法
這篇文章主要介紹了強制Android應(yīng)用使用某個Locale的方法,涉及Android基于Locale進行語言設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-10-10