新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

androidstudioListview简单实例-创新互联

//layout 布局

成都创新互联公司从2013年开始,先为建昌等服务建站,建昌等地企业,进行企业商务咨询服务。为建昌企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

  android:paddingRight="@dimen/activity_horizontal_margin"

  android:paddingTop="@dimen/activity_vertical_margin"

  android:paddingBottom="@dimen/activity_vertical_margin"

  >

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:id="@android:id/list"

  />

//代码

package com.example.test.testlistview;

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class ListViewArrayadapter extends ListActivity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_list_view_arrayadapter);

    Listls = new ArrayList();

    ls.add("测试数据1");

    ls.add("测试数据2");

    ls.add("测试数据1");

    ls.add("测试数据2");

    ls.add("测试数据1");

    ls.add("测试数据2");

    ls.add("测试数据1");

    ls.add("测试数据2");

    ls.add("测试数据1");

    ls.add("测试数据2");

    ls.add("测试数据1");

    ls.add("测试数据2");

    ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,ls);

    setListAdapter(adapter);  //继承ListActivity才有此函数

  }

  @Override

  protected void onListItemClick(ListView l, View v, int position, long id) {

    String s =((TextView)v).getText().toString();

    Toast.makeText(this,"提示"+position+s,Toast.LENGTH_LONG).show();

    super.onListItemClick(l, v, position, id);

  }

}

//效果图

简单的listview,关键是ArrayAdapter中布局和数据的适配。android.R.layout.simple_list_item_1系统自带的样式,可以根据自己的要求更改布局样式。Listls = new ArrayList() 用于显示的数据。

android studio  Listview简单实例

//SimpleAdapter 适配Listview

//layout

  android:orientation="horizontal" android:layout_width="match_parent"

  android:layout_height="match_parent">

  android:id="@+id/img2"

  android:layout_height="wrap_content"

  android:layout_width="wrap_content"

  />

  

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="vertical">

    

      android:id="@+id/title2"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content" />

    

      android:id="@+id/price"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"/>

  

//关键代码设置listview

 SimpleAdapter adapter1 =new SimpleAdapter(this,lm,R.layout.simple2, new String[]{"title","img","price"},new int[]{R.id.title2,R.id.img2,R.id.price});

 setListAdapter(adapter1);

//得到数据

 public List> getData2()

  {

    Mapvalue =new HashMap();

    List>lm =new ArrayList>();

    value.put("title","苹果");

    value.put("img",R.drawable.aphone);

    value.put("price","价格:5800");

    lm.add(value);

    value =new HashMap();

    value.put("title", "三星");

    value.put("img",R.drawable.sphone);

    value.put("price","价格:4800");

    lm.add(value);

    value =new HashMap();

    value.put("title","华为");

    value.put("img",R.drawable.hphone);

    value.put("price","价格:6000");

    lm.add(value);

    return  lm;

  }

android studio  Listview简单实例

//SimpleCursorAdapter适配Listview 用于数据库数据的显示

//关键代码

 //获得一个指向系统通讯录数据库的Cursor对象获得数据来源

        Cursor cursor = getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);

        startManagingCursor(cursor);

        //实例化列表适配器

        ListAdapter la = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{Contacts.People.NAME,Contacts.People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});

        setListAdapter(la);

//一定要在AndroidManifest.xml加读取联系人的权限。

 

android studio  Listview简单实例

//代码下载

http://down.51cto.com/data/2119105

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:androidstudioListview简单实例-创新互联
转载注明:http://www.wjwzjz.com/article/cdghhd.html
在线咨询
服务热线
服务热线:028-86922220
TOP