我正在开发一个Android应用程序,我需要显示拉丁词汇表的列表视图。我查看了许多教程和指南,并编写了自定义ArrayAdapter和模型,但列表视图中只有一个项目填充了我传递的值。以下是我认为相关的代码片段。
使用ViewHolder模式的我的ArrayAdapter
public class MyArrayAdapter extends ArrayAdapter<Vocab> {
private int layout;
public MyArrayAdapter(Context context, int resource, ArrayList<Vocab> objects) {
super(context, resource, objects);
layout = resource;
}
private static class ViewHolder {
TextView dict_entry;
TextView definition;
Button add;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
Vocab word = getItem(position);
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.dict_entry = (TextView)convertView.findViewById(R.id.TVdict_entry);
viewHolder.definition = (TextView)convertView.findViewById(R.id.TVdefinition);
viewHolder.add = (Button)convertView.findViewById(R.id.Badd);
viewHolder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
} else {
mainViewHolder = (ViewHolder)convertView.getTag();
mainViewHolder.dict_entry.setText(word.dict_entry);
mainViewHolder.definition.setText(word.definition);
}
return convertView;
}
}
我的词汇模型
public class Vocab {
public String dict_entry;
public String definition;
public String id;
public Button add;
public Vocab(JSONObject object){
try {
this.dict_entry = object.getString("dict_entry");
this.definition = object.getString("definition");
this.id = object.getString("id");
} catch ( JSONException e){
e.printStackTrace();
}
}
public static ArrayList<Vocab> fromJSON(JSONArray jsonObjects){
ArrayList<Vocab> words = new ArrayList<Vocab>();
for(int i = 0; i < jsonObjects.length(); i++){
try{
words.add(new Vocab(jsonObjects.getJSONObject(i)));
}catch (JSONException e){
e.printStackTrace();
}
}
return words;
}
}
我获得JSONArray并将其传递给Vocab construtor然后尝试填充我的LiveView的代码
vocab_json = response;
status.setText(response);
try {
JSONObject json = new JSONObject(response);
JSONArray jsonArray = json.getJSONArray("all_words");
ArrayList<Vocab> word_list = Vocab.fromJSON(jsonArray);
ArrayList<Vocab> arrayOfVocab = new ArrayList<Vocab>();
MyArrayAdapter adapter = new MyArrayAdapter(getBaseContext(), R.layout.vocab_list_item, arrayOfVocab);
adapter.addAll(word_list);
ListView listView = (ListView) findViewById(R.id.LVvocabwords);
listView.setAdapter(adapter);
}catch(JSONException e){
e.printStackTrace();
}
布局以防万一
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dict_Entry"
android:id="@+id/TVdict_entry"
android:layout_alignBottom="@+id/Badd"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Definition"
android:id="@+id/TVdefinition"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/Badd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/Badd"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
只有第一个元素具有更改的textviews的默认文本。任何帮助将不胜感激,我相信这是一个简单的我忽略。
答案 0 :(得分:3)
像这样更改您的适配器类。
public class MyArrayAdapter extends ArrayAdapter<Vocab> {
private int layout;
public MyArrayAdapter(Context context, int resource, ArrayList<Vocab> objects) {
super(context, resource, objects);
layout = resource;
}
private static class ViewHolder {
TextView dict_entry;
TextView definition;
Button add;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
Vocab word = getItem(position);
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.dict_entry = (TextView)convertView.findViewById(R.id.TVdict_entry);
viewHolder.definition = (TextView)convertView.findViewById(R.id.TVdefinition);
viewHolder.add = (Button)convertView.findViewById(R.id.Badd);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.dict_entry.setText(word.dict_entry);
viewHolder.definition.setText(word.definition);
viewHolder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}