John Nguyen
John Nguyen

Reputation: 557

Android ListView Adapter shows last item on click

Im having a problem with my ListView adapter, Everything is being parsed VIA json into the list View just fine but when I click on the ListView, it always shows me the results from the last parsed json in the ListView.

Here is my first class called Jobs:

 public class Jobs extends ListActivity {

// JSON Node names
private static final String jobs = "jobs";
private static final String job = "job";
private static final String pays = "pays";
private static final String ready_at = "ready_at";
private static final String due_by = "due_by";
private static final String customer_reference = "customer_reference";
private static final String pieces = "pieces";
private static final String weight = "weight";
private static final String pickup_name = "pickup_name";
private static final String pickup_addr1 = "pickup_addr1";
private static final String pickup_city = "pickup_city";
private static final String pickup_state = "pickup_state";
private static final String pickup_zip_postal = "pickup_zip_postal";
private static final String deliver_name = "deliver_name";
private static final String deliver_addr1 = "deliver_addr1";
private static final String deliver_city = "deliver_city";
private static final String deliver_state = "deliver_state";
private static final String deliver_zip_postal = "deliver_zip_postal";
private static final String signature_required = "signature_required";
private static final String pickup_to_see = "pickup_to_see";
private static final String pickup_room = "pickup_room";
private static final String pickup_phone = "pickup_phone";
private static final String deliver_to_see = "deliver_to_see";
private static final String deliver_room = "deliver_room";
private static final String pickup_special_instr = "pickup_special_instr";
private static final String deliver_special_instr = "deliver_special_instr";
private static final String deliver_phone = "deliver_phone";

String JOB, PAYS, READY_AT, DUE_BY, CUSTOMER_REFERENCE, PIECES, WEIGHT, PICKUP_NAME, PICKUP_ADDR1, PICKUP_CITY, PICKUP_STATE,
        PICKUP_ZIP_POSTAL, DELIVER_NAME, DELIVER_ADDR1, DELIVER_CITY, DELIVER_STATE, DELIVER_ZIP_POSTAL, SIGNATURE_REQUIRED,
        PICKUP_TO_SEE, PICKUP_ROOM, PICKUP_PHONE, DELIVER_TO_SEE, DELIVER_ROOM, PICKUP_SPECIAL_INSTR, DELIVER_SPECIAL_INSTR,
        DELIVER_PHONE;

// Strings
String username, password, firstName, lastName, lastLatitudeUpdate, dvrcheckin, dvrScheduleToWork, dvrStartTime, jobs_assigned;
int checkedin, Minute, Hour, Minutedvr, Hourdvr, status;

Vibrator vib;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jobs);
    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    new ParseJobs().execute();


    TextView assigned = (TextView) findViewById(R.id.assigned);

    if (jobs_assigned.matches("0")){
    assigned.setText("You have no jobs assigned");
    } else {
        assigned.setVisibility(View.GONE);
    }


    // selecting single ListView item
    ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long ide) {

            vib.vibrate(40);

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    SingleMenuItemActivity.class);
            in.putExtra(pays, PAYS);
            in.putExtra(job, JOB);
            in.putExtra(ready_at, READY_AT);
            in.putExtra(due_by, DUE_BY);
            in.putExtra(customer_reference, CUSTOMER_REFERENCE);
            in.putExtra(pieces, PIECES);
            in.putExtra(weight, WEIGHT);
            in.putExtra(pickup_name, PICKUP_NAME);
            in.putExtra(pickup_addr1, PICKUP_ADDR1);
            in.putExtra(pickup_city, PICKUP_CITY);
            in.putExtra(pickup_state, PICKUP_STATE);
            in.putExtra(pickup_zip_postal, PICKUP_ZIP_POSTAL);
            in.putExtra(deliver_name, DELIVER_NAME);
            in.putExtra(deliver_addr1, DELIVER_ADDR1);
            in.putExtra(deliver_city, DELIVER_CITY);
            in.putExtra(deliver_state, DELIVER_STATE);
            in.putExtra(deliver_zip_postal, DELIVER_ZIP_POSTAL);
            in.putExtra(signature_required, SIGNATURE_REQUIRED);
            in.putExtra(pickup_to_see, PICKUP_TO_SEE);
            in.putExtra(pickup_room, PICKUP_ROOM);
            in.putExtra(pickup_phone, PICKUP_PHONE);
            in.putExtra(deliver_to_see, DELIVER_TO_SEE);
            in.putExtra(deliver_room, DELIVER_ROOM);
            in.putExtra(pickup_special_instr, PICKUP_SPECIAL_INSTR);
            in.putExtra(deliver_special_instr, DELIVER_SPECIAL_INSTR);
            in.putExtra(deliver_phone, DELIVER_PHONE);

            startActivity(in);
            Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);

        }
    });
}

