我有一個使用自定義適配器的列表視圖,並且我試圖向列表中添加步驟,但是我嘗試使用按鈕按鈕添加的步驟未被添加。如果這是愚蠢的,我對此很新,所以很抱歉。使用帶按鈕的自定義適配器添加到ListView
ListClass 公共類RecipeListActivity延伸ActionBarActivity {
RecipeAdapter myCustomAdapter;
List<String> myStepsList;
Button addTextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_list);
final ListView recipeStepsList = (ListView)findViewById(R.id.mylist);
myStepsList = new ArrayList<String>();
myCustomAdapter = new RecipeAdapter(this, myStepsList);
recipeStepsList.setAdapter(myCustomAdapter);
myCustomAdapter.add("Step 1");
addToAdapter("Step 2");
myStepsList.add("Step 3");
final Button addTextStepButton = (Button)findViewById(R.id.addbutton);
View.OnClickListener HandlerAdd = new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("Step 4 should be added");
addToAdapter("Step 4");
}
};
addTextStepButton.setOnClickListener(HandlerAdd);
}
public void addToAdapter(String text)
{
myCustomAdapter.add(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
在此步驟1,2和3加入,並在列表中顯示,並打印系統輸出,然而步驟4不添加。如果我在按鈕中調用notifyDatasetChanged(),它會崩潰。
定義適配器
public class RecipeAdapter extends BaseAdapter
{
private List<String> steps;
private Context myContext;
private LayoutInflater mlayout = null;
public RecipeAdapter(Activity context, List<String> mlist)
{
this.steps = mlist;
this.myContext = context;
mlayout = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return steps.size();
}
@Override
public Object getItem(int i) {
return steps.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public void add(String text){
this.steps.add(text);
}
public void updateRecipeList(List<String> newList)
{
steps.clear();
steps.addAll(newList);
this.notifyDataSetChanged();
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View v = convertView;
recipeStepHolder viewHolder;
if(convertView == null){
LayoutInflater li = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.recipe_text, null);
viewHolder = new recipeStepHolder(v);
}else{
viewHolder = (recipeStepHolder) v.getTag();
}
viewHolder.recipeText.setText(steps.get(i));
return v;
}
}
class recipeStepHolder{
public TextView recipeText;
public recipeStepHolder(View base){
recipeText = (TextView)base.findViewById(R.id.recipeText);
}
}
總之我要上addTextStepButton的新聞添加到列表視圖,但我做錯了什麼。非常感謝任何幫助!
編輯系統崩潰日誌:
07-07 19:14:50.508 1930-1930/com.example.cookclock.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.cookclock.app.RecipeAdapter.getView(RecipeAdapter.java:75)
at android.widget.AbsListView.obtainView(AbsListView.java:2251)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillSpecific(ListView.java:1318)
at android.widget.ListView.layoutChildren(ListView.java:1600)
at android.widget.AbsListView.onLayout(AbsListView.java:2102)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
這成爲一個很長的帖子,但是從這裏有點過提示我解決了,在一個更簡單的方法重做我GetView:
public View getView(int i, View convertView, ViewGroup viewGroup) {
LayoutInflater li = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = li.inflate(R.layout.recipe_text, viewGroup, false);
TextView stepText = (TextView)rowView.findViewById(R.id.recipeText);
stepText.setText(steps.get(i).toString());
return rowView;
當您添加一個項目時,您的應用程序應該知道它應該重畫屏幕以顯示這些新添加的項目。爲此,您必須在適配器上調用notifyDataSetChanged(),我認爲您錯過了。參考下面Gabe Sechan給出的答案。 – Jimmy