0

I have a navigtion drawer by default on create my home fragment launches. Now from my mainactivity I go to next activity where I perform some operations and get back to mainactivity using onActivityResult.

Upto here everything works fine. Here comes my problem.

I have a listview in my HomeFragment so in the onActivityResult I get the values which are performed in other activity. Now how do I pass these values to my home fragment.

This is how I launch a new activity from mainactivity:

Intent i = new Intent(MainActivity.this, AddTask.class);
startActivityForResult(i,1);

Now In the AddTask I perform some operations and send back to main activity as shown:

Intent i = new Intent(AddTask.this,MainActivity.class);
setResult(RESULT_OK, i);
finish();

Now In my MainActivity onActivityResult:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == 1) {
                if(resultCode == RESULT_OK){
                     AddedTask = data.getStringExtra("NewTask");
                     CategoryName= data.getStringExtra("CategoryItem");
                     TaskTime=data.getStringExtra("TaskTime");

                    SharedPreferences settings = getSharedPreferences("taskdetails", 
                         Context.MODE_PRIVATE);

                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("NewTask", AddedTask);
                    editor.putString("CategoryItem", CategoryName);
                    editor.putString("TaskTime", TaskTime);
                    editor.commit();

                }
                if (resultCode == RESULT_CANCELED) {
                    //Write your code if there's no result
                }
            }
     }

Now I have a listView in my HomeFragment where I need to update with these values:

public class HomeFragment extends Fragment  {
    ListView lv;
    static final String TAG ="HomeFragment";
    private AdapterListViewData adapterListViewData; 
    private ArrayList<DataShow> listData = new ArrayList<DataShow>();
    private ListView listViewData;
    String CategoryName,TaskTime;
    String AddedTask;
    public HomeFragment(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        listViewData = (ListView)rootView.findViewById(R.id.listViewData);

        SharedPreferences settings = this.getActivity().getSharedPreferences("taskdetails", 
                Context.MODE_PRIVATE);
         AddedTask = settings.getString("NewTask",null);
         CategoryName= settings.getString("CategoryItem",null);
         TaskTime=settings.getString("TaskTime",null);
         listData.add(new DataShow(AddedTask,CategoryName,TaskTime));
         adapterListViewData = new AdapterListViewData(getActivity().getBaseContext(),listData);
         adapterListViewData.notifyDataSetChanged();
        return rootView;
  }



}

I'm not getting any error and the listview is not shown. Kindly advise me.

coder
  • 13,002
  • 31
  • 112
  • 214

5 Answers5

0

From what i can see, you never set your adapter to your listView.

So, add the following between creating a new AdapterListViewData and doing .notifyDataSetChanged()

listViewData.setAdapter(adapterListViewData);
Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • The problem is not setting the adapter but unable to get the details added to listview. – coder May 10 '15 at 17:32
0

There are different ways of achieving that.

I mostly use an event bus for communication between fragments and activities. You could use this highly recommended event bus Otto by Square team.

http://square.github.io/otto/

You can Subscribe to an event in Fragment. When you get results in Activity Produce that event and send to your Fragment.

Sharjeel
  • 15,588
  • 14
  • 58
  • 89
0

It seems there is a timing issue between onCreateView() of HomeFragment and onActivityResult() in MainActivity. Google recommends the use of Bundle for the data arguments and Interface for communication between Activity and Fragment(s). It is not hard to set up and it is good knowledge for other purposes in your app.

Anyway, I have posted an answer in the past with detailed explanations @ Passing data between fragments contained in an activity (search for my user name, if not found immediately). I like to show a snippet, sample of sending data to a fragment, from Google webpage @ Communicating with Other Fragments:

// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
Community
  • 1
  • 1
The Original Android
  • 6,147
  • 3
  • 26
  • 31
0

I just thought of another, possibly better, easier answer. Since you're depending in onCreateView() of HomeFragment, you can pass data through newInstance(). Google webpage @ Fragment. Search text for "static MyFragment newInstance", is a good example. For the Activity, search text for "Fragment newFragment = MyFragment.newInstance("From Arguments")". Code snippets at that webpage for your HomeFragment:

public static class MyFragment extends Fragment {
    CharSequence mLabel;

    /**
     * Create a new instance of MyFragment that will be initialized
     * with the given arguments.
     */
    static MyFragment newInstance(CharSequence label) {
        MyFragment f = new MyFragment();
        Bundle b = new Bundle();
        b.putCharSequence("label", label);
        f.setArguments(b);
        return f;
    }

For the Activity:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_arguments);

    if (savedInstanceState == null) {
        // First-time init; create fragment to embed in activity.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment newFragment = MyFragment.newInstance("From Arguments");
        ft.add(R.id.created, newFragment);
        ft.commit();
    }
}

I believe you can replace the code easily yourself. Your code shows good understanding of Android. Good luck, have fun, keep us posted...

The Original Android
  • 6,147
  • 3
  • 26
  • 31
0

The problem is the navigation drawer has menu in mainactivity not in homefragment. So the onActivityResult is pointing to mainactivity instead of my HomeFragment. After changing the menu to HomeFragment resolved my issue.

Thanks for all the folks who helped me out :)

coder
  • 13,002
  • 31
  • 112
  • 214