我是Android编程新手,无法使用自定义适配器向listview添加项目。我的程序包括列表,文本字段,微调器和按钮。目标是在按下按钮时将新项目添加到列表中(文本字段的内容作为名称,微调器选择作为类型)。问题是项目不会出现在列表中。我尝试过几个教程(lastly this one)但似乎没有任何帮助。以下是我的消息来源:
activity_list_view_demo.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"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewNum"
android:text="0"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TextView>
<Spinner
android:id="@+id/shop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/shop_name"
/>
<ListView
android:id="@+id/lista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/txtItem"
android:layout_gravity="bottom"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Esine"
/>
<Spinner
android:id="@+id/type"
android:layout_gravity="bottom"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:entries="@array/type_name"
/>
<Button
android:id="@+id/btnAdd"
android:layout_gravity="bottom"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Lisää"
/>
</LinearLayout>
</LinearLayout>
listitem_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewName"
android:text="TextView"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</TextView>
<TextView
android:text="TextView"
android:id="@+id/textViewType"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
</LinearLayout>
ListViewAdapter.java
package com.example.testi2;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends ArrayAdapter<ShoppingList>
{
Context context;
ArrayList<ShopItem> items;
int layoutResourceId = 0;
public ListViewAdapter(Context context, ShoppingList list, int resourceId) {
super(context, R.layout.listitem_row);
this.context = context;
this.layoutResourceId = resourceId;
for (int i = 0; i < list.getLength() - 1; i++) {
items.add(list.getItem(i));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.listitem_row, null);
}
ShopItem i = items.get(position);
if (i != null) {
TextView nameView = (TextView) rowView.findViewById(R.id.textViewName);
TextView typeView = (TextView) rowView.findViewById(R.id.textViewType);
nameView.setText(i.getName());
typeView.setText(i.getType());
}
return rowView;
}
}
ListViewDemo.java
package com.example.testi2;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class ListViewDemo extends Activity {
private Spinner typeSpinner;
private ListViewAdapter adapter;
private ListView lview;
private ShoppingList shoppinglist;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_list_view_demo);
shoppinglist = new ShoppingList();
shoppinglist.addItem("Banaani", "Hedelmä");
createAddBtn();
createList();
typeSpinner = (Spinner) findViewById(R.id.type);
}
public void createList() {
lview = (ListView) findViewById(R.id.lista);
adapter=new ListViewAdapter(this, shoppinglist, R.layout.listitem_row);
lview.setAdapter(adapter);
}
public void createAddBtn() {
Button btn = (Button) findViewById(R.id.btnAdd);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
String editStr = edit.getText().toString();
String spinnerStr = typeSpinner.getSelectedItem().toString();
TextView numView = (TextView) findViewById(R.id.textViewNum);
if (editStr.length()>0) {
shoppinglist.addItem(editStr, spinnerStr);
edit.setText("");
adapter.notifyDataSetChanged();
numView.setText(String.valueOf(shoppinglist.getItems().length));
}
}
};
btn.setOnClickListener(listener);
}
}
任何想法我做错了什么?
答案 0 :(得分:1)
首先,我建议你为任何适配器(http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder)实现ViewHolder模式,但这超出了这个问题的范围。
要回答您的问题:您应该查看数组适配器获取其项目的位置。在您的实现构造函数中,您只需复制所有项目即可破坏购物清单和项目数组之间的任何依赖关系。这导致购物清单中的任何添加不会转移到适配器项目列表。因此,notifyDatasetChanged将无效。
要解决此问题,请尝试使用ArrayAdapters三参数构造函数,该构造函数也采用ArrayList,以便您可以在适配器的数据集和活动中的购物清单之间保持硬依赖关系。
假设ShoppingList只是扩展ArrayList<ShopItem>
,这就像
public ListViewAdapter(Context context, ShoppingList list, int resourceId) {
super(context, 0, list);
this.context = context;
this.layoutResourceId = resourceId;
}
此外,您的ArrayAdapter应该扩展ArrayAdapter<ShopItem>
,而不是ArrayAdapter<ShoppingList>
。通用应该是要显示的项目类型,而不是列表。