首页 > 其他分享 >ListView和各种Adapter的使用

ListView和各种Adapter的使用

时间:2023-03-20 10:04:29浏览次数:40  
标签:各种 layout Adapter public new import android ListView id


1,和最简单的ArrayAdapter的使用:

package com.wt.app;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class App extends ListActivity {
String[] array={/*"aaa","bbb","ccc"*/};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//为空时的显示内容
this.getListView().setEmptyView(findViewById(R.id.empty));

ListAdapter adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,array);
this.setListAdapter(adapter);
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Nothing"
/>
</LinearLayout>




2,和SimpleAdapter的使用:


package com.wt.app;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class App extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定Layout里面的ListView
ListView list = (ListView) findViewById(R.id.ListView01);

//生成动态数组,加入数据
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
int[] images=new int[]{android.R.drawable.ic_menu_add,android.R.drawable.ic_menu_delete,android.R.drawable.ic_menu_edit,android.R.drawable.ic_menu_view};
for(int i=0;i<4;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemImage", images[i]);//图像资源的ID
map.put("itemTitle", "Title "+i);
map.put("itemText", "this is Text "+i);
listItem.add(map);
}
//生成适配器的Item和动态数组对应的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源
R.layout.row,//ListItem的XML实现
//动态数组与ImageItem对应的子项
new String[] {"itemImage","itemTitle", "itemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.itemImage,R.id.itemTitle,R.id.itemText}
);

//添加并且显示
list.setAdapter(listItemAdapter);
//添加点击
list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
}
});

//添加长按点击
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长按菜单-ContextMenu");
menu.add(0, 0, 0, "弹出长按菜单0");
menu.add(0, 1, 0, "弹出长按菜单1");
}
});
}

//长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
return super.onContextItemSelected(item);
}
}



main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView01"
/>
</LinearLayout>\

row.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingTop="4dip"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView
android:paddingTop="12dip"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemImage"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="fill_parent"
android:id="@+id/itemTitle"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/itemTitle"
android:id="@+id/itemText"
/>
</RelativeLayout>

3,和SimpleCursorAdapter的使用:


package com.wt.app;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.telephony.PhoneNumberUtils;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class App extends Activity {
private static final String TAG="App";
ListView listView;
ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(Color.BLACK);
LinearLayout.LayoutParams param=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

listView=new ListView(this);
listView.setBackgroundColor(Color.BLACK);

linearLayout.addView(listView,param);

this.setContentView(linearLayout);

//从数据库获取联系人姓名和电话号码
Cursor cur=this.getContentResolver().query(People.CONTENT_URI,null, null,null,null);
adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cur,new String[]{People.NAME,People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});
this.startManagingCursor(cur);
listView.setAdapter(adapter);
//listView.setEmptyView(findViewById(R.id.empty));

listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//openToast("滚动到:"+arg0.getSelectedItemId());
//短信发送
// PendingIntent pi = PendingIntent.getActivity(App.this,0,new Intent(App.this,App.class),0);
// SmsManager sms = SmsManager.getDefault();
// sms.sendTextMessage("5554", null, "message", pi, null);
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
// String[] names=((CursorWrapper)listView.getItemAtPosition(position)).getColumnNames();
//从指针的封装类中获得选中项的电话号码并拨号
CursorWrapper wrapper=(CursorWrapper)listView.getItemAtPosition(position);
int columnIndex=wrapper.getColumnIndex(People.NUMBER);
if(!wrapper.isNull(columnIndex)){
String number=wrapper.getString(columnIndex);
Log.d(TAG,"number="+number);
// //判断电话号码的有效性
if(PhoneNumberUtils.isGlobalPhoneNumber(number)){
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel://"+ number));
startActivity(intent);
}
}
/*
//直接拨号,需要CALL_PHONE权限
Intent intent=new Intent(Intent.ACTION_CALL);
Cursor c=(Cursor)adapter.getItem(position);
long phoneID=c.getLong(c.getColumnIndex(People.PRIMARY_PHONE_ID));
intent.setData(ContentUris.withAppendedId(android.provider.
Contacts.Phones.CONTENT_URI, phoneID));
startActivity(intent);
*/

}
});
//添加长按点击,弹出快显菜单
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
menu.setHeaderTitle("你想要...?").setHeaderIcon(R.drawable.icon);
menu.add(0, 0, 0, "删除").setIcon(R.drawable.icon);
menu.add(0, 1, 0, "修改").setIcon(R.drawable.icon);
}
});
}
//长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
//setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
openToast("你点击了"+item.getTitle());
return super.onContextItemSelected(item);
}
private void openToast(String str){
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
}



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wt.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".App"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 点击拨号时,询问调用默认的程序还是调用本程序拨号 -->
<intent-filter>
<action android:name="android.Intent.Action.CALL_BUTTON" />
<category android:name="android.Intent.Category.DEFAULT" />
</intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-sdk android:minSdkVersion="3" />

</manifest>

标签:各种,layout,Adapter,public,new,import,android,ListView,id
From: https://blog.51cto.com/u_5454003/6131806

相关文章

  • 命名规范——总结各种场景
    **TableSpace<表空间分类>(_数据分类)<_用途><_8K01>**表空间分类:ReGular=TSRGUserTemp=TSUTSystemTemp=TSSTLarGe=TSLG数据分类:CUSTOMER=CUST......
  • STM32F407 学习 (0) 各种外设功能
      本文对正点原子STM32F4探索者的基本功能及外设作最基本的介绍,随笔者本人的学习进程(基本按照正点原子)而不定时更新,起到总结的作用。一、HAL库编写程序的运行逻辑  ......
  • Qt音视频开发26-监控画面各种图形绘制设计
    一、前言视频监控系统做到后面,逐渐需要搭配人工智能算法,将算法计算后的信息以OSD标签以及方框各种图形的信息显示到视频中,这种当然和OSD一样也是有两种方式,一种是源头就贴......
  • sql join on和不用join区别_图解 SQL 各种 JOIN,太有用了!
    原文地址:https://blog.csdn.net/weixin_39965283/article/details/111390490  前言在各种问答社区里谈及SQL里的各种JOIN之间的区别时,最被广为引用的是CodeProj......
  • 【Android开发】基本组件-ListView(重要)
    1.ListView的样子打开任意一款Android手机的“设置”选项,你所看到的效果就是ListView的效果。类似下图:2.详细剖析ListViewListView界面的每一行就是ListView的一个“条目”......
  • css 各种居中
    1.内部容器居中flex<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>文档标题</title></head><style>body{width:100vw;height:100vh;}.......
  • k8s(Kubernetes)中yaml文件的各种 kind 类型
    k8syaml中文件内容一般有kind类型之分,每种类型有不同的功能(一般用---符号隔开)常见的kind类型1、EndpointsEndpoints可以把外部的链接到k8s系统中(可以理解为引用外部资......
  • nginx中proxy_pass各种用法详解
    代理转发规则nginx中配置location代理转发规则的时候不同写法对应不同转发规则。如果proxy_pass使用了URI,当传送请求到后端服务器时,规范化以后的请求路径与配置中的路径......
  • 安卓 listView 组件的使用
    xml<ListViewandroid:id="@+id/list1"android:layout_width="wrap_content"android:layout_height="wrap_content"/>javaListViewlistView1=(ListView)......
  • WPF ItemsControl/ListBox/ListView 控件样式
    一、前言ItemsControl、ListBox、ListView这三种控件在WPF中都可作为列表信息展示控件。我们可以通过修改这三个控件的样式来展示我们的列表信息。既然都是展示列表信息......