Android onItemLongClick adds position to the arrayList in different activity

时间:2015-05-24 20:34:58

标签: android listview android-arrayadapter onclicklistener onitemlongclicklistener

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?

2 个答案:

答案 0 :(得分:0)

What does add position mean ?

In my opinion add position means add one element to a structure (array, list, etc).

So basically what I mean is that you can have an array in your CustomApplicatioClass or even a Static array(someone probably would not agree with me) and onItemLongClick() add the element you want in the structure.

Then when you load the Activity with the ListView you can do your ListView stuff using the updated structure

hope it helps

答案 1 :(得分:0)

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(); 
}