2016-05-15 5 views
0

私はalertdialogでlistviewを使用していますが、ダイアログが表示されても何も表示されません。オブジェクトは空ではなく、アラートダイアログの前に配列リストに追加します。相続人は私の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は正しい。 arraylistは確かに空だった。しかし、私は今それを修正しました。私のforループの愚かな監視。

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

関連する問題