gpt4 book ai didi

android - AlertDialog中的ListView重复出现

转载 作者:行者123 更新时间:2023-11-29 13:59:08 25 4
gpt4 key购买 nike

我的 AlertDialog 包含一个 ListView。当 AlertDialog 被关闭并返回时,ListView 出现两次。我不知道为什么会这样。这是我的代码:

protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(LegendTest.this);
final LayoutInflater inflater = (LayoutInflater) Legend`enter code here`Test.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.selectfeaturetype, null);
ListView listView = (ListView) view.findViewById(R.id.listView1);
Dialog dialog;

switch (id) {
case LEGEND_DIALOG:

builder.setTitle("Set legend");
builder.setView(view);


listView.setAdapter(new BaseAdapter() {

@Override
public View getView(int position, View convertView,
ViewGroup parent) {

if(convertView == null)
{
convertView = inflater.inflate(R.layout.itemwithimage, null);
}
MyLegend legendItem = data.get(position);
if(legendItem != null)
{
ImageView img = (ImageView) convertView.findViewById(R.id.listimageView);
img.setImageBitmap(legendItem.legendImage);
TextView txt = (TextView) convertView.findViewById(R.id.listtextView);
txt.setText(legendItem.legendname);
}

return convertView;
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public int getCount() {

if (data == null)
return 0;

return data.size();
}
});

dialog = builder.create();
break;

default:

dialog = null;
break;
}
return dialog.

最佳答案

尝试下面的代码

package at.bartinger.list;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PracticeClas extends Activity {

ArrayList<String> data = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data.add("Hello");
data.add("Hello One");
data.add("Hello Two");
data.add("Hello Three");
data.add("Hello Four");
data.add("Hello Five");
data.add("Hello Six");

showDialog(0);
}

protected Dialog onCreateDialog(int id) {
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.main, null);
ListView listView = (ListView) view.findViewById(R.id.LsOne);
Dialog dialog = null;

dialog = new Dialog(PracticeClas.this);
dialog.setContentView(view);
dialog.setTitle("Material & Labor:");

// dialog.create();
switch (id) {
case 0:
listView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View convertView,
ViewGroup parent) {

if (convertView == null) {
convertView = inflater.inflate(
R.layout.list_item_section, null);
}

TextView txt = (TextView) convertView
.findViewById(R.id.list_item_section_text);
txt.setText(data.get(position));
return convertView;
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public int getCount() {

if (data == null)
return 0;

return data.size();
}
});

dialog.show();
break;

default:
dialog = null;
break;
}
return dialog;
}

}

关于android - AlertDialog中的ListView重复出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com