我的ListView适配器有问题,Everything正在被解析VIA json进入列表视图很好但是当我点击ListView时,它总是向我显示ListView中最后解析的json的结果。
这是我的第一个名为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);
}
}
下面是第二课,当您单击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);
}
}
以下是一些有助于更好地解释情况的图片。
例如,我点击了Job no。在ListView中1642554,它会将我发送到下一个活动,但是你可以看到作业号完全关闭(第二个图像1642769实际上是ListView中的最后一个作业,并且总是显示最后一个Listview)
有什么建议吗?
按照skUDA的建议,以下是我的日志:
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):
答案 0 :(得分:2)
问题是你发送到意图中的字符串(JOB,PAYS,READY_AT,DUE_BY等)被设置为最后一个,因为那是最后创建的视图。试试
Object j = lv.getAdapter().getItem(position);
在onItemClickListener中(其中object是你的项目的任何数据类型),并从那里获取字符串值。
编辑:更详细的细分。
在进一步审查您的代码之后,我看到了一个易于实现的解决方案。在doInBackground方法中,将每个字符串设置为从JSONArray获得的任何字符串。由于您通过意图发送这些字符串,因此您最终会得到下载和映射的最后一个作业。我的建议:获取JSONArray JOBS并将其声明为Jobs类中的字段变量。在这里,您的内部异步类和整个Jobs类都可以访问它。现在,将onItemClickListener更改为:
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在质疑你为什么要在你的意图中加入如此多的数据时是正确的。由于您正在使用JSONObjects,只需将JSONString发送到您的下一个活动。然后创建一个全局JSONObject并在onCreate中初始化它,如下所示:
myJsonJob = new JSONObject(getIntent().getStringExtra("jobInfo"));
现在,您可以使用JSONObject方法直接访问数据,而不是从意图中获取数据并将其存储在字符串变量中,然后使用这些字符串变量设置textviews。(顺便说一下,你几乎可以删除整个字符串变量步骤,无论你如何做到这一点
myTextView.setText(intent.getStringExtra("nameOfString"))
比如,在您的代码中,它将是:
trackno.setText(in.getStringExtra(job)).
而不是将字符串赋值给变量,然后使用变量设置它。它减少了内存使用量和中间人的额外成本。)
这应该可以解决你的问题。
答案 1 :(得分:0)
您的onItemClickListener
似乎有问题。你投入到意图中的是被发送到显示你的信息的活动(SingleMenuItemActivity
)。在onItemClick()
方法中,会向其传递一个位置。该位置是用户在ListView中选择的索引。无论发生在字符串中的值是什么,你似乎都在传递它。