I use bundle before but it only return null,
how can i get the name of the student_name that populated from database using json of the clicked item and show it into new activity?
public void ListDrawer() {
final List<Map<String, String>> studentList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("student_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("student_name");
String number = jsonChildNode.optString("student_id");
String outPut = name + "-" + number;
studentList.add(createStudent("Students", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
////////////////////////////////// UPDATE LISTVIEW ITEMS ONCLICK
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do your logic for getting the student variables here
Intent intent = new Intent(MainPage.this,Profile.class);
intent.putExtra("student ", String.valueOf(id));
startActivity(intent);
}
});
//////////////////////////////////////UPDATE
SimpleAdapter simpleAdapter = new SimpleAdapter(this, studentList,
android.R.layout.simple_list_item_1,
new String[] { "Students" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
Toast.makeText(getApplication(), "Logged in Successfully", Toast.LENGTH_SHORT).show();
Above is mylistview and my onItemClicked function
i use json to retrieve the list of students from mysql and view it in listview and now im trying to pass data from the selected student in listview to new activity
[1]: http://i.stack.imgur.com/zc56O.jpg
答案 0 :(得分:0)
要添加@brahmyadigopula评论,使用POJO
会更简单,如果您已经使用JSON作为首选方式,则可以使用Google视图库将JSON strings
转换为Objects
用一行代码。
https://github.com/google/gson
然后你可以将对象转换为具有相同库的JSON String,并将其作为String传递给Intent,并将它作为字符串“捕获”在Activity中并将其转换回Object并使用它作为你的愿望。
它看起来像这样:
public class User {
private String name;
private String number;
(getters/setters)
}
然后在你的适配器中你会做:
User user = new Gson().fromJson(jsonMainNode, User.class);
这样您就可以在获取数据时获得更清晰的代码。因此,当使用intent传递数据时,您可以通过执行以下操作将User
对象转换为字符串:String jsonString = new Gson().toJson(user);
并将其传递给intent。
希望这有帮助。
答案 1 :(得分:0)
对于您的适配器,您正在使用&#34; android.R.layout.simple_list_item_1&#34;作为列表项的布局。这将为您提供一个简单的TextView,此TextView将包含整个学生信息(名称编号)作为完整的String变量。 我有3种解决方案可以解决您的问题:
1-获取项目TextView的文本并使用拆分功能获取学生信息,如下所示:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get the list item subviews using IDs of the custom item layout
TextView tvStudentName = (TextView)view.findViewById(R.id.tvStudentName);
TextView tvStudentNumber = (TextView)view.findViewById(R.id.tvStudentNumber);
//Create the intent to start the Profile activity
//Add student info to extras
Intent intent = new Intent(MainPage.this,Profile.class);
intent.putExtra("studentName", tvStudentName.getText());
intent.putExtra("studentID", tvStudentNumber.getText());
startActivity(intent);
}
});
2-为ListView适配器创建一个自定义布局,该适配器将在LinearLayout中包含2个TextView,一个用于名称,一个用于数字。然后,您可以轻松地分别获取每个信息:
public class MainPage extends Activity {
//Declare studentList as a global variable
List<Map<String, String>> studentList = new ArrayList<>();
.
.
.
//Change the structure of your ListDrawer method
public void ListDrawer() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("student_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("student_name");
String number = jsonChildNode.optString("student_id");
//Add the student info to a new Hashmap object
//Add the student to the array
Map<String, String> studentInfo = new HashMap<>();
studentInfo.put("student_name", name);
studentInfo.put("student_id", number);
studentList.add(i, studentInfo);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
}
}
3-将学生列表作为全局变量,然后直接从数组中获取信息:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Create the intent to start the Profile activity
//Add student info to extras
Intent intent = new Intent(MainPage.this,Profile.class);
intent.putExtra("studentName", studentList.get(position).get("student_name"));
intent.putExtra("studentID", studentList.get(position).get("student_id"));
startActivity(intent);
}
});
然后,将数据发送到个人资料活动:
public class Profile extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
Log.e("EXTRA", "Student Name : " + getIntent().getExtras().getString("studentName"));
Log.e("EXTRA", "Student ID : " + getIntent().getExtras().getString("studentID"));
}
}
最后,检索从MainPage Activity发送到Profile Activity的信息:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<thead>
<tr>
<th>Standard Name</th>
<th>Subject Name</th>
<th>Test Name</th>
<th>Total Test Mark</th>
<th>DateTime</th>
<th>Percentage</th>
<th colspan="6">Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $key => $value): ?>
<?php $correct_answer = 0; ?>
<?php $wrong_answer = 0; ?>
<?php $per_question_mark = ($value['total_test_mark'] / $value['examination_test_question']); ?>
<tr>
<td><?php echo $value['standard_name']; ?></td>
<td><?php echo $value['subject_name']; ?></td>
<td><?php echo $key; ?></td>
<td><?php echo $value['total_test_mark']; ?></td>
<td><?php echo date("d-F-Y H:i:s", strtotime($value['time_limit'])); ?></td>
<?php foreach ($value['is_correct'] as $value1): ?>
<?php if ($value1['is_correct'] == 1): ?>
<?php $correct_answer+= $value1['is_correct']; ?>
<?php endif; ?>
<?php if ($value1['is_correct'] == 0): ?>
<?php $wrong_answer+= 1; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php $obtain_mark = ($correct_answer * $per_question_mark); ?>
<?php $percentage = number_format(($obtain_mark / $value['total_test_mark'] * 100), 2); ?>
<td><?php echo $percentage; ?> %</td>
<?php //if ($percentage != 0): ?>
<td> <a href="<?php echo base_url(); ?>student/Examinations/view_answersheet/<?php echo $value['examination_test_id']; ?>" class="btn btn-primary">View AnswerSheet</a></td>
<?php //endif; ?>
<?php // if ($percentage == 0): ?>
<td> <a href="<?php echo base_url(); ?>student/Examinations/apply_examinations/<?php echo $value['examination_test_id']; ?>" class="btn btn-primary" id="apply" onclick="resetCountdownInLocalStorage();">Apply</a></td>
<?php //endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script>
function resetCountdownInLocalStorage() {
sessionStorage.removeItem("seconds");
}