如何在ListView项目的对话框中显示自定义消息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在ListView项目的对话框中显示自定义消息相关的知识,希望对你有一定的参考价值。
我正在寻找帮助显示对话框上的消息,通过单击ListView中的项目..示例:
答案
如果您使用某个适配器来填充列表视图
public class DemoListView extends AppCompatActivity implements ListAdapter.PlaceClickInterface {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_list);
List<String> places = new ArrayList<>(Arrays.asList("New york","washington","london"));
listView = (ListView)findViewById(R.id.list_view_demo);
ListAdapter adapter = new ListAdapter(places , this) ;
listView.setAdapter(adapter);
}
@Override
public void onPlaceClick(int position) {
Log.v("DemoListView" , "position : "+position) ;
//show dialog here with Alert dialog builder or ListFragment class with custom UI
}
}
扩展列表适配器,如ArrayAdapter或BaseAdapter:
在此适配器中,创建一个可以在要显示该对话框的Activity(DemoListView)中实现的接口。
在实现的方法onPlaceClick()中,在DemoListView中创建并显示对话框
public class ListAdapter extends BaseAdapter{
List<String> mPlaces ;
Context mContext ;
LayoutInflater mInflater ;
PlaceClickInterface mPlaceClickInterface;
public ListAdapter( List places , DemoListView demoListView) {
this.mContext = demoListView.getBaseContext() ;
this.mPlaces = places ;
this.mInflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mPlaceClickInterface = (PlaceClickInterface) demoListView;
}
@Override
public int getCount() {
return mPlaces.size();
}
@Override
public Object getItem(int position) {
return mPlaces.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_list_data, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.bind(holder ,position);
return convertView ;
}
private class ViewHolder{
TextView places ;
ViewHolder(View view){
this.places = (TextView) view.findViewById(R.id.list_place);
}
// used to populate the list with list item and attach click listener on each item
void bind(ViewHolder holder ,final int position){
holder.places.setText(mPlaces.get(position));
holder.places.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPlaceClickInterface.onPlaceClick(position);
}
});
}
}
public interface PlaceClickInterface {
void onPlaceClick(int position) ;
}
}
另一答案
或许更详细一点(代码片段)可以帮助您回复查询。但我将在这里提供一个通用的解决方案。
在您初始化列表视图的活动/片段上,您可以使用listviews setOnItemClickListener方法并覆盖AdapterViews onItemClick方法以获取列表项位置:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/*listAdapter is the adapter for your listView and ListItem
represents individual item model for the list*/
ListItem listItem= listAdapter.getItem(i);
if(listItem!=null){
//show your dialog message according to listItem here
}
}
});
以上是关于如何在ListView项目的对话框中显示自定义消息的主要内容,如果未能解决你的问题,请参考以下文章