Reputation: 392
I have a custom dialog with me. In which an editText id there, I am reading input through editText, putting the input to a string. This code is followed by a thread (sub) to handle one url. I want to use this string in the thread mentioned. But the thread is getting invoked before I type to the editText. How can i dynamically use the same text from the userinput inside the thread? Thanks in advance..
public void onClick(View v) {
switch (v.getId())
{
case R.id.i1:
MyDevice.getInstance().currentUserImageId=R.drawable.jerry1;
MyDevice.getInstance().userName="You";
MyDevice.getInstance().facebook=0;
this.finish();
break;
case R.id.i2:
MyDevice.getInstance().currentUserImageId=R.drawable.chaplin1;
MyDevice.getInstance().userName="You";
MyDevice.getInstance().facebook=0;
this.finish();
break;
case R.id.i3:
MyDevice.getInstance().currentUserImageId=R.drawable.budy;
MyDevice.getInstance().userName="You";
MyDevice.getInstance().facebook=0;
this.finish();
break;
case R.id.facebook:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.usernamefbdialog);
dialog.setTitle("Enter Facebook Username");
Button dialogButton = (Button) dialog.findViewById(R.id.done);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
EditText edit=(EditText)dialog.findViewById(R.id.username);
text=edit.getText().toString();
dialog.dismiss();
}
});
dialog.show();
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
MyDevice.getInstance().bitmap=getUserPic(text);
MyDevice.getInstance().facebook=1;
ImageView facebookImg=(ImageView) findViewById(R.id.facebookimg);
facebookImg.setImageBitmap(MyDevice.getInstance().bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
thread.start();
break;
}
}
Upvotes: 1
Views: 246
Reputation: 44571
I'm not exactly sure if I understand but this sentence "How can i dynamically use the same text from the userinput inside the thread?"sounds like you want to implement a TextWatcher on your EditText
public class TempWatcher implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// you can update something here
}
}
But if you check for a valid value in your EditText
when clicking the Button
and only start your Thread
then, it should do what you want. If this doesn't work then please clarify your problem and show where you start the Thread
and how it is implemented
Upvotes: 0
Reputation: 4186
Basically what you want to do is run the Thread from within the button click event, that way it runs after you get the text. See your modified code below. The events that happen to the String occur in this order. 1. Create the String 2. Set the String equal to the edit text String 3. Start the Thread and use the String
final String theStringYouWantToUseInTheThread = null;
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
//use theStringYouWantToUseInTheThread here
MyDevice.getInstance().bitmap=getUserPic(text);
MyDevice.getInstance().facebook=1;
ImageView facebookImg=(ImageView) findViewById(R.id.facebookimg);
facebookImg.setImageBitmap(MyDevice.getInstance().bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
EditText edit=(EditText)dialog.findViewById(R.id.username);
theStringYouWantToUseInTheThread = edit.getText().toString();
thread.start();
dialog.dismiss();
}
});
dialog.show();
thread.start();
Upvotes: 1