Proper removeView() usage - android

Can someone please explain where do put the removeView() in this case?
private void regListClickCallback() {
ListView myList = (ListView) findViewById(R.id.gamePlayList);
LayoutInflater li = LayoutInflater.from(this);
scorCh = li.inflate(R.layout.score_change_layout, null);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long idInDB) {
AlertDialog.Builder aDB = new AlertDialog.Builder(context);
aDB.setView(scorCh);
aDB.setCancelable(false).setMessage(i).setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog aD = aDB.create();
aD.show();
}
});
}
When I run the app I can click on one of the list items and get the dialog, but when I click the same item again or any other item, I get
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I have tried to put the following code both in onCreate and in this regListClickCallback.
if(scorCh!=null)
{
ViewGroup parent = (ViewGroup) scorCh.getParent();
if(parent!=null)
parent.removeView(scorCh);
};

Related

I want to get the selected value from List view in alert dialog

I have ArrayList of object (citiesInSpinner) each object have two value(Id, Name)
I have already get it in alert dialog
I use this function to alert dialog:
public void test()
{
FillSpinner();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchFligtsActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Select City");
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,citiesInSpinner);
lv.setAdapter(adapter);
/* alertDialog.setItems(citiesInSpinner, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
/////
}
});*/
alertDialog.show();
/**/
}
now I want to make something to get the ((ID)) of item (I mean the Id of City)
I tried to do that but I failed...
any help Please !!
and thank you
Simply use
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
City city = citiesInSpinner.get(position)
//get your id -> city.Id
}
});

Diaglog with Spinners selecteditem

i was searching and trying alot today but it all does not help.
I want to use a Dialog with my own layout. In this layout there is a spinner with some subitems. My Problem is i can't get those selecteditems.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.decision, null));
LayoutInflater inflater2= getLayoutInflater();
View viewInflater=inflater2.inflate(R.layout.decision, null);
Spinner spinner=(Spinner) viewInflater.findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("click", "returnValue");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.v("click", "nothing");
}
});
builder.setPositiveButton("Start", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v("click", "positive");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v("click", "negative");
}
});
builder.show();
}
I think that the problem can be that you inflate a layout, which returns a new view. This new view has different reference than this original and when you set the listener, it affects only at this new, not this you want. Maybe you should set this new view to newly created Alert Dialog?
Have you tried something like this (instead of builder.show())?
AlertDialog alertDialog = builder.create();
alertDialog.setView(viewInflater);
alertDialog.show();
It helped me so much when I wanted do set my custom view to alertdialog.

Why setText() of EditText is not working?

I want to put a string in a EditText in a AlertDialog,which is in a onItemClick Listener with a ListVIew.Here is the part of code
dialogbuilder= new AlertDialog.Builder(this)
.setTitle(R.string.title_connect_dialog)
.setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.dialog_connect, null);
edit1 = (EditText)view.findViewById(R.id.devicename);
edit2 = (EditText)view.findViewById(R.id.deviceadd);
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
String info = ((TextView) v).getText().toString();
name = info.substring(0,info.length() - 18);
address = info.substring(info.length() - 17);
dialogbuilder.show();
edit1.setText(name);
edit2.setText(address);
}
};
When I run the project,there is nothing in the EditText!It does not work!But there is no exception or error.I tried only set text in the XML file can work.I replaced EditText with TextView ,but is not work too.
Are there something wrong in logical?Thank you for reading.
Use as view instance for accessing Views form Dialog layout which you are passing in setView method of AlertDialog.Builder change:
.setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))
to
.setView(view)
EDIT:
or instead of inflate layout again to access views you should use dialogbuilder instance to initialize EditText as:
edit1 = (EditText)dialogbuilder.findViewById(R.id.devicename);
edit2 = (EditText)dialogbuilder.findViewById(R.id.deviceadd);
You are showing the Dialog before setting the text
dialogbuilder.show();
edit1.setText(name);
edit2.setText(address);
Try changing that around
edit1.setText(name);
edit2.setText(address);
dialogbuilder.show();

CheckBox in BaseAdapter auto check

I have a ListView with each row consisting of a textview and a checkbox.
The user is allowed to click on each TextView, and once he does a DialogBox is presented to him where he is expected to either choose Yes or No.
When he chooses No, the CheckBox next to the TextView the user clicked needs to be ticked automatically.
The problem that I am facing is that no matter which TextBox the user chooses, the last CheckBox of the list gets ticked all the time, and not the one next to the chosen TextView.
After debugging I noticed that this is happening because the getView() method of the adapter class stops on the last row, thus the last row always gets selected. I tried setting up tags for each row so that I can than tick the checkbox accordingly but then neither Checkboxes get ticked.
I would appreciate any help on this matter.
....
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
holder.text.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(final View v)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String clickedError;
clickedError = holder.text.getText().toString();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
((Activity)mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//holder.chk.setChecked(true); // This ticks the last checkbox of the list.
if(holder.chk.getTag() == v.getTag())
{
holder.chk.setChecked(true);
}
}
});
builder.show();
}
});
Try passing the checkbox for the row into your on click listener:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
holder.text.setOnClickListener(new MyClickListener(holder.chk);
}
private class MyClickListener implements OnClickListener
{
Checkbox checkbox = null;
public MyClickListener(final Checkbox checkbox)
{
this.checkbox = checkbox;
}
#Override
public void onClick(final View v)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String clickedError;
clickedError = holder.text.getText().toString();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
((Activity)mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//checkbox.setChecked(true); // This ticks the last checkbox of the list.
if(checkbox.getTag() == v.getTag())
{
checkbox.setChecked(true);
}
}
});
builder.show();
}
});
CheckBox extends TextView. Use CheckBox.setText(CharSequence | int) to set text into CheckBox and remove the TextView (that will not solve your check problem, of course). Regarding your problem - it's because most AdapterView's re-use Views. Store the checked item information somewhere and in getView call setChecked(checkedItems.get(pos)).
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
holder.chk.setChecked(checkedItems.get(position))
.....
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//holder.chk.setChecked(true); // This ticks the last checkbox of the list.
if(holder.chk.getTag() == v.getTag())
{
holder.chk.setChecked(true);
checkedItems.put(position, true);
}
}
});
}
Where checkedItems is a SparseBooleanArray

Access controls within custom alert dialog in android

When displaying custom dialog box which is showing textView and EditView. How can i access these elements within dialog. Below is the code which is giving error.
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
string = uetd.getText().toString() + ":" + petd.getText().toString(); /////producing error
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
public class listdemo extends ListActivity {
/** Called when the activity is first created. */
private static final int DIALOG_TEXT_ENTRY = 7;
EditText uetd;
EditText petd;
SharedPreferences.Editor editor;
String string;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
uetd=(EditText)findViewById(R.id.username_edit);
petd=(EditText)findViewById(R.id.password_edit);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
showDialog(DIALOG_TEXT_ENTRY);
}
});
}
}
Instead of returning the dialog immedately with return new AlertDialog.builder....and so on....you should save an instance of the dialog by creating a class variable just like this
AlertDialog dialog;
Then you can easily access the views of the dialog within the callback of the dialog:
dialog.findViewById(R.id.username_edit);
You have to do this because when creating the callback it is in a way constructing a new class in memory so when you call findViewById() directly it can't find the activity
try this code
uetd=(EditText) textEntryView.findViewById(R.id.username_edit);
after following line in your code
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);

Categories

Resources