androidlistviewandroid-arrayadapteronclicklisteneronitemlongclicklistener

Android onItemLongClick adds position to the arrayList in different activity


I have got two activities.

My goal is: After longClick on any ListView position in activity 2, I want some String to be added to ListView in activity 1. From activity 2 I'm going back to activity 1 by pressing back button. Each ListView has got different adapter.

I tried with Bundle (put extra), but it makes app crash.

Fragment of code from activity 1:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_layout);


 mDrawerList = (ListView)findViewById(R.id.navList);
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

}

private void addDrawerItems() { //It adds text ect. to listView - It works so I cut out the rest of code
        ar.add("anything");
        ar.add(lv.try);

        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ar);
        mDrawerList.setAdapter(mAdapter);

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

        });
    }

Simply adding like ar.add("anything"); works. The lv.try contains by default text "First text". I tried to update this variable in activity 2 onItemLongClick fragment of code:

mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //What to type here?

                return false;
            }
        });

Putting simply try = "Second text"; will not update my try variable. I tried also with Bundle (extras) but without success. Any ideas?


Solution

  • I don't know if I understood the question. Anyway, have you tried to override onBackPressed?

    In the second activity you create a boolean variabile and inizialize it as false, then change its value when you perform a long click:

    mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                longClick = true;
                return false;
           }
       });
    

    Remember, longClick has to be a class variable!

    Outside onCreate:

    @Override
    public void onBackPressed() {
       Intent intent = new Intent(SecondActivityName.this, FirstActivity.class);
       intent.putExtra("longClick", longClick);
       finish();
       startActivity(setIntent);
    }
    

    And in the first Activity:

    Intent intent = getIntent();
    if (intent.getBooleanExtra("longClick", false)) {
        addDrawerItems(); 
    }