2017-02-25 142 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

太好了,不客氣:) –