public class ParseJobs extends AsyncTask<Void, Void, Void> {

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // contacts JSONArray
    JSONArray JOBS;

    ProgressDialog VerifyDriver;

    @Override
    protected void onPreExecute() {

        VerifyDriver = ProgressDialog.show(Jobs.this, "Getting Your Jobs", "Loading Data... Please Wait...");
        WindowManager.LayoutParams lp = VerifyDriver.getWindow().getAttributes();
        VerifyDriver.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        lp.dimAmount = .65f;
        VerifyDriver.getWindow().setAttributes(lp);

        Intent i = getIntent();
        username = i.getExtras().getString("uid");
        password = i.getExtras().getString("pwd");
        firstName = i.getExtras().getString("firstName");
        lastName = i.getExtras().getString("lastName");
        lastLatitudeUpdate = i.getExtras().getString("lastLatitudeUpdate");
        Minute = i.getExtras().getInt("Minute");
        Hour = i.getExtras().getInt("Hour");
        dvrcheckin = i.getExtras().getString("check-in");
        Hourdvr = i.getExtras().getInt("Hourdvr");
        Minutedvr = i.getExtras().getInt("Minutedvr");
        status = i.getExtras().getInt("status");
        dvrScheduleToWork = i.getExtras().getString("dvrScheduleToWork");
        dvrStartTime = i.getExtras().getString("dvrStartTime");
        jobs_assigned = i.getExtras().getString("jobs_assigned");
    }

