listview 适配器
直到我阅读Lucas Rocha的精彩示例和文章: Android的ListView性能提示,我才对带适配器的Android ListView的工作方式不清楚。
今天,我将向您展示我希望用户在具有ListView的AlertDialog中选择“项目”时使用的代码。 如果您正在寻找一个带有简单ListView(不带适配器)的AlertDialog示例,这是我撰写的博客文章: 具有ListView的Android AlertDialog 。
这里的ListView项包含一个文本(项名称)和一个ID(项ID),因此,只要用户在弹出窗口中选择一个项,您就可以获取所选项的文本和ID。 如果项目来自数据库–具有ID和名称的记录,这将非常有用。 标签(项目ID)是使用TextView的setTag()方法设置的。
影片示范
这是我们今天的代码的最终输出。 取了姓名和身份证,并通过烤面包显示。
代码下载
您可以在此处下载我们使用的代码: ListViewPopUpWithAdapter.zip
让我们编码!
我们只需要6个文件即可成功运行此示例代码。
1. activity_main.xml –我们的主要布局。 显示ListView的触发器是一个按钮。
<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" >
<Button
android:id="@+id/buttonShowPopUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Show AlertDialog with ListView" />
</RelativeLayout>
2. MainActivity.java –将显示主布局,即位于屏幕中心的按钮。 单击该按钮将显示我们很棒的ListView。
package com.example.listviewpopupwithadapter;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.app.Activity;
import android.app.AlertDialog;
public class MainActivity extends Activity {
AlertDialog alertDialogStores;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// a button to show the pop up with a list view
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonShowPopUp:
showPopUp();
break;
}
}
};
findViewById(R.id.buttonShowPopUp).setOnClickListener(handler);
}
public void showPopUp(){
// add your items, this can be done programatically
// your items can be from a database
ObjectItem[] ObjectItemData = new ObjectItem[20];
ObjectItemData[0] = new ObjectItem(91, "Mercury");
ObjectItemData[1] = new ObjectItem(92, "Watson");
ObjectItemData[2] = new ObjectItem(93, "Nissan");
ObjectItemData[3] = new ObjectItem(94, "Puregold");
ObjectItemData[4] = new ObjectItem(95, "SM");
ObjectItemData[5] = new ObjectItem(96, "7 Eleven");
ObjectItemData[6] = new ObjectItem(97, "Ministop");
ObjectItemData[7] = new ObjectItem(98, "Fat Chicken");
ObjectItemData[8] = new ObjectItem(99, "Master Siomai");
ObjectItemData[9] = new ObjectItem(100, "Mang Inasal");
ObjectItemData[10] = new ObjectItem(101, "Mercury 2");
ObjectItemData[11] = new ObjectItem(102, "Watson 2");
ObjectItemData[12] = new ObjectItem(103, "Nissan 2");
ObjectItemData[13] = new ObjectItem(104, "Puregold 2");
ObjectItemData[14] = new ObjectItem(105, "SM 2");
ObjectItemData[15] = new ObjectItem(106, "7 Eleven 2");
ObjectItemData[16] = new ObjectItem(107, "Ministop 2");
ObjectItemData[17] = new ObjectItem(108, "Fat Chicken 2");
ObjectItemData[18] = new ObjectItem(109, "Master Siomai 2");
ObjectItemData[19] = new ObjectItem(110, "Mang Inasal 2");
// our adapter instance
ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, ObjectItemData);
// create a new ListView, set the adapter and item click listener
ListView listViewItems = new ListView(this);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());
// put the ListView in the pop up
alertDialogStores = new AlertDialog.Builder(MainActivity.this)
.setView(listViewItems)
.setTitle("Stores")
.show();
}
}
3. ObjectItem.java –列表中的每个项目都被视为具有名称和ID的对象。
package com.example.listviewpopupwithadapter;
//another class to handle item's id and name
public class ObjectItem {
public int itemId;
public String itemName;
// constructor
public ObjectItem(int itemId, String itemName) {
this.itemId = itemId;
this.itemName = itemName;
}
}
4. ArrayAdapterItem.java –我们的ListView性能取决于。
package com.example.listviewpopupwithadapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
// here's our beautiful adapter
public class ArrayAdapterItem extends ArrayAdapter<ObjectItem> {
Context mContext;
int layoutResourceId;
ObjectItem data[] = null;
public ArrayAdapterItem(Context mContext, int layoutResourceId, ObjectItem[] data) {
super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* The convertView argument is essentially a "ScrapView" as described is Lucas post
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
// object item based on the position
ObjectItem objectItem = data[position];
// get the TextView and then set the text (item name) and tag (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
textViewItem.setText(objectItem.itemName);
textViewItem.setTag(objectItem.itemId);
return convertView;
}
}
5. OnItemClickListenerListViewItem.java –回答如果用户单击列表中的项目会发生什么?
package com.example.listviewpopupwithadapter;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
/*
* Here you can control what to do next when the user selects an item
*/
public class OnItemClickListenerListViewItem implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Context context = view.getContext();
TextView textViewItem = ((TextView) view.findViewById(R.id.textViewItem));
// get the clicked item name
String listItemText = textViewItem.getText().toString();
// get the clicked item ID
String listItemId = textViewItem.getTag().toString();
// just toast it
Toast.makeText(context, "Item: " + listItemText + ", Item ID: " + listItemId, Toast.LENGTH_SHORT).show();
((MainActivity) context).alertDialogStores.cancel();
}
}
6. list_view_row_item.xml –列表中每个项目的布局。 如果要为ListView的每个项目都具有图像或字幕,则可以修改它。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<TextView
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."
android:textSize="15dp" />
</RelativeLayout>
翻译自: https://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html
listview 适配器