layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>
main.java
package com.example.day08_listview_listener; import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity { private ListView listView;
public static String[] citys = {"长沙","上海","深圳","广州","北京","广东","杭州","北京","天津","嘉兴","永州","衡阳","西安"};
private List<String> list;
private Myadapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化控件
listView = (ListView) findViewById(R.id.listview);
list = new ArrayList<String>();
for (int i = 0; i < citys.length; i++) {
list.add(citys[i]);
}
adapter = new Myadapter();
//设置适配器
listView.setAdapter(adapter);
//设置item点击事件
listView.setOnItemClickListener(new OnItemClickListener() {
/**
* 当点击适配器控件的item的时候调用
* AdapterView<?> parent,表示的是设置该适配器的适配器控件
* View view,表示的是被点击的item的view
* int position, 表示的是与被点击item的view的索引
* long id 表示id,该值与getItemId的返回值对应
*/
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("MainActivity", "MainActivity--onItemClick--position"+position);
}
});
//设置长按事件
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
/**
* 问题:当用户点击item的时间超过2s就会触发长按事件,如果同时设置了单击事件,会先触发长按,松开手时触发单击事件]
* 返回值 false
* 表示该方法不处理该事件(不消耗该事件),事件会交给其他的方法去处理
* 返回值 true
* 表示该方法消耗了该事件,该事件就不会交给其他的方法去调用
*/
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//获取数据
String dataArray = citys[position];//方法一
String dataArray1 = (String) listView.getItemAtPosition(position);//方法二
String dataArray2 = (String) adapter.getItem(position) ;//方法三
String dataArray4 = (String) parent.getItemAtPosition(position);
Log.i("MainActivity", "MainActivity--onItemLongClick--position"+position);
return true;
}
}); }
class Myadapter extends BaseAdapter{ @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
3.实现getview
// 1.编写复杂样式的item布局
// 2.将xml文件转换成view
// 3.找到view中的控件
// 4.给控件设置值
// 5.返回view //第一种--获得布局加载器
/*LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.listview_item_city, null);*/
//第二种
/*LayoutInflater layoutInflater2 = LayoutInflater.from(MainActivity.this);
View view = layoutInflater2.inflate(R.layout.listview_item_city, null);*/
//第三种
// LayoutInflater layoutInflater = MainActivity.this.getLayoutInflater();
// View view = layoutInflater2.inflate(R.layout.listview_item_city, null);
//第四种
View view = View.inflate(MainActivity.this, R.layout.listview_item_city, null);
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 创建一个TextView 对象,设置参数并返回view
TextView textView = new TextView(MainActivity.this);
textView.setText(list.get(position));
textView.setTextSize(30);
return textView;
} } }