我正在尝试使用以下代码将带有自定义 ListAdapter 的 ListView 放入 AlertDialog。目前该活动将显示 AlertDialog,但没有显示 ListView:
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>
任何帮助,将不胜感激。