Skip to content

Instantly share code, notes, and snippets.

@yoga1290
Last active December 10, 2015 13:09
Show Gist options
  • Save yoga1290/4439295 to your computer and use it in GitHub Desktop.
Save yoga1290/4439295 to your computer and use it in GitHub Desktop.
/*
Here's my test view, actuclly I'm using a swipping view that's why I'm using Fragments…(nvm about that!)
Here, I'm making this class "implements URLThread_CallBack";
providing implementation for a URLCallBack(String resp) function
*/
public class view4sqr extends Fragment implements OnClickListener,URLThread_CallBack
{
View v; //view to be updated later; better be a global variable
URLThread connect=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
v=inflater.inflate(R.layout.view4sqr, container, false);
Button checkin= (Button)v.findViewById(R.id.checkin);
checkin.setOnClickListener(this);
return v;
}
// ....to be continued later
}
/*
I know a dude used to keep pressing & releasing fingers on same button as if things will get faster!
don't worry, here's a deal for such kind of people,
yes,initizaling the thread object with NULL & do nothing if it's already alive!
& let dudes have fun with the buttons ;)
*/
public class view4sqr extends Fragment implements OnClickListener,URLThread_CallBack
{
// ...
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.checkin: //matching some button id
if(connect==null)
{
connect=new URLThread("http://smth.smth", this, "my POST data");
connect.start();
}
}
}
//...
}
/*
Here's the trick!
DON'T EVER change UI all of sudden; App will clash!
Instread, ask the UI to run this seperate Thread to deal with when free :)
*/
public class view4sqr extends Fragment implements OnClickListener,URLThread_CallBack
{
//...
@Override
public void URLCallBack(String resp)
{
final String res=resp;
this.getActivity().runOnUiThread(
new Runnable()
{
public void run()
{
TextView tv=(TextView) v.findViewById(R.id.textView1);
tv.setText(res);
}
}
);
}
}
/*
First, you need to force people (or just yourself) to create a Call-Back function;
something that will be called to handle URLConnection responses
*/
interface URLThread_CallBack
{
public void URLCallBack(String response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment