gpt4 book ai didi

android - 弹出窗口中的ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:57 27 4
gpt4 key购买 nike

我正在制作一个应用程序并为 ListView 使用自定义适配器。它正在 Activity 中工作

public class Adapter extends ArrayAdapter {
Context mContext;
int resourceID;
ArrayList<String> names;
public Adapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
this.mContext = context;
this.resourceID=resource;
this.names= objects;
}

@Override
public String getItem(int position) {
return names.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(resourceID, parent, false);

TextView text = (TextView) row.findViewById(R.id.text);

text.setText(names.get(position));
return row;
}

}

我使用此代码使它们出现在 Activity 中

    myNames= (ListView) findViewById(R.id.List);
adapter = new Adapter(this,R.layout.names_view, Current.Names);
myNames.setAdapter(adapter);

现在我想点击一个按钮使相同的列表出现在弹出窗口中,有什么帮助吗?

最佳答案

您可以执行以下操作:

1) 在点击按钮时创建自定义对话框:

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

@Override
public void onClick(View v) {

final Dialog dialog = new Dialog(YourActivity.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Title...");
myNames= (ListView) dialog.findViewById(R.id.List);
adapter = new Adapter(YourActivity.this,R.layout.names_view, Current.Names);
myNames.setAdapter(adapter);
dialog.show();

}
});

2)在对话框布局(custom_dialog.xml)中添加 ListView :

<LinearLayout 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:orientation="vertical">

<ListView
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

关于android - 弹出窗口中的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42458460/

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