2013-05-01 41 views
0

我試圖使用下面的代碼將帶有自定義ListAdapter的ListView放入AlertDialog中。目前,該活動將顯示AlertDialog,但沒有顯示的ListView:Android應用程序 - 使用自定義列表視圖設置AlertDialog視圖

public class EditMindMapActivity extends Activity { 

    private Button NewObjectButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edit_mind_map); 

     // Assign external resources 
     NewObjectButton = (Button)findViewById(R.id.button9);  

     // Set the listener for the interact button 
     NewObjectButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       //create text field for user input 
       final EditText textField = new EditText(EditMindMapActivity.this); //the ed..this might be wrong 

       /*   Create selection images ListView   */ 
        //just for placeholding really 
       ListView listView1 = new ListView(EditMindMapActivity.this); 
       listView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       listView1.setCacheColorHint(00000000); 
       ArrayList<Drawable> images = new ArrayList<Drawable>(); 
       //fill up the list of images 
       images.add(getResources().getDrawable(R.drawable.shape01)); 
       images.add(getResources().getDrawable(R.drawable.shape02)); 
       images.add(getResources().getDrawable(R.drawable.shape03)); 
       images.add(getResources().getDrawable(R.drawable.shape04)); 
       images.add(getResources().getDrawable(R.drawable.shape05)); 
       images.add(getResources().getDrawable(R.drawable.shape06)); 
       images.add(getResources().getDrawable(R.drawable.shape07)); 
       images.add(getResources().getDrawable(R.drawable.shape08)); 

       // add to the list here 
       CustomListAdapter01 adapter = new CustomListAdapter01(images, EditMindMapActivity.this); 
       listView1.setAdapter(adapter); 
       listView1.setOnItemClickListener(new ListView.OnItemClickListener() 
       { 
        public void onItemClick(AdapterView<?> listView, View itemView, int position, long itemId) 
        { 
         //nothing 
         //probably set some variable 
        } 
       }); 
       /*      ^^^^^^^^^^^      */ 


       /*   Display Select Image AlertDialog   */ 
        //may want to make this an external function 

       // 1. Instantiate an AlertDialog.Builder with its constructor 
       final AlertDialog.Builder builder = new AlertDialog.Builder(EditMindMapActivity.this); 

       // 2. Chain together various setter methods to set the dialog characteristics 
       builder.setMessage("where will this be?") 
       .setView(listView1) 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          //since the user hit 'ok', set imageLocation and ask the user to name the object -> 

          //this int holds the selected image location 
          final int imageLocation = R.drawable.shape05; 
          //final int imageLocation = listView1 

          /*   Display Input Text AlertDialog   */ 

          //might have problem because of overwriting previous builder methods 
          builder.setMessage("Enter a name for the New Object") 
          .setView(textField) 
          .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            //since the user hit 'ok', create the object specified -> 

            //need this to reference mindMapView and access it's arrays 
            MindMapView mindMapView = (MindMapView)findViewById(R.id.MindMapView); 
            //get the string from the input, set it to the label of the new object 
            mindMapView.mindMapArrayListLabels.add(textField.getText().toString()); 
            //add the correct image. 
            mindMapView.mindMapArrayListImgs.add(imageLocation); 
            //move the index to the end position of the arrays 
            mindMapView.imageIndex = (mindMapView.mindMapArrayListLabels.size() - 1); 
            //redraw the view 
            mindMapView.invalidate(); 
           } 
          }) 
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            //since the user hit 'cancel', don't do anything 
           } 
          }) 
          .setTitle("Might not need this") 
          .show(); 
          /*     ^^^^^^^^^^^      */ 
         } 
        }) 
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          //since the user hit 'cancel', don't do anything 
         } 
        }) 
        .setTitle("Choose an image:") 
        .show(); 
       /*     ^^^^^^^^^^^      */ 




      } 

     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.edit_mind_map, menu); 
     return true; 
    } 

} 

這裏是我CustomListAdapter01類:

public class CustomListAdapter01 extends BaseAdapter { 

    //contains all of the images that will be displayed 
    ArrayList<Drawable> images; 

    Context context; 

    public CustomListAdapter01(ArrayList<Drawable> images, Context context) { 
     super(); 
     // TODO Auto-generated constructor stub 
     this.images = images; 
     this.context = context; 
    } 



    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 

     Drawable image = images.get(position); 
     if (convertView == null) 
     { 
      LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_layout, null); 
     } 
     ImageView imageView = (ImageView)convertView.findViewById(R.id.image); 
     imageView.setBackgroundDrawable(image); 

     return convertView; 
    } 

} 

下面是XML文件 'child_layout':

<?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="wrap_content"> 
    <ImageView android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</RelativeLayout> 

任何幫助,將不勝感激。

回答

0
@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

應該是下面的適配器需要知道列表的大小:

@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     images.size(); 
    } 
+0

看來我這樣做,感謝後,開始工作。 – SheerSt 2013-05-02 01:34:14

+0

你能接受答案,因爲它非常有幫助。 – j2emanue 2013-05-02 13:11:11

+0

可以有人給我這個表決 – j2emanue 2013-05-03 13:17:56