2016-05-15 165 views
0

我在alertdialog中使用列表視圖,但是當對話框顯示時,它不顯示任何東西。該對象不是空的,因爲我填充它並將它添加到alertdialog之前的數組列表中。下面有我的XML和類代碼:alertdialog與自定義適配器的列表視圖不顯示

當我設置適配器:

   AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setTitle("Select a match"); 
      //insert array to constructor 
      LayoutInflater inflater = getLayoutInflater(savedInstanceState); 
      View dialogLayout = inflater.inflate(R.layout.dialoglist, null); 
      matchAdapter testAdapter = new matchAdapter(getContext(), userInfos); 
      ListView listView = (ListView) dialogLayout.findViewById(R.id.dialogListView); 
      listView.setAdapter(testAdapter); 
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        //do something 
       } 
      }); 
      builder.setView(dialogLayout); 

      AlertDialog alert = builder.create(); 
      alert.show(); 

自定義適配器:

class matchAdapter extends ArrayAdapter<userInfo> { 

public matchAdapter(Context context, ArrayList<userInfo> test) { 
    super(context, R.layout.match_result, test); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 

    View customRow = inflater.inflate(R.layout.match_result, parent, false); 
    userInfo singleItemTest = getItem(position); 

    /* 
    TODO: get references to layout elements 
    */ 

    TextView username = (TextView) customRow.findViewById(R.id.matchUsername); 
    TextView sex = (TextView) customRow.findViewById(R.id.matchSex); 
    TextView age = (TextView) customRow.findViewById(R.id.matchAge); 
    Button sendRequest = (Button) customRow.findViewById(R.id.matchSendRequest); 

    username.setText(singleItemTest.getUserName()); 

    return customRow; 
}} 

row.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/match_result" 
android:orientation="horizontal" 
android:padding="16dp" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="left"> 
<TextView 
    android:id="@+id/matchUsername" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Username" 
    android:layout_marginRight="10dp"/> 

<TextView 
    android:id="@+id/matchSex" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Sex" 
    android:layout_marginRight="10dp"/> 

<TextView 
    android:id="@+id/matchAge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Age" 
    android:layout_marginRight="10dp"/> 


<Button 
    android:id="@+id/matchSendRequest" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Send Request" 
    android:minHeight="5dp" 
    android:minWidth="0dp" 
    android:layout_marginLeft="80dp"/> 

dialoglist.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/dialogListView" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"> 

</ListView> 

+0

使用片段作爲對話框 – Haroon

+0

您能在代碼中顯示您的意思嗎? – SpecialSnowflake

+0

http://javatechig.com/android/android-dialog-fragment-example以此爲例 – Haroon

回答

0

邁克的ID。 M在他的評論中是正確的。陣列列表確實是空的。但是我現在修復了它。愚蠢的監督我的循環。

0

在適配器中添加一個對象ArrayList<userInfo> infos

現在Constractor使用

public matchAdapter(Context context, ArrayList<userInfo> test) { 
    super(context, R.layout.match_result, test); 
    infos=test; 

} 

現在getView回調將是這樣的:

userInfo singleItemTest = infos.get(position); 

我認爲這將解決您的問題:)

+0

這與'ArrayAdapter'在默認情況下已經做了基本相同的事情。 –

+0

沒有工作。對話框仍然沒有顯示任何元素。 – SpecialSnowflake

+1

在數組適配器中,覆蓋和getCount()方法並返回infos.size() –

0

你充氣match_result.xml在你的適配器,但你要訪問row.xml

相關問題