Using List Adapter in a Fragment Activity - java

I have an activity class that extends from FragmentActivity as I am using DialogFragment in it.
Earlier this class was extended from ListActivity and there was no issues but when I extended it from FragmentActivity when the requirement of DialogFragment arrive the method setListAdapter becomes unavaible.
I want to know that how can I use the method setListAdapter while extending my class from FragmentActivity
public class Main extends FragmentActivity{
…
…
private class fetchStudentInfo extends AsyncTask<String, Void, List<mStudentInfo>> {
#Override
protected List<mStudentInfo> doInBackground(String... urls) {
…
}
public void onPostExecute(List<mStudentInfo> StudentInfoCollection) {
setListAdapter(new StudentInfoAdapter((Activity) mainAppContext, StudentInfoCollection));
}
}
}

For setting setAdapter you need a listView reference like this ,
public class Main extends FragmentActivity {
private ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_id);
mListView = (ListView)findViewById(R.id.list);
}
private class fetchStudentInfo extends AsyncTask<String, Void, List<mStudentInfo>> {
#Override
protected List<mStudentInfo> doInBackground(String... urls) {
…
}
public void onPostExecute(List<mStudentInfo> StudentInfoCollection) {
mListView.setListAdapter(new StudentInfoAdapter((Activity) mainAppContext, StudentInfoCollection));
}
}
}
And also you need layout file with a listview inside with id R.id.list.

Related

how to use activity context from other activity?

I want to use activity context from another activity simple code example below Any idea ?
public class Activity_A extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_A);
}
}
public class Activity_B extends AppCompatActivity {
Dialog dialog1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_B);
dialog1 = new Dialog(I want Activity_A Context) ; // Is this possible ??
}
}
Thanks
Tamim you can definitely achieve this using a different class by making a public static function in that other class
public class Utils {
public static void showDialog(Context context){
//// your code here
}
now you use it wherever you want...
Utils.showDialog(this);

Calling a method of MainActivity from other class

I am developing an Android app and thus, I have a MainActivity class. Inside of that MainActivity class, I have a method, let's call it doSomething():
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doSomething(){
// bla bla bla
}
}
I also have a different class (with different layout) that is called OtherActivity. I want to use the doSomething method inside it:
public class OtherActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
}
}
I tried this:
public class OtherActivity extends AppCompatActivity {
MainActivity main;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
MainActivity main = new MainActivity();
main.doSomething();
}
}
But it does not work. I also tried to make OtherActivity to extend the MainActivity, doing the following:
public class OtherActivity extends MainActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
super.doSomething();
}
}
But it does not allow me to initialize the layout...
How can I do?
Thanks in advance.
To communicate between to Activity Broadcast is the best way, and for the same application, we can use local broadcast using LocalBroadcastManager.
First, we should register one broadcast in MainActivity,
public class MainActivity1 extends AppCompatActivity {
public static final String INTENT_FILTER = "do_some_action";
public static final String INTENT_BUNDLE_VALUE = "value1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
LocalBroadcastManager.getInstance(this).registerReceiver(
mChangeListener, new IntentFilter(INTENT_FILTER));
}
#Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mChangeListener);
}
private BroadcastReceiver mChangeListener = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intentData) {
// you can do anything here
if (intentData != null && intentData.hasExtra(INTENT_BUNDLE_VALUE)) {
String value = intentData.getStringExtra(INTENT_BUNDLE_VALUE);
doSomeAction(value);
}
}
};
private void doSomeAction(String value) {
}
}
Then to do some action in MainActivity from OtherActivity, we can send Local broadcast from OtherActivity it will reach the receiver of Which we register in MainActivity,
public class OtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
// You can call MainActivity to do some actions
Intent intent = new Intent(MainActivity1.INTENT_FILTER);
intent.putExtra(MainActivity1.INTENT_BUNDLE_VALUE, "Any string or any value");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Done!!!.
Something like this should do the trick, I'm going to make a static navigator to handle your navigation logic. If you are opposed to static methods you could also make them on your Application object to make it easier to manage dependencies, I'm just making it static for simplicity.
//Making this fully static for simplicity, this is fine for a small app
//you can make it a singleton on the application class for more flexibility
public class Navigator {
//static member vars that determine navigation
// pass in Context if needed for navigation purposes
public static void doSomething(Context context){
// bla bla bla
}
}
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
}
private void doSomething() {
Navigator.doSomething(this);
}
}
public class OtherActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
}
private void doSomething() {
Navigator.doSomething(this);
}
}

