此代码使用listView中的wamp服务器从数据库中获取数据,但是如果我单击第1行它会在第二个活动上显示第1行,如果我单击2它在第二个活动上显示第2行,我必须这样做
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class attendence extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendence);
String name = getIntent().getExtras().getString("username");
getJSON("http://192.168.0.102/A/select.php" + "?username=" + name);
listView = (ListView) findViewById(R.id.LV);
// TextView txtname = (TextView) findViewById(R.id.txt_success_name);
//
// Intent intent = getIntent();
// String loginName = intent.getStringExtra("username");
// txtname.setText("Welcome, " +loginName);
}
private void getJSON(final String urlWebService) {
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
//
private void loadIntoListView(String json) throws JSONException {
JSONObject object = new JSONObject(json);
JSONArray jsonArray = object.getJSONArray("Result");
String[] heroes = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
heroes[i] = obj.getString("ID") + " " + obj.getString("STATUS") + " " + obj.getString("ATD_DATE");
if (!obj.getString("STATUS").toString().equals("A")) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(attendence.this, atd_leave_form.class);
startActivity(i);
}
});
} else {
}
// Toast.makeText(getApplication(),"GREEN",Toast.LENGTH_LONG).show();
// listView.setBackgroundColor(Color.GREEN);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, heroes);
listView.setAdapter(arrayAdapter);
}
}
我只想显示在android中单击列表视图的相同单个数据。 提前感谢您的帮助。
答案 0 :(得分:0)