    @Override
    protected Void doInBackground(Void... params) {

        // getting JSON string from URL
        JSONObject json = JSONfunction.getJSONfromURL("http://www.jetdelivery.com/mobile/api/getresourcestatus/?type=json&uid=" + (username) + "&pwd=" + (password));

        try {
            // Getting Array of Contacts
            JOBS = json.getJSONArray(jobs);

            // looping through All Contacts
            for (int i1 = 0; i1 < JOBS.length(); i1++) {
                JSONObject c = JOBS.getJSONObject(i1);

                // Storing each json item in variable
                JOB = c.getString(job);
                PAYS = c.getString(pays);
                READY_AT = c.getString(ready_at);
                DUE_BY = c.getString(due_by);
                DELIVER_PHONE = c.getString(deliver_phone);
                PIECES = c.getString(pieces);
                WEIGHT = c.getString(weight);
                PICKUP_NAME = c.getString(pickup_name);
                PICKUP_ADDR1 = c.getString(pickup_addr1);
                PICKUP_CITY = c.getString(pickup_city);
                PICKUP_STATE = c.getString(pickup_state);
                PICKUP_ZIP_POSTAL = c.getString(pickup_zip_postal);
                DELIVER_NAME = c.getString(deliver_name);
                DELIVER_ADDR1 = c.getString(deliver_addr1);
                DELIVER_CITY = c.getString(deliver_city);
                DELIVER_STATE = c.getString(deliver_state);
                DELIVER_ZIP_POSTAL = c.getString(deliver_zip_postal);
                SIGNATURE_REQUIRED = c.getString(signature_required);
                PICKUP_TO_SEE = c.getString(pickup_to_see);
                PICKUP_ROOM = c.getString(pickup_room);
                PICKUP_PHONE = c.getString(pickup_phone);
                DELIVER_TO_SEE = c.getString(deliver_to_see);
                DELIVER_ROOM = c.getString(deliver_room);
                PICKUP_SPECIAL_INSTR = c.getString(pickup_special_instr);
                DELIVER_SPECIAL_INSTR = c.getString(deliver_special_instr);
                CUSTOMER_REFERENCE = c.getString(customer_reference);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(job, JOB);
                map.put(pays, PAYS);
                map.put(ready_at, READY_AT);
                // map.put(weight, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        Jobs.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        params.x = 0;
        params.height = 520;
        params.width = 480;
        params.y = 200;
        params.dimAmount = .70f;
        Jobs.this.getWindow().setAttributes(params);

        /** Updating parsed JSON data into ListView */
        ListAdapter adapter = new SimpleAdapter(Jobs.this, contactList, R.layout.list_item, new String[] { job, pays, ready_at }, new int[] { R.id.job1, R.id.pays1, R.id.ready_at1 });
        setListAdapter(adapter);

        VerifyDriver.dismiss();

    }
}

@Override
protected void onResume() {
    super.onResume();
    Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}

@Override
protected void onPause() {
    super.onPause();
    Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);

}

@Override
public void onBackPressed() {
    vib.vibrate(40);

    // Send username and password strings into screen1 class
    Intent i = new Intent(Jobs.this, screen1.class);
    i.putExtra("uid", username);
    i.putExtra("pwd", password);
    i.putExtra("Minute", Minute);
    i.putExtra("Hour", Hour);
    i.putExtra("firstName", firstName);
    i.putExtra("lastName", lastName);
    i.putExtra("check-in", dvrcheckin);
    i.putExtra("Hourdvr", Hourdvr);
    i.putExtra("Minutedvr", Minutedvr);
    i.putExtra("status", status);
    i.putExtra("lastLatitudeUpdate", lastLatitudeUpdate);
    i.putExtra("dvrScheduleToWork", dvrScheduleToWork);
    i.putExtra("dvrStartTime", dvrStartTime);
    i.putExtra("jobs_assigned", jobs_assigned);
    Jobs.this.finish();
    startActivity(i);
    Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);

}

@Override
protected void onRestart() {
    super.onRestart();
    Intent i = new Intent(this, AgentPortalActivity.class);
    startActivity(i);
    Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);

}
}

