本文主要是介绍ListView 显示两列并添加监听(Button 控件除外),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于项目需要,查阅资料自己做的demo,总共两个类,两个布局文件2.Adapterpackage com.example.sqlitelistview;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter;public class MainActivity extends Activity { private ListView listView = null; private List<String> nameList = null; private List<String> ageList = null; private MyAdapter adapter = null; // private List<HashMap<String, String>> list= null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list); adapter = new MyAdapter(this, getNameList(), getAgeList()); listView.setAdapter(adapter); // listView.setAdapter(getAdapter()); //设置监听 // listView.setOnItemClickListener(new OnItemClickListener() { // public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { // } // }); } public List<String> getAgeList(){ ageList = new ArrayList<String>(); for(int i = 1 ; i <= 20 ; i ++){ ageList.add(String.valueOf(i)); } return ageList; } public List<String> getNameList(){ nameList = new ArrayList<String>(); String firstName = "张"; for(int i = 1 ; i <= 20 ; i ++){ nameList.add(firstName + i); } return nameList; } // public List<HashMap<String,String>> getData(){ // HashMap<String, String> userMap = null; // list = new ArrayList<HashMap<String,String>>(); // String name = "张"; // for(int i = 22 ; i < 26 ; i ++){ // userMap = new HashMap<String, String>(); // userMap.put("name",name + i ); // userMap.put("age",String.valueOf(i) ); // list.add(userMap); // } // return list; // } // public SimpleAdapter getAdapter(){ // return new SimpleAdapter(getApplicationContext(), getData(), R.layout.adapter_list, new String[]{"name","age"}, new int[]{R.id.name,R.id.age}); // // } }
package com.example.sqlitelistview;import java.util.ArrayList; import java.util.List;import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast;public class MyAdapter extends BaseAdapter { private Listener listener = null; private Context context; private List<String> name = new ArrayList<String>(); private List<String> age = new ArrayList<String>(); private LayoutInflater inflater; public MyAdapter(Context context,List<String> name, List<String> age){ this.context = context; this.name = name; this.age = age; inflater=LayoutInflater.from(context); listener = new Listener(); } public int getCount() { return name.size(); } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = inflater.inflate(R.layout.adapter_list, null); } TextView nameView = (TextView) convertView.findViewById(R.id.name); TextView ageView = (TextView) convertView.findViewById(R.id.age); nameView.setText(name.get(position)); ageView.setText(age.get(position)); //设置监听 nameView.setOnClickListener(listener); ageView.setOnClickListener(listener); return convertView; } class Listener implements OnClickListener{ public void onClick(View v) { TextView view = (TextView) v; switch(v.getId()){ case R.id.name: Toast.makeText(context, view.getText(), 0).show(); break; case R.id.age: Toast.makeText(context, view.getText(), 0).show(); break; } } } }
3.layout document
4.ListView每列布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" ></ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:textSize="12pt" android:clickable="true" ></TextView> <TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:textSize="12pt" android:clickable="true" > </TextView></LinearLayout>
这篇关于ListView 显示两列并添加监听(Button 控件除外)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!