Android應用中使用GridView實現資料格顯示

來源:互聯網
上載者:User
Android應用中使用GridView實現資料格顯示UsingGridView.java源碼:

package com.sinaapp.ssun.girdview;</p><p>import java.util.*;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.AdapterView;<br />import android.widget.AdapterView.OnItemClickListener;<br />import android.widget.GridView;<br />import android.widget.SimpleAdapter;<br />import android.widget.Toast;</p><p>public class UsingGridViewActivity extends Activity {<br />private GridView gridView;</p><p>@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); </p><p> gridView = (GridView) this.findViewById(R.id.gridView);<br /> show();<br /> } </p><p>private void show() {<br />List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();<br />for(int i=0; i<12; i++){<br />HashMap<String, Object> hm = new HashMap<String, Object>();<br />hm.put("image", R.drawable.ic_launcher);<br />hm.put("name", "NO." + i);<br />data.add(hm);<br />}</p><p>SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,<br /> new String[]{"image","name"},new int[]{R.id.imageView,R.id.textView});</p><p> gridView.setAdapter(adapter);</p><p> gridView.setOnItemClickListener(new OnItemClickListener(){</p><p>@Override<br />public void onItemClick(AdapterView<?> parent, View view,<br />int position, long id) {<br />HashMap<String, Object> hm = (HashMap<String, Object>)parent.getItemAtPosition(position);<br />Toast.makeText(UsingGridViewActivity.this, hm.get("name").toString(), Toast.LENGTH_LONG).show();<br />}});<br />}<br />}

main.xml檔案:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical" ></p><p> <GridView<br /> android:id="@+id/gridView"<br /> android:layout_width="wrap_content"<br /> android:layout_height="match_parent"<br /> android:numColumns="3" ><br /> </GridView></p><p></LinearLayout>

item.xml檔案:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> android:orientation="vertical" ></p><p> <ImageView<br /> android:id="@+id/imageView"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:src="@drawable/ic_launcher"<br /> android:layout_gravity="center_horizontal"/></p><p> <TextView<br /> android:id="@+id/textView"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_gravity="center_horizontal"/></p><p></LinearLayout>
本文章是在參考http://blog.csdn.net/hellogv/article/details/4567095寫的,如果沒有那篇文章,可能今天晚上又沒得睡了,謝謝他了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.