Heres me second class which simply shows more information about the job when you click on the ListView:

 public class SingleMenuItemActivity extends Activity {

// JSON Node names

private static final String job = "job";
private static final String pays = "pays";
private static final String ready_at = "ready_at";
private static final String due_by = "due_by";
private static final String customer_reference = "customer_reference";
private static final String pieces = "pieces";
private static final String weight = "weight";
private static final String pickup_name = "pickup_name";
private static final String pickup_addr1 = "pickup_addr1";
private static final String pickup_city = "pickup_city";
private static final String pickup_state = "pickup_state";
private static final String pickup_zip_postal = "pickup_zip_postal";
private static final String deliver_name = "deliver_name";
private static final String deliver_addr1 = "deliver_addr1";
private static final String deliver_city = "deliver_city";
private static final String deliver_state = "deliver_state";
private static final String deliver_zip_postal = "deliver_zip_postal";
private static final String signature_required = "signature_required";
private static final String pickup_to_see = "pickup_to_see";
private static final String pickup_room = "pickup_room";
private static final String pickup_phone = "pickup_phone";
private static final String deliver_to_see = "deliver_to_see";
private static final String deliver_room = "deliver_room";
private static final String pickup_special_instr = "pickup_special_instr";
private static final String deliver_special_instr = "deliver_special_instr";
private static final String deliver_phone = "deliver_phone";



String JOB, PAYS, READY_AT, DUE_BY, CUSTOMER_REFERENCE, PIECES, WEIGHT, SIGNATURE_REQUIRED, // General Job Information
PICKUP_NAME,PICKUP_TO_SEE, PICKUP_ROOM, PICKUP_ADDR1, PICKUP_CITY, PICKUP_STATE, PICKUP_ZIP_POSTAL, PICKUP_PHONE, PICKUP_SPECIAL_INSTR, // Pickup Job Information
DELIVER_NAME, DELIVER_ADDR1, DELIVER_CITY, DELIVER_STATE, DELIVER_ZIP_POSTAL, DELIVER_TO_SEE, DELIVER_ROOM,  DELIVER_SPECIAL_INSTR, DELIVER_PHONE,  // Deliver Job Information

username, password, firstName, lastName, lastLatitudeUpdate, dvrcheckin, dvrScheduleToWork, dvrStartTime, jobs_assigned; // Original Strings Passed through from main Activity
int checkedin, Minute, Hour, Minutedvr, Hourdvr, status;    // Integers Passed through from main Activity

Vibrator vib;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.single_menu_item_activity);
    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    Get_Intents();

    ////////////////////////////////////////////////////////
    ////////        Initializing all TextViews      ////////
    ////////////////////////////////////////////////////////
    TextView trackno = (TextView) findViewById(R.id.trackno);
    TextView readyAT = (TextView) findViewById(R.id.readyAT);
    TextView jobpays = (TextView) findViewById(R.id.jobpays);
    TextView pcs = (TextView) findViewById(R.id.pcs);
    TextView weightt = (TextView) findViewById(R.id.weightt);
    TextView custref = (TextView) findViewById(R.id.custref);
    TextView dueby = (TextView) findViewById(R.id.dueby);
    TextView sigreq = (TextView) findViewById(R.id.sigreq);

    ///////////////     PickUp TextViews    //////////////////////
    TextView puname = (TextView) findViewById(R.id.puname);
    TextView puaddress = (TextView) findViewById(R.id.puaddress);
    TextView puroom = (TextView) findViewById(R.id.puroom);
    TextView pusee = (TextView) findViewById(R.id.pusee);
    TextView puphone = (TextView) findViewById(R.id.puphone);
    TextView puinstructions = (TextView) findViewById(R.id.puinstructions);

    //////////////      Deliver TextViews       ///////////////////
    TextView delname = (TextView) findViewById(R.id.delname);
    TextView deladdress = (TextView) findViewById(R.id.deladdress);
    TextView delroom = (TextView) findViewById(R.id.delroom);
    TextView delsee = (TextView) findViewById(R.id.delsee);
    TextView delphone = (TextView) findViewById(R.id.delphone);
    TextView delinstructions = (TextView) findViewById(R.id.delinstructions);

    /////////////       Miscellaneous TextViews     /////////////////
    TextView origin = (TextView) findViewById(R.id.origin);
    TextView destination = (TextView) findViewById(R.id.destination);
    origin.setText(Html.fromHtml("<i><u>ORIGIN</u></i> "));
    destination.setText(Html.fromHtml("<i><u>DESTINATION</u></i> "));


    /////////////////////////////////
    // Setting String to TextViews //
    /////////////////////////////////
    trackno.setText(JOB);
    readyAT.setText(READY_AT);
    jobpays.setText(PAYS);
    pcs.setText(PIECES);
    weightt.setText((WEIGHT) + " Lbs");
    custref.setText(CUSTOMER_REFERENCE);
    dueby.setText(DUE_BY);

    ///////// Pickup Info ///////////
    puname.setText(PICKUP_NAME);
    puaddress.setText((PICKUP_ADDR1) + "\n" + (PICKUP_CITY) + " " + (PICKUP_STATE) + " " + (PICKUP_ZIP_POSTAL));
    puroom.setText(PICKUP_ROOM);
    pusee.setText(PICKUP_TO_SEE);
    puphone.setText(PICKUP_PHONE);
    puinstructions.setText(PICKUP_SPECIAL_INSTR);

    ///////// Deliver Info //////////
    delname.setText(DELIVER_NAME);
    deladdress.setText((DELIVER_ADDR1) + "\n" + (DELIVER_CITY) + " " + (DELIVER_STATE) + " " + (DELIVER_ZIP_POSTAL));
    delroom.setText(DELIVER_ROOM);
    delsee.setText(DELIVER_TO_SEE);
    delphone.setText(DELIVER_PHONE);
    delinstructions.setText(DELIVER_SPECIAL_INSTR);

    ////   Fixing String To Display  ////
    //// Yes or No Instead of Y or N ////
    if (SIGNATURE_REQUIRED.matches("Y")){ 
        sigreq.setText("Yes");
    }
    if (SIGNATURE_REQUIRED.matches("N")){   
        sigreq.setText("No");       
    }


    Accept_Job_Button_Instructions();
    Deny_Job_Button_Instructions();

}



