2012-08-21 109 views
0

我一直坚持这个很长一段时间,这真的令人沮丧。基本上,应用程序以ListView开头,其中包含Movie Titles,它们的GrossYearAndroid - 更新列表视图

然后,用户可以使用与菜单中的不同活动添加新电影,总值和年份。这些值然后返回到第一个活动并放置在列表的底部。

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

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(); 
} 
} 

如果用户增加了电影的标志Boolean变量主要是告诉。如果用户添加了电影,它将进入else语句并从那里更新。我真的很困惑在哪里把这个if-else声明。

我跑GetSearchResult函数SampleCustomAdapter的一些实验,发现它直接影响ListView的输出。我试着把if-else陈述放在那里,但是我最终在ListView中得到了很多物品。

使用adapter.notifyDataSetChanged();会给出NullPointerException错误并指向我放置它的位置。所以即使这样做:

MyMovies newMovie = new MyMovies(); 
newMovie.setMovie(data.getStringExtra("Title")); 
newMovie.setGross(data.getStringExtra("Gross")); 
newMovie.setYear(data.getStringExtra("Year")); 
movieList.add(newMovie); 
adapter.notifyDataSetChanged(); 

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

这个问题一直让我感到沮丧,因为2-3天前,帮助真的很感激。谢谢。

回答

2

你只需要通知改变了你的适配器上的数据集

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

列表视图将自动更新

UPDATE 你总是可以使用下面的它会工作,但我更喜欢通知适配器。

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

我曾尝试过。但是,我会得到一个空指针或索引超出界限的错误。 – Razgriz 2012-08-21 13:35:09

+0

检查已更新的答案 – 2012-08-21 13:47:47

+0

我明白了。我也更新了这个问题,我想我应该把它添加到我放置的第二块代码中?会发生什么是只显示一部电影。 – Razgriz 2012-08-21 14:03:46

0

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

+0

使用'adapter.notifyDataSetChanged();'给了我一个NullPointerException 。列表视图的数据取自strings.xml而不是手动输入。 – Razgriz 2012-08-21 13:43:13