gpt4 book ai didi

android - 自定义 android 对话框看起来很奇怪

转载 作者:行者123 更新时间:2023-11-30 00:59:39 25 4
gpt4 key购买 nike

我试图创建一个对话框,其中包含 listView 和两个 editText

对话代码

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
alertDialog.setView(convertView);
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
EditText username = (EditText)convertView.findViewById(R.id.username);
EditText pass = (EditText) convertView.findViewById(R.id.password);
ArrayList<ToDoList> search = new ArrayList<ToDoList>();
DialogAdapter adapter=new DialogAdapter(getApplication(),search,lv,username,pass);
lv.setAdapter(adapter);
alertDialog.show();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

对话框适配器

public class DialogAdapter extends BaseAdapter {

Context context;
private static ArrayList<ToDoList> search;
private LayoutInflater mInflater;
ListView listview;
EditText User,Pass;

public DialogAdapter(Context context, ArrayList<ToDoList> searchList,ListView listview,EditText user,EditText pass) {
search=searchList;
this.listview=listview;
this.User= user;
this.Pass=pass;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return search.size();
}

public ToDoList getItem(int position) {
return search.get(position);
}

public long getItemId(int position) {
return 0;
}


public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.dialog_with_edittext, null);
holder= new ViewHolder();
holder.name = (EditText) convertView.findViewById(R.id.username);
holder.pass = (EditText) convertView.findViewById(R.id.password);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.name.setText(search.get(position).getDate());
holder.pass.setText(search.get(position).getTask());
return convertView;
}

static class ViewHolder {
EditText pass;
EditText name;

}
}

待办事项

public class ToDoList {

private String task = "";
private String date = "";

public ToDoList(String task)
{
this.task= task;
}

public void setTask(String task) {
this.task = task;
}

public String getTask() {
return task;
}

public void setDate(String date) {
this.date = date;
}

public String getDate() {
return task;
}
}

自定义对话框

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</ListView>

dialog_with_edittext

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Add new task" />

<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="Summary"/>
</LinearLayout>

快照

enter image description here

对话框看起来很奇怪。这里出了什么问题?谢谢。

最佳答案

首先,没有任何显示的主要原因是因为您的 ArrayList<ToDoList>没有元素。

在适配器的构造函数中,您不需要传递 editTexts , convertView在您的适配器中将确保为您提供引用。

所以这是修改后的适配器类:

public class DialogAdapter extends BaseAdapter {

private static ArrayList<ToDoList> search;
ListView listview;
Context mContext;


public DialogAdapter(Context context, ArrayList<ToDoList> searchList, ListView listview) {
search=searchList;
this.listview=listview;
this.mContext = context;

}
@Override
public int getCount() {
return search.size();
}

@Override
public ToDoList getItem(int position) {
return search.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder= new ViewHolder();
if (convertView == null) {

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.dialog_with_edittext, parent,false);

holder.name = (EditText) convertView.findViewById(R.id.username);
holder.pass = (EditText) convertView.findViewById(R.id.password);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(search.get(position).getDate());
holder.pass.setText(search.get(position).getTask());
return convertView;
}

static class ViewHolder {
EditText pass;
EditText name;

}
}

对于您的对话框,请尝试使用类 Dialog ,这样您就可以更轻松地对其进行自定义:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialog = new Dialog(this, android.R.style.Theme_Material_Dialog);

} else {
dialog = new Dialog(this);
}
dialog.setContentView(R.layout.custom_dialog);

ListView lv = (ListView) dialog.findViewById(R.id.listView1);


ArrayList<ToDoList> search = new ArrayList<>();
search.add(0,new ToDoList("example")); //populate your list here

DialogAdapter adapter=new DialogAdapter(this,search,lv);
lv.setAdapter(adapter);

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = 800;
params.height = 800; //create the window size however you like
dialog.getWindow().setAttributes(params);
dialog.setTitle("My dialog");
dialog.show();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

关于android - 自定义 android 对话框看起来很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599917/

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