2012-04-20 71 views
0

如何在此自定義對話框中使用列表?香港專業教育學院設置列表詮釋列表視圖的.XML,但我不熟悉如何正確地使用它(我想要的值列表來從一個字符串數組)在自定義對話框中使用列表視圖

我主要的Java:

package custom.dialouge.list; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 

public class CustomDialougeListTestActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final Context mContext = this; 
    final Context context = this; 
    Button button; 



    button = (Button) findViewById(R.id.button01); 

    // add button listener 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 


     // custom dialog 
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.list); 
     dialog.setTitle("List"); 

     // set the custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById(R.id.TextView01); 
     text.setText("Test"); 

     Button dialogButton = (Button) dialog.findViewById(R.id.Button01); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 
     dialog.show(); 
     } 
    }); 

} 
} 

我定製的臺詞的.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <Button 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:text="Back" /> 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="TextView" /> 


    <ListView 
     android:id="@+id/listview1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/Button01" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/TextView01" > 

    </ListView> 

</RelativeLayout> 
+0

哪裏是列表視圖代碼中的'java文件....添加它第一次......' – 2012-04-20 15:19:12

+0

這就是我需要幫助implemanting的事情之一,是列表視圖從未使用過的東西香港專業教育學院之前 – CarbonAssassin 2012-04-20 15:19:53

回答

0

嘗試是這樣的:

ArrayList<String> myStringArray = ... // insert your code to get/create the array here 
ListView view = dialog.findViewById(R.id.listview1); 
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, myStringArray); 
view.setAdapter(listAdapter); 

像這樣:

public class CustomDialougeListTestActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final Context mContext = this; 
    final Context context = this; 
    Button button; 

    ArrayList<String> myStringArray = ... // insert your code to get/create the array here 
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, myStringArray); 

    button = (Button) findViewById(R.id.button01); 

    // add button listener 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 


     // custom dialog 
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.list); 
     dialog.setTitle("List"); 

     // set the custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById(R.id.TextView01); 
     text.setText("Test"); 

     ListView view = dialog.findViewById(R.id.listview1); 
     view.setAdapter(listAdapter); 


     Button dialogButton = (Button) dialog.findViewById(R.id.Button01); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 
     dialog.show(); 
     } 
    }); 

} 
} 
+0

我會只是地方這在我的主要活動? – CarbonAssassin 2012-04-20 15:29:32

+0

你可以將它放在任何你有對話框引用的地方。它可以想象適合你的'onCreate'。 – 2012-04-20 15:38:05

+0

你能快速讓我如何將它放入我的代碼?列表視圖真的讓我困惑 – CarbonAssassin 2012-04-20 17:04:14