我正在尝试创建一个自定义ListView,我想我正确地制作了我的适配器。 我对android很新,并且不太了解所有带有视图和适配器等的概念。
我的代码全部是在片段中完成的,而不是主要的活动。我不知道这是否会影响您将内容传递给列表的方式。
我正在尝试显示从JSON格式的字符串中获取的信息。当我运行我的应用程序时,列表永远不会显示,我不知道它是否因为我传递了错误的视图或类似的东西。它没有崩溃。
personal_profile_fragment:
public class Personal_Profile_Fragment extends Fragment {
private charAdapter adapter;
ListView list;
ArrayList<charactors> charactorList;
public Personal_Profile_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_personal__profile_, container, false);
Bundle bundle = getArguments();
String JSON = bundle.getString("JSON");
parseJson(JSON);
list = (ListView)v.findViewById(R.id.list);
charactorList = new ArrayList<charactors>();
adapter = new charAdapter(getContext(), charactorList);
list.setAdapter(adapter);
return v;
}
private void parseJson(String json) {
try {
JSONObject chars = new JSONObject(json);
JSONArray charArray = chars.getJSONArray("heroes");
for (int i = 0; i<charArray.length(); i++) {
JSONObject realCharacter = charArray.getJSONObject(i);
charactors charactor = new charactors();
charactor.setName(realCharacter.getString("name"));
charactor.setClassType(realCharacter.getString("class"));
charactor.setLevel(realCharacter.getString("level"));
charactor.setParagonLevel(realCharacter.getString("paragonLevel"));
charactor.setHardcore(realCharacter.getString("hardcore"));
charactor.setSeasonal(realCharacter.getString("seasonal"));
charactorList.add(charactor);
}
} catch(Exception e) {
}
}
}
personal_profile.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/personalProfileFragment"
tools:context="com.hfad.diablo3assistant.Personal_Profile_Fragment">
<!-- TODO: Update blank fragment layout -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="368dp"
android:layout_height="551dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginTop="34dp"
android:text="TextView" />
<TextView
android:id="@+id/classType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="12dp"
android:text="TextView" />
<TextView
android:id="@+id/level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/classType"
android:layout_below="@+id/classType"
android:layout_marginTop="15dp"
android:text="TextView" />
<TextView
android:id="@+id/paragonLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/level"
android:layout_below="@+id/level"
android:layout_marginTop="15dp"
android:text="TextView" />
<TextView
android:id="@+id/hardcore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/paragonLevel"
android:layout_below="@+id/paragonLevel"
android:layout_marginTop="13dp"
android:text="TextView" />
<TextView
android:id="@+id/seasonal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/hardcore"
android:layout_below="@+id/hardcore"
android:layout_marginTop="20dp"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>
charactors class:
public class charactors {
private String name;
private String classType;
private String level;
private String paragonLevel;
private String hardcore;
private String seasonal;
public charactors() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getParagonLevel() {
return paragonLevel;
}
public void setParagonLevel(String paragonLevel) {
this.paragonLevel = paragonLevel;
}
public String getHardcore() {
return hardcore;
}
public void setHardcore(String hardcore) {
this.hardcore = hardcore;
}
public String getSeasonal() {
return seasonal;
}
public void setSeasonal(String seasonal) {
this.seasonal = seasonal;
}
}
Charactor适配器:
public class charAdapter extends ArrayAdapter<charactors> {
private final Context context;
private final ArrayList<charactors> charactorsArrayList;
public charAdapter(Context context, ArrayList<charactors> charactorsArrayList) {
super(context, R.layout.row, charactorsArrayList);
this.context = context;
this.charactorsArrayList = charactorsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.row, parent, false);
// 3. Get the two text view from the rowView
TextView name = (TextView) rowView.findViewById(R.id.name);
TextView classType = (TextView) rowView.findViewById(R.id.classType);
TextView level = (TextView) rowView.findViewById(R.id.level);
TextView paragonLevel = (TextView) rowView.findViewById(R.id.paragonLevel);
TextView hardcore = (TextView) rowView.findViewById(R.id.hardcore);
TextView seasonal = (TextView) rowView.findViewById(R.id.seasonal);
// 4. Set the text for textView
name.setText("Name: "+charactorsArrayList.get(position).getName());
classType.setText("Class: "+charactorsArrayList.get(position).getClassType());
level.setText("Level: "+charactorsArrayList.get(position).getLevel());
paragonLevel.setText("Paragon: "+charactorsArrayList.get(position).getParagonLevel());
hardcore.setText("Hardcore: "+charactorsArrayList.get(position).getHardcore());
classType.setText("Seasonal: "+charactorsArrayList.get(position).getSeasonal());
// 5. retrn rowView
return rowView;
}
}
答案 0 :(得分:0)
您的适配器的getView()
实施效率低下,但它看起来功能正确。我认为问题只是onCreateView()
中的一个简单错误:
parseJson(JSON); ... charactorList = new ArrayList<charactors>(); adapter = new charAdapter(getContext(), charactorList);
看起来你正在解析JSON字符串,然后通过将new ArrayList
分配给charactorList
来立即抛弃解析工作。
实际上,看得更深一点,当charactorList
正在运行时parseJson()
似乎是空的(但是catch
块正在隐藏它)。所以你只想重新安排一下这段代码:
charactorList = new ArrayList<charactors>();
parseJson(JSON);
...
adapter = new charAdapter(getContext(), charactorList);