我想在列表视图中显示一个JSONObject。
$ awk '/##/{f=1; next} f{print ">"FILENAME; print; f=0}' *.txt > output.txt
JSON
private void showStudentInfo(final List<StudentDetails> studentDetails) {
String userNameValue = getIntent().getStringExtra("name");
if(studentDetails != null) {
final ArrayList<Map<String, Object>> itemDataList = new ArrayList<>();
int size = studentDetails.size();
for(int i=0; i<size; i++)
{
StudentDetails student = studentDetails.get(i);
Map<String, Object> listItemMap = new HashMap<>();
listItemMap.put("id", student.getId());
listItemMap.put("name", student.getId());
listItemMap.put("email", student.getId());
listItemMap.put("phone", student.getId());
listItemMap.put("addresS", student.getId());
itemDataList.add(listItemMap);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, itemDataList, R.layout.list_row,
new String[]{"id", "name","email","phone","addresS"},
new int[]{R.id.id_textview,
R.id.name_textview,
R.id.email_textview,
R.id.phone_textview,
R.id.address_textview
});
}
}
OnClickListener
[{
"id": "1",
"name": "Ramesh",
"email": "Ramesh@gmail.com",
"phone": "123456789",
"addresS": "blah blah blah"
},
{
"id": "2",
"name": "Mahesh",
"email": "Mahesh@gmail.com",
"phone": "123456789",
"addresS": "blah blah blah"
},
....
]
我想要的是,如果用户在上一个活动中单击例如-mahesh,那么我想从json文件中获取该名称的详细信息-id,name,email等...,并将其显示在列表视图中。
StudentManagerInterface
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
selectedItem = studentDetails.get(position).getName();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setPositiveButton("View", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("name", selectedItem);
startActivity(intent);
}
});
学生详细信息
@GET("rnr0k")
public Call<List<StudentDetails>> getUserByName(
@Query("name") String studentNameValue
);
StudentManager
Getter and Setter methods.
我正在使用翻新产品,而我是新手。感谢@TaQuangTu的回答,但是我很困惑,因为我没有使用JSONArray。 我必须对代码进行哪些更改?
答案 0 :(得分:1)
假设您有Json字符串。例子
String mJson = "[{
"id": "1",
"name": "Ramesh",
"email": "Ramesh@gmail.com",
"phone": "123456789",
"addresS": "blah blah blah" },
{
"id": "2",
"name": "Mahesh",
"email": "Mahesh@gmail.com",
"phone": "123456789",
"addresS": "blah blah blah" },
.... ]"
然后,创建一个数组列表以存储Json字符串中的StudentDetails
个对象:
ArrayList<User> getArrayList(String mJson)
{
ArrayList<User> studentArrayList= new ArrayList<>();
try
{
JSONArray jsonArray = new JSONArray(mJson);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject student= (JSONObject) jsonArray.getJSONObject(i);
StudentDetails studentId= student.getInt("id");
String studentName= student.getString("name");
//get more fields if you want
studentArrayList.add(new StudentDetails(userId,name,/*parameter*/));//assuming class StudentDetails have the constructor
}
} catch (JSONException e) {
e.printStackTrace();
}
return studentArrayList;
}
然后,为每个listview项目创建一个简单的布局,如下所示: 文件layout_studen_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_name"
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#161616"/>
<TextView
android:id="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000" />
</LinearLayout>
然后,使用上述布局为列表视图创建自定义适配器。
public class StudentAdapter extends BaseAdapter {
private Context mContext;
private int mIdResourceLayout;
private List<StudentDetails> StudentList;
public StudentAdapter(Context mContext, int mIdResourceLayout, List<StudentDetails> StudentList)
{
this.mContext = mContext;
this.mIdResourceLayout = mIdResourceLayout;
this.studentList= studentList;
}
@Override
public int getCount() {
return studentList.size();
}
@Override
public Object getItem(int i) {
return studentList.get(i);
}
@Override
public long getItemId(int i) {
return studentList.get(i).getmId(); //getmID is a method of StudentDetails class
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
ViewHolder viewHolder = null;
if(view==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(mIdResourceLayout,null);
viewHolder = new ViewHolder();
viewHolder.mTextViewName = view.findViewById(R.id.tv_name);
viewHolder.mTextViewId = view.findViewById(R.id.tv_id);
view.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.mTextViewId.setText("Id: "+studentList.get(i).getmId());
viewHolder.mTextViewName.setText("Name: "+studentList.get(i).getmName());//getmName is a method of class StudentDetails
return view;
}
private class ViewHolder
{
TextView mTextViewName,mTextViewId;
}
}
然后,在mainActivity中,假设您有一个列表视图,让我们为其设置适配器。
ArrayList<StudentDetails> studentList = getArrayList(mJson); //mJson is the json file that i have mentioned above
StudentAdapter adapter= new StudentAdapter(MainActivity.this,R.layout.layout_student_item,studentList);
mListViewUser.setAdapter(adapter);
现在,您已经在ArrayList studentList
中获得了所有学生信息,一切都变得很容易。
答案 1 :(得分:0)
您的代码除以下部分外几乎是正确的。
Map<String,Object> listItemMap = new HashMap<>();
listItemMap.get(student.getId()); // here is the problem
listItemMap.get(student.getName()); // here is the problem
itemDataList.add(listItemMap);
您从空的(或新创建的)HashMap
中获取值(id和名称),而不是向其中添加数据。像下面这样的简单更改可能会解决您的问题(因为我还没有亲自测试代码)。
Map<String, Object> listItemMap = new HashMap<>();
listItemMap.put("id", student.getId()); // use put() with key "id"
listItemMap.put("name", student.getName()); // use put() with key "name"
itemDataList.add(listItemMap);
如果以上指定的解决方案不能解决问题,建议您调试代码并确保studentDetails
(ArrayList)不为空。这意味着您的API正在返回正确的数据。
修改
要仅显示名称,只需进行以下更改。
SimpleAdapter simpleAdapter = new SimpleAdapter(this, itemDataList,R.layout.list_row,
new String[]{"name"}, new int[]{R.id.name_textview});
从适配器中删除ID值(仅传递名称)。并从XML布局中删除id_textview
TextView。就是这样!
祝你好运!