A
A
Artix2014-09-14 12:48:02
Android
Artix, 2014-09-14 12:48:02

Android. listview. How to set up an element click handler to open fragments?

Hi all!
The situation is this: there is an activity and it has a list (ListView). You need to configure the click handler so that when you click on the first element, activity2 is called showing the first fragment, and similarly, if you click on the second element, then display activity2 showing the second fragment.
Here's a picture so you can understand what I'm talking about
8d317be127484408aa7932154565f436.png
My code:

public class Class extends Activity {
    ArrayAdapter<String> adapter = null;
    View emptyView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity);
        getActionBar().hide();
        ListView listView = (ListView) findViewById(R.id.ListView);
        listView.setEmptyView(
                emptyView = getLayoutInflater().inflate(R.layout.empty_view, null));
        ((ViewGroup)listView.getParent()).addView(emptyView);
        fillListView();
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // When clicked, show a toast with the TextView text
               if (((TextView) view).getText() == "Go1"){

               } else
               if (((TextView) view).getText() == "Go2"){

                  FragmentManager fragmentManager1 = getFragmentManager();
                   FragmentTransaction fragmentTransaction1 = fragmentManager1
                           .beginTransaction();
                   Intent Fragment1 = new Intent(Class.this, MainActivity.class);
                   startActivity(Fragment1);
                   Fr myFragment1 = new Fr();
                   myFragment1.setRetainInstance(true);
                   fragmentTransaction1.replace(R.id.container, myFragment1);
                   fragmentTransaction1.commit();
                   SearchActivity.this.finish();
               }
                if (((TextView) view).getText() == "Go3"){
                    
                }
            }
        });

        EditText editFilter = (EditText) findViewById(R.id.edit_search);
        editFilter.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                adapter.getFilter().filter(s.toString());
            }
        });

    }

    private void fillListView() {
        List<String> catList = new ArrayList<String>();
        catList.add("Go1");
        catList.add("Go2");
        catList.add("Go3");

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, catList);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
linreal, 2014-09-14
@nikkorejz

Why not just create an Intent with a nested key when clicking on a list item, open Activity2 and load the necessary fragment in it, based on the passed key?
activity1:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

Intent i = new Intent(getBaseContext(), Activity2.class);
i.putExtra("position", position);
startActivity(i);
}

activity2:
int key = getIntent.getIntExtra("position", 0);

switch(i){

case 1:

//load Fr1

case 2:

//load Fr2

O
Oleg Gamega, 2014-09-14
@gadfi

I will continue the thought, in my opinion it would be more kosher to create your own interface and implement it in the parent activity, so that it has all the logic, and the ui just spins in the fragment and the interface method of the parent activity is called on click

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question