private void Deny_Job_Button_Instructions() {
    ///////////////// Deny Button ///////////////////////////////
    Button deny = (Button) findViewById(R.id.deny);
    deny.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            vib.vibrate(40);
            startActivity(new Intent(SingleMenuItemActivity.this, JobDenied.class));    //SingleMenuItemActivity.this.finish();
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);           
        }           
    });
}


private void Accept_Job_Button_Instructions() {
    ///////////////// Accept Button //////////////////////////////
    Button accept = (Button) findViewById(R.id.accept);
    accept.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            vib.vibrate(40);
        }       
    }); 
}

// First Initialize upon Starting Activity //
private void Get_Intents() {
    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    PAYS = in.getStringExtra(pays);
    JOB = in.getStringExtra(job);
    READY_AT = in.getStringExtra(ready_at);
    DUE_BY = in.getStringExtra(due_by);
    CUSTOMER_REFERENCE = in.getStringExtra(customer_reference);
    PIECES = in.getStringExtra(pieces); 
    WEIGHT = in.getStringExtra(weight); 
    SIGNATURE_REQUIRED = in.getStringExtra(signature_required);

    // PickUp Info
    PICKUP_NAME = in.getStringExtra(pickup_name);
    PICKUP_ADDR1 = in.getStringExtra(pickup_addr1);
    PICKUP_CITY = in.getStringExtra(pickup_city);
    PICKUP_STATE = in.getStringExtra(pickup_state);
    PICKUP_ZIP_POSTAL = in.getStringExtra(pickup_zip_postal);
    PICKUP_TO_SEE = in.getStringExtra(pickup_to_see);
    PICKUP_ROOM = in.getStringExtra(pickup_room);
    PICKUP_PHONE = in.getStringExtra(pickup_phone);
    PICKUP_SPECIAL_INSTR = in.getStringExtra(pickup_special_instr);

    //Deliver Info
    DELIVER_NAME = in.getStringExtra(deliver_name);
    DELIVER_ADDR1 = in.getStringExtra(deliver_addr1);
    DELIVER_CITY = in.getStringExtra(deliver_city); 
    DELIVER_STATE = in.getStringExtra(deliver_state);
    DELIVER_ZIP_POSTAL = in.getStringExtra(deliver_zip_postal); 
    DELIVER_TO_SEE = in.getStringExtra(deliver_to_see); 
    DELIVER_ROOM = in.getStringExtra(deliver_room);
    DELIVER_PHONE = in.getStringExtra(deliver_phone);
    DELIVER_SPECIAL_INSTR = in.getStringExtra(deliver_special_instr);

}


@Override
protected void onResume() {
    super.onResume();
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}

@Override
protected void onPause() {
    super.onPause();

    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}

@Override
public void onBackPressed() {

    Intent i = new Intent(SingleMenuItemActivity.this, AgentPortalActivity.class);
    startActivity(i); 
    SingleMenuItemActivity.this.finish();
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);   
}

