gpt4 book ai didi

Android ListView 适配器在点击时显示最后一项

转载 作者:行者123 更新时间:2023-11-29 22:03:03 26 4
gpt4 key购买 nike

我的 ListView 适配器有问题,一切都通过 json 解析到 ListView 中,一切正常,但是当我单击 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 Activity

例如,我点击了作业编号。 1642554 在 ListView 中,它会将我发送到下一个 Activity ,但如您所见,作业编号完全关闭(第二张带有 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):

最佳答案

问题是您发送到 Intent 中的字符串(JOB、PAYS、READY_AT、DUE_BY 等)被设置为最后一个,因为这是最后创建的 View 。尝试

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

(其中对象是您的项目的任何数据类型)在您的 onItemClickListener 中,并从那里获取字符串值。

编辑:更详细的分割。

在进一步审查您的代码后,我发现了一个易于实现的解决方案。在您的 doInBackground 方法中,您将每个字符串设置为您从 JSONArray 获得的任何内容。由于您通过 Intent 发送这些字符串,因此您最终会得到下载和映射的最后一个作业。我的建议:采用 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 质疑您为何将如此多的额外数据放入您的 Intent 中是正确的。由于您正在使用 JSONObjects,只需将 JSONString 发送到您的下一个 Activity 。然后创建一个全局 JSONObject 并在你的 onCreate 中初始化它,如下所示:

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

现在您可以使用 JSONObject 方法直接访问您的数据,而不是从 Intent 中获取数据并将其存储在字符串变量中,然后使用这些字符串变量设置您的 TextView 。(顺便说一句,您几乎可以删除整个字符串可变的步骤,无论你如何去做,都可以

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

例如,在您的代码中,它将是:

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

不是将字符串分配给变量然后用变量设置它。它减少了内存使用和中间人的额外成本。)

这应该可以解决您的问题。

关于Android ListView 适配器在点击时显示最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com