我有一个扩展baseadapter的类,为两个textview分配值,然后我在main活动中调用这个类,并用两个textview的值初始化它。然后我将适配器类设置为listview。 当用户点击列表时,我想要做的是弹出带有edittext的AlertDialog,用户可以输入新值并将其追加到列表视图中。我在执行此操作时遇到问题,请提供帮助。
编辑
此方法显示alertdialog
public void editInfo(final String item, final List<Info> list, final int position, final String itemTitle, final InfoAdapter adapter)
{
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Edit "+item+"?");
//Create a Layout where all the views will be appened
LinearLayout myLayout = new LinearLayout(this);
myLayout.setOrientation(LinearLayout.VERTICAL);
// Set an EditText view to get user input
final EditText inputEdit = new EditText(this);
//Declare all the properties for the email inputBox
inputEdit.setHint(item);
inputEdit.setSingleLine(true);
myLayout.addView(inputEdit);
// add all the EditTexts into the alertdialog view
alert.setView(myLayout);
//Declare the variable that will be returned
alert.setPositiveButton("Save", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int whichButton)
{
//Get all the values from the input boxes
editItem = inputEdit.getText().toString();
//Validate all the data before doing anything with it
if(editItem.equals(""))
{
showEnterValuesToast();
editInfo(item,list,position,itemTitle,adapter);
}else
{
list.set(position, new Info(itemTitle, item));
adapter.notifyDataSetChanged();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
这是listView的setOnItemClickListener
final InfoAdapter adapter = new InfoAdapter(this, listOfInfo);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int position, long index)
{
switch(position)
{
case 0:
String itemToEditName = listOfInfo.get(position).getItem();
editInfo(itemToEditName,listOfInfo,position,"Name",adapter);
break;
case 1:
String itemToEditSurname = listOfInfo.get(position).getItem();
editInfo(itemToEditSurname,listOfInfo,position,"Surname",adapter);
break;
case 2:
String itemToEditEmail = listOfInfo.get(position).getItem();
editInfo(itemToEditEmail, listOfInfo, position,"Email",adapter);
break;
case 3:
String itemToEditDate = listOfInfo.get(position).getItem();
editInfo(itemToEditDate,listOfInfo,position,"Date Of Birth",adapter);
break;
case 4:
String itemToEditAddress = listOfInfo.get(position).getItem();
editInfo(itemToEditAddress,listOfInfo,position,"Physical Address",adapter);
break;
case 5:
String itemToEditPostal = listOfInfo.get(position).getItem();
editInfo(itemToEditPostal,listOfInfo,position,"Postal Code",adapter);
break;
case 6:
String itemToEditNumber = listOfInfo.get(position).getItem();
editInfo(itemToEditNumber,listOfInfo,position,"Phone Number",adapter);
break;
}
}
});
listView.setAdapter(adapter);