ClaireG
ClaireG

Reputation: 1244

String value is null

I have a ListView with each row containing a TextView. On user click, I'm getting the value of the TextView and storing it in a String variable, and the user is navigated to another activity where he / she needs to input some data on said TextView. In order to not lose the information state of the first activity, I had to use startActivityForResult() and later get the information from the second activity with the method onActivityResult().

The problem is this: I need to compare a value with that of the TextView in question, however the String variable which supposedly contains the TextView's value is null.

Before starting up the second activity I toasted the String value and it returned the correct string, however not in the onActivityResult(), where it returns null.

Why is this happening? Isn't the Intent I used suppose to retain all information of the opened activity?

ListView's onItemClick

@Override
        public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemID)
        {


            clickedError = ((TextView)itemView).getText().toString();
            String errorIDQuery = "SELECT _id FROM " + TABLE_ERROR + " WHERE error_description LIKE '" + clickedError + "';";

                Cursor getErrorID = db.rawQuery(errorIDQuery, null);
                getErrorID.moveToFirst();
                errorID = getErrorID.getInt(0);

            Intent intent = new Intent(Repair.this, Material.class);
            intent.putStringArrayListExtra("materialList", materialList);
            startActivityForResult(intent, 1);
            Toast.makeText(getApplicationContext(), clickedError, Toast.LENGTH_LONG).show();
        }

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if(resultCode == RESULT_OK)
    {
        @SuppressWarnings("unchecked")
        HashMap <String, String> materialDetails = (HashMap <String, String>)data.getSerializableExtra("map");
        Set<?> set = materialDetails.entrySet();
        Iterator<?> i = set.iterator();

        ArrayList<String> materialNames = new ArrayList<String>();
        ArrayList<String> materialAmounts = new ArrayList<String>();

        do
        {
            @SuppressWarnings("rawtypes")
            Map.Entry me = (Map.Entry)i.next();

            materialNames.add(me.getKey().toString());
            materialAmounts.add(me.getValue().toString());
        }
        while (i.hasNext());

        for(Error e : errorList)
        {
            // ----- COMPARING THE VALUES -----
            // here clickedError is null
            if(e.description.equals(clickedError)) 
            {
                for(int j = 0; j < materialNames.size(); j++)
                {
                    MaterialClass mc = new MaterialClass();
                    mc.setName(materialNames.get(j));
                    mc.setAmount(Integer.parseInt(materialAmounts.get(j)));
                    e.materialList.add(mc);
                }
                if (e.materialList.size() != 0)
                    e.checked = true;
                else
                    e.checked = false;
            }
}

Upvotes: 0

Views: 137

Answers (3)

Delblanco
Delblanco

Reputation: 681

My guess: the activity is destroyed and restarted while moving to the second activity. This is easy to confirm with log messages in the activity life cycle methods(onCreate/onRestart/etc).

The following link might be of use to persist data during activity switching: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Upvotes: 2

Satheesh
Satheesh

Reputation: 1730

This is working if its not ask me

TextView textviewDate=(TextView)findViewById(R.id.yourtextviewid);
clickedError=textviewDate.getText().toString();

Upvotes: 0

AndroUser
AndroUser

Reputation: 592

Make your String value as static so that it will retain its value. or you can use shared preference to save value and use it later.

Upvotes: 0

Related Questions