How to execute an inner class inside of Class which accessed from adapter inside of Class

I have a problem with access an Asynctask<> which inside of A class and A class have a adapter (RecyclerViewAdapter) which wanted to access the class which extends the Asynctask<>
for short, it'd be like this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
RecyclerView recyclerView;
SimpleRecyclerAdapter simpleRecyclerAdapter;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setAdapter(SimpleRecyclerAdapter);
public class getData extends AsyncTask<>{
....
}
}
and this is the SimpleRecyclerAdapter
public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleRecyclerAdapter.VersionViewHolder> {
//from this I would like to execute getData class
}
Try like this,
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
RecyclerView recyclerView;
SimpleRecyclerAdapter simpleRecyclerAdapter;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
new GetData().execute();
}
public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleRecyclerAdapter.VersionViewHolder> {
//from this I would like to execute getData class
}
public class GetData extends AsyncTask<>{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
recyclerView.setAdapter(new SimpleRecyclerAdapter());
}
}

Error in implementation of interface between activity and view

I'm trying to get in my MyActivity an int from a View MyView. In my activity I have the following:
public class MyActivity extends AppCompatActivity implements MyView.GetCallBack {
final MyActivity context = this;
private AsyncTask<Void, Void, Void> task;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_act);
task = new myTask();
task.execute();
}
#Override
public void onPercentageReceived(int msg){
// you have got your msg here.
}
public class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
}
#Override
protected void onPostExecute(Void result) {
LinearLayout surface = (LinearLayout) findViewById(R.id.surfaceView);
surface.addView(new MyView(getApplicationContext()));
surface.setBackgroundColor(Color.BLACK);
}
}
Now MyView contains the following code:
public class MyView extends View {
final MyView context = this;
private GetCallBack callback;
// Constructor
public PlacingBoxView(Context context) {
super(context);
callback = (GetCallBack) context;
}
#Override
protected void onDraw(Canvas canvas) {
dataPercentage(Percentage);
}
public void dataPercentage(int Percentage){
callback.onPercentageReceived(Percentage);
}
public interface GetCallBack{
void onPercentageReceived(int msg);
}
I can compile the code without problems, but in the LogCat I get the following mistake:
FATAL EXCEPTION: main
Process: com.example.ex, PID: 8035
java.lang.ClassCastException: android.app.Application cannot be cast
to com.example.ex.myView$GetCallBack
at com.example.ex.myView.(myView.java:49)
I know the error is related with the Context, but I still haven't found out a way to correct it,
Any idea will be really appreciated! :)
You have implemented the interface in myActivity but you are passing application context. That's why you are getting ClassCastException. Pass myActivity.this, so try like this:
surface.addView(new MyView(MyActivity.this);

Send string variable from Activity to AsyncTask Class

I need to send a string variable from my main activity class to the AsyncTask Class and use that string as part of the url to make the api call.
I tried using Intent and share preferences but neither can seem to be accessed in the AsyncTask Class. Can I use Singleton pattern, and if yes, how would I go about it?
If you declare a global variable:
public class MainActivity extends Activity {
private String url = "http://url.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DownloadFilesTask().execute();
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected Long doInBackground(Void... params) {
// You can use your 'url' variable here
return null;
}
protected void onProgressUpdate(Void... result) {
return null;
}
protected void onPostExecute(Void result) {
}
}
}
if you work in seperate classes:
new MyAsyncTask("Your_String").execute();
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
public MyAsyncTask(String url) {
super();
// do stuff
}
// doInBackground()
}

Categories