更新列表视图

时间:2012-08-21 13:23:57

标签: android listview

我已经坚持了很长一段时间,这真的令人沮丧。基本上,应用程序以包含ListViewMovie TitlesGross的{​​{1}}开头。

然后,用户可以使用菜单中的其他活动添加新电影,毛重和年份。然后将值返回到第一个活动,并放在列表的底部。

这是我的问题开始的地方。我遇到的第一个问题是应用程序强制关闭它何时显示新项目。现在,它根本不想显示。这是代码:

Year

Flag public class Lab7_084106 extends ListActivity { private SampleCustomAdapter adapter; private ArrayList<MyMovies> movieList; public static boolean Flag = false; @SuppressWarnings("null") @Override public void onCreate(Bundle savedInstanceState) { //create stuff super.onCreate(savedInstanceState); movieList = new ArrayList<MyMovies>(); Intent data = getIntent(); //Flag = data.getStringExtra("Flag"); String[] oldMovieList = getResources().getStringArray(R.array.movieArray); String[] oldGrossList = getResources().getStringArray(R.array.worldwideGross); String[] oldYearList = getResources().getStringArray(R.array.yearArray); //if there's no new movie to display if(!Flag){ for (int i = 0; i < oldMovieList.length; i++) { MyMovies newMovie = new MyMovies(); newMovie.setMovie(oldMovieList[i] + "NEW"); newMovie.setGross(oldGrossList[i]); newMovie.setYear(oldYearList[i]); movieList.add(newMovie); } //adapter = new SampleCustomAdapter(movieList); //setContentView(R.layout.row); //setListAdapter(adapter); } else{ Toast.makeText(getApplicationContext(), "Else Entered", Toast.LENGTH_SHORT).show(); int newLength = 50; //oldMovieList.length + 1; //create new array to store the new value String[] newMovieArray = new String[newLength]; String[] newGrossArray = new String[newLength]; String[] newYearArray = new String[newLength]; //populate the new list with the old one plus the new one for (int i = 0; i < newLength; i++) { if( i != newLength - 1){ newMovieArray[i] = oldMovieList[i]; newGrossArray[i] = oldGrossList[i]; newYearArray[i] = oldYearList[i]; } else{ newMovieArray[i] = data.getStringExtra("Title"); newGrossArray[i] = data.getStringExtra("Gross"); newYearArray[i] = data.getStringExtra("Year"); } } //populate the old one using the new list for (int i = 0; i < newLength; i++) { oldMovieList[i] = newMovieArray[i]; oldGrossList[i] = newGrossArray[i]; oldYearList[i] = newYearArray[i]; } //display stuff for (int i = 0; i < newLength; i++) { MyMovies newMovie = new MyMovies(); newMovie.setMovie(oldMovieList[i]); newMovie.setGross(oldGrossList[i]); newMovie.setYear(oldYearList[i]); movieList.add(newMovie); } //adapter = new SampleCustomAdapter(movieList); //setListAdapter(adapter); } adapter = new SampleCustomAdapter(movieList); setListAdapter(adapter); ListView lv = getListView(); lv.setTextFilterEnabled(true); //set stuff such that Page2 sends back a result to page 1 lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView t = (TextView) view.findViewById(R.id.title); String name = (String) t.getText(); Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show(); } }); } private class SampleCustomAdapter extends BaseAdapter { private ArrayList<MyMovies> internalList; String[] oldMovieList = getResources().getStringArray(R.array.movieArray); String[] oldGrossList = getResources().getStringArray(R.array.worldwideGross); String[] oldYearList = getResources().getStringArray(R.array.yearArray); private ArrayList<MyMovies> GetSearchResults(){ ArrayList<MyMovies> results = new ArrayList<MyMovies>(); // make sure the arrays have the same length for (int i = 0; i < oldMovieList.length; i++) { MyMovies sr = new MyMovies(); sr.setMovie(oldMovieList[i]); sr.setGross(oldGrossList[i]); sr.setYear(oldYearList[i]); results.add(sr); } return results; } public SampleCustomAdapter(ArrayList<MyMovies> contacts){ internalList = contacts; } @Override public int getCount() { // TODO Auto-generated method stub return internalList.size(); } @Override public Object getItem(int index) { // TODO Auto-generated method stub return internalList.get(index); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // NOTE: you can only do this if you have access to the Activity object // which is why this is an inner class LayoutInflater inflater = getLayoutInflater(); View view; //System.out.println(parent.getClass().getName()); //System.out.println(position); if (convertView==null){ view = inflater.inflate(R.layout.row, null); } else{ view = convertView; } // extract the views to be populated TextView movie = (TextView) view.findViewById(R.id.title); TextView gross = (TextView) view.findViewById(R.id.gross); TextView date = (TextView) view.findViewById(R.id.date); // extract the object that will fill these MyMovies movies = GetSearchResults().get(position); //MyMovies movies = internalList.get(position); movie.setText(movies.getMovie()); gross.setText(movies.getGross()); date.setText(movies.getYear()); // return the view return view; } } //menu lawl @Override public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menupage1, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { //Handle item selection using item.getItemId() switch(item.getItemId()){ case R.id.addMovie: AddMovie(); break; } return true; } //end menu stuff lol public void AddMovie(){ Intent intent2 = new Intent(this, com.Android.Lab7.addMovie.class); startActivity(intent2); finish(); } } 变量基本上告诉用户是否添加了电影。如果用户添加了电影,它将进入else语句并从那里更新。我真的很困惑把这个Boolean声明放在哪里。

我使用if-else的{​​{1}}函数进行了一些实验,发现它直接影响GetSearchResult的输出。我尝试在那里放置SampleCustomAdapter语句,但我最终在ListView中添加了很多项。

使用if-else会出现NullPointerException错误并指向我放置它的位置。所以即使做了类似的事情:

ListView

作为adapter.notifyDataSetChanged();块,它不起作用。我认为这与我从string.xml资源文件夹中获取初始值而不是通过硬编码或用户输入这一事实有关。

这个问题自从2-3天以来一直令我感到沮丧,并且非常感谢帮助。感谢。

2 个答案:

答案 0 :(得分:2)

您只需通知适配器上已更改的数据集

movieList.add(newMovie);
adapter.notifyDataSetChanged();

列表视图将自动更新

<强>更新 你可以随时使用它,但我更喜欢通知适配器。

movieList.add(newMovie);
adapter = new SampleCustomAdapter(movieList);
setListAdapter(adapter);

答案 1 :(得分:0)

你应该使用 adapter.notifyDataSetChanged()。检测This Link