@Override
protected void onRestart() {
    super.onRestart();
    Intent i = new Intent(this, AgentPortalActivity.class);
    startActivity(i);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    }
}

Here are some images to help explain the situation better. Job Activity

For example, I clicked on Job no. 1642554 in the ListView and it will send me into the Next Activity but as you can see the Job numbers are totally off (the second image with 1642769 is actually the last job inside the ListView and will always show me the last Listview)

Any suggestions?

Here are my logs after following what skUDA suggested:

07-17 08:37:17.158: W/dalvikvm(12096): threadid=1: thread exiting with uncaught exception (group=0x40015578)
 07-17 08:37:17.162: E/AndroidRuntime(12096): FATAL EXCEPTION: main
 07-17 08:37:17.162: E/AndroidRuntime(12096): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jetdelivery.mobile/com.jetdelivery.mobile.SingleMenuItemActivity}: java.lang.NullPointerException
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.os.Handler.dispatchMessage(Handler.java:99)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.os.Looper.loop(Looper.java:130)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.ActivityThread.main(ActivityThread.java:3687)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at java.lang.reflect.Method.invokeNative(Native Method)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at java.lang.reflect.Method.invoke(Method.java:507)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at dalvik.system.NativeStart.main(Native Method)
 07-17 08:37:17.162: E/AndroidRuntime(12096): Caused by: java.lang.NullPointerException
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at com.jetdelivery.mobile.SingleMenuItemActivity.onCreate(SingleMenuItemActivity.java:146)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 07-17 08:37:17.162: E/AndroidRuntime(12096):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
  07-17 08:37:17.162: E/AndroidRuntime(12096): 

Upvotes: 1

Views: 2628

Answers (2)

skUDA
skUDA

Reputation: 316

The problem is that your strings (JOB, PAYS, READY_AT, DUE_BY, etc) that you're sending into the intent are set to the last one because that was the last view that was created. Try

Object j = lv.getAdapter().getItem(position);

(where object is whatever data type your item is) in your onItemClickListener, and get the string values from there.

Edit: A breakdown in greater detail.

After further review of your code, I see an easy solution to implement. In your doInBackground method, you set each of your strings to whatever you got from the JSONArray. Since you send these strings through the intent, you end up with whatever the last job was that was downloaded and mapped. My suggestion: Take the JSONArray JOBS and declare it as a field variable in the Jobs class. Here it will be accessible by both your inner async class and throughout your Jobs class. Now, change your onItemClickListener to something like:

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long ide) {

        vib.vibrate(40);

        // Starting new intent
        try {
        Intent in = new Intent(getApplicationContext(),
                SingleMenuItemActivity.class);
        in.putExtra("jobInfo", JOBS.getJSONObject(position).toString());

        startActivity(in);
        Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
});

BlaineOmega is correct in questioning why you're putting so many data extras into your intent. Since you're working with JSONObjects, just send the JSONString to your next activity. Then create a global JSONObject and initialize it in your onCreate like so:

myJsonJob = new JSONObject(getIntent().getStringExtra("jobInfo"));

Now you can use the JSONObject methods to access your data directly instead of getting it from the intent and storing it in string variables, then setting your textviews with those string varriables.(Btw, you can pretty much cut out the whole string variable step no matter how you do it by just going

myTextView.setText(intent.getStringExtra("nameOfString"))

Like, in your code, it would be:

trackno.setText(in.getStringExtra(job)). 

instead of assigning the string to a variable then setting it with the variable. It cuts down on memory usage and the extra cost of the middle man.)

That should solve your problem.

Upvotes: 2

Joel
Joel

Reputation: 4772

It looks like a problem with your onItemClickListener. What you put into the intent is was is sent to the activity that is displaying your information (SingleMenuItemActivity). In the onItemClick() method, a position is passed to it. That position is the index that the user selected in your ListView. You just seem to be passing it whatever values happen to be stored in the strings.

Upvotes: 0

Related Questions