2017-02-25 26 views
0

我正在制作一个应用程序并使用ListView的自定义适配器。它是在活动ListView弹出窗口

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; 
    } 

} 

工作,我用这个代码,使他们出现在活动

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

现在我想点击一个按钮,使同一列表出现在弹出窗口中,任何帮帮我?

+0

一个弹出窗口是一个AlertDialog或你是什么意思? –

+0

我的意思是这个类PopupWindow试图做多种方式,但它没有工作 – Esraa

回答

4

你可以做如下:

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):

<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> 
+0

非常感谢你,它的工作,但而不是“这个”我用“Activity.this”和dialog.show()到底 – Esraa

+0

对不起,错误。我纠正了我的答案。如果它有帮助,您可以将其标记为已接受的答案。 – tahsinRupam

+0

完成:),但改变其他这也是 – Esraa

0

可以使用自定义对话框

定制对话框mydialog.xml

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

在你的活动

Dialog dialog = new Dialog(Activity.this); 
    dialog.setContentView(R.layout.mydialog) 
ListView lv = (ListView) dialog.findViewById(R.id.lv); 
dialog.setCancelable(true); 
dialog.setTitle("ListView"); 
dialog.show(); 
+0

dialog.setCancelable(true);做什么? – Esraa

+0

用户不会通过按回退键或在屏幕上点击来解除对话框他只能按下对话框按钮以使其消失 –

+0

这是一个非常好的建议非常感谢你:) – Esraa

1

custom.xml

<?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> 

活动

String names[] ={"A","B","C","D"}; 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 
       LayoutInflater inflater = getLayoutInflater(); 
       View convertView = (View) inflater.inflate(R.layout.custom, null); 
       alertDialog.setView(convertView); 
       alertDialog.setTitle("List"); 
       ListView lv = (ListView) convertView.findViewById(R.id.List); 
       Adapter<String> adapter = new Adapter(this,R.layout.names_view, Current.Names); 
       lv.setAdapter(adapter); 
       alertDialog.show(); 
+0

谢谢它完美的工作,但要有它,当我点击一个按钮,我替换“这个”在“适配器适配器=新的适配器(这,R.layout.names_view,当前。名);”以“MainActivity.this” – Esraa

+0

欢迎...高兴地帮助@ E.Khaled – mahiraj2709

0

我输入 “PopupWindow”进入谷歌,发现这个:

https://www.youtube.com/watch?v=fn5OlqQuOCk

直到现在我还不知道PopupWindow,我建议使用一个对话框,例如具有Costum视图的AlertDialog。

我希望我能帮忙!

+0

非常感谢你的帮助,我使用了Dialog,它确实有效 – Esraa

+0

太好了,不客气:) –