2014-10-07 248 views
0

我想在運行時添加項目到放置在選項卡上的ListView。 但是當我這樣做,我得到一個錯誤。有3個標籤,我不使用ListFragments,因爲我已經在所有選項卡將項目添加到ListView

我的代碼如下

SwipeTabFragment.java

public class SwipeTabFragment extends Fragment { 

    private String tab; 
    private int color; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle bundle = getArguments(); 
     tab = bundle.getString("tab"); 
     color = bundle.getInt("color"); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.bookshelf, null); 
     TextView tv = (TextView) view.findViewById(R.id.textView); 
     tv.setText(tab); 
     view.setBackgroundResource(color); 
     return view; 
    } 
} 

TabPagerAdapter.java

public class TabPagerAdapter extends FragmentPagerAdapter { 

    public TabPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int index) { 
     Bundle bundle = new Bundle(); 
     String tab = ""; 
     int colorResId = 0; 
     SwipeTabFragment swipeTabFragment = new SwipeTabFragment(); 
     switch (index) { 
      case 0: 
       tab = "BookShelf"; 
       bundle.putString("tab",tab); 
       swipeTabFragment.setArguments(bundle); 
       return swipeTabFragment; 


       // break; 
      case 1: 
       tab = "Now Listening"; 
       colorResId = R.color.color2; 
       BookFragment bookFragment = new BookFragment(); 
       return bookFragment; 
       //break; 
      case 2: 
       tab = "Library"; 
       colorResId = R.color.color3; 
       TabLibrary tabLibrary = new TabLibrary(); 
       return tabLibrary; 
       break; 
     } 

     return null; 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 
} 
不同的內容

TabLibrary.java

public class TabLibrary extends Fragment { 

    final String LOG_TAG = "myLogs"; 
    ListView lvLibList; 
    ImageButton btAddItem; 
    ArrayList<String> list = new ArrayList<String>(); 
    ArrayAdapter<String> adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.library, parent, false); 

     ListView lvLibList = (ListView)v.findViewById(R.id.lvLibList); 
     ImageButton btn = (ImageButton)v.findViewById(R.id.btAddItem); 
     adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list); 
     View.OnClickListener listener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       EditText edit = (EditText)v.findViewById(R.id.eAddItem); 
       list.add(edit.getText().toString()); 
       edit.setText(""); 
       adapter.notifyDataSetChanged(); 
      } 
     }; 
     btn.setOnClickListener(listener); 
     lvLibList.setAdapter(adapter); 
     return v; 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 
} 
+2

什麼錯誤?發佈logcat以及 – 2014-10-07 16:36:54

+0

只是一個猜測 - 你可以嘗試在'TabLibrary.java'中嘗試使用'adapter.add(edit.getText()。toString())'而不是'list.add(...)'嗎?此外,這不會導致錯誤 - 所以發佈錯誤將幫助我們爲您提供比野生猜測更好的解決方案。 – 2014-10-07 16:55:59

+0

在第一篇文章中完成 – RyndinV 2014-10-07 16:56:25

回答

0

@Gurusharan S是對的,問題是關於變量的名稱。爲了避免混淆,我建議你的代碼改成這樣:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.library, parent, false); //Change the name from v to view 

    ListView lvLibList = (ListView)view.findViewById(R.id.lvLibList); 
    ImageButton btn = (ImageButton)view.findViewById(R.id.btAddItem); 
    EditText edit = (EditText)view.findViewById(R.id.eAddItem); //Initialize the EditText outside of the click method 

    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list); 

    View.OnClickListener listener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      list.add(edit.getText().toString()); 
      edit.setText(""); 
      adapter.notifyDataSetChanged(); 
     } 
    }; 
    btn.setOnClickListener(listener); 
    lvLibList.setAdapter(adapter); 
    return view; 
} 

而在未來我建議,給你的變量更有意義的名稱,有時需要你多一點的編碼,但它是值得的。

+0

//錯誤:(47,26)錯誤:從內部類訪問局部變量編輯;需要宣佈最終// 我想宣佈,但我不明白。 – RyndinV 2014-10-07 18:07:36

+0

'最後EditText編輯=(EditText)view.findViewById(R.id.eAddItem);' 這應該做 – 2014-10-07 20:15:36

+0

偉大的THX!是工作! – RyndinV 2014-10-08 03:05:24

0

發表我的評論作爲答案,因爲我敢肯定這是問題:

變量edit似乎爲空。這是因爲當你打電話給v.findViewById時,你實際上是在被點擊的視圖上調用它,而不是片段的視圖。

所以,你有兩個選擇:

  1. 更改片段的視圖的名稱。例如。 View v1 = inflater.inflate(R.layout.library, parent, false);,然後將該行更改爲v1.findViewById
  2. 更改onClick參數的名稱。例如。 public void onClick(View v1) {

希望有所幫助。

+0

View.OnClickListener listener = new View.OnClickListener(){ @Override public void onClick(View v1){ EditText eAddItem =(EditText)v1.findViewById(R.id.eAddItem); list.add(eAddItem.getText()。toString()); adapter.notifyDataSetChanged(); } }; – RyndinV 2014-10-07 17:37:27

+0

它不工作,但當我只是添加list.add(「123」),而不是list.add(eAddItem.getText()。toString())它的工作 – RyndinV 2014-10-07 17:40:35

+0

你怎麼看list.add(eAddItem.getText() .toString())? – RyndinV 2014-10-07 17:41:24