2014-10-01 109 views
1

首先,我第一次在三週前關注了Java,所以如果這段代碼很糟糕,請耐心等待。這是學校的一項任務,我要在原型應用程序上構建併爲其提供UI,因此適配器基本上就是我爲此所做的所有工作。Android - 無法向上滾動

我的問題是,只要我觸摸滾動,我就會被拋到列表的底部,並且無法向下滾動而無法回滾。

/** 
* VaxjoWeather.java 
* Created: May 9, 2010 
* Jonas Lundberg, LnU 
*/ 

package dv106.weather; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.StringWriter; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.ListActivity; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 

/** 
* This is a first prototype for a weather app. It is currently 
* only downloading weather data for Växjo. 
* 
* This activity downloads weather data and constructs a WeatherReport, 
* a data structure containing weather data for a number of periods ahead. 
* 
* The WeatherHandler is a SAX parser for the weather reports 
* (forecast.xml) produced by www.yr.no. The handler constructs 
* a WeatherReport containing meta data for a given location 
* (e.g. city, country, last updated, next update) and a sequence 
* of WeatherForecasts. 
* Each WeatherForecast represents a forecast (weather, rain, wind, etc) 
* for a given time period. 
* 
* The next task is to construct a list based GUI where each row 
* displays the weather data for a single period. 
* 
* 
* @author jlnmsi 
* 
*/ 

public class VaxjoWeather extends ListActivity { 
    //private InputStream input; 
    private WeatherReport report = null; 

    //private ArrayList<WeatherForecast> forecastList = new ArrayList<WeatherForecast>(); 

    private WeatherAdapter adapter; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     adapter = new WeatherAdapter(this); 
     setListAdapter(adapter); 

     //getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED); 

     try { 
      URL url = new URL("http://www.yr.no/sted/Sverige/Kronoberg/V%E4xj%F6/forecast.xml"); 
      AsyncTask task = new WeatherRetriever().execute(url); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 

     //adapter.notifyDataSetChanged(); 
    } 

    private void PrintReportToConsole() { 
     if (this.report != null) { 
      /* Print location meta data */ 
      //System.out.println(report); 

      /* Print forecasts */ 
      int count = 0; 
      for (WeatherForecast forecast : report) { 
       count++;     
       adapter.add(forecast); 
      } 
     } 
     else { 
      System.out.println("Weather report has not been loaded."); 
     } 
     //adapter.notifyDataSetChanged(); 
    } 

    private class WeatherRetriever extends AsyncTask<URL, Void, WeatherReport> { 
     protected WeatherReport doInBackground(URL... urls) { 
      try { 
       return WeatherHandler.getWeatherReport(urls[0]); 
      } catch (Exception e) { 
       throw new RuntimeException(e); 
      } 
     } 

     protected void onProgressUpdate(Void... progress) { 

     } 

     protected void onPostExecute(WeatherReport result) { 
      report = result; 
      PrintReportToConsole(); 
     } 
    } 

    // custom ArrayAdpater to show, weather icon, temperature, and precipation. 
    class WeatherAdapter extends ArrayAdapter<WeatherForecast> { 

     public WeatherAdapter(Context context) { 
      super(context,R.layout.forecast); 
     } 

     @Override // Called when updating the ListView 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row; 
      if (convertView == null) { // Create new row view object   
       LayoutInflater inflater = getLayoutInflater(); 
       row = inflater.inflate(R.layout.forecast,parent,false); 
      } 
      else // reuse old row view to save time/battery 
       row = convertView; 

      // TextView for Temperature 
      TextView temperature = (TextView)row.findViewById(R.id.temperature); 
      temperature.setText(Integer.toString(this.getItem(position).getTemp())+" °C"); 

      // TextView for out Precipation. 
      TextView precipation = (TextView)row.findViewById(R.id.rain); 
      precipation.setText(String.valueOf(this.getItem(position).getRain())+" mm"); 

      // Image Icon for forecast. 
      ImageView icon = (ImageView)row.findViewById(R.id.icon); 
      String iconPath = "ic_";    

      if (this.getItem(position).getWeatherCode() <= 9){ 
       iconPath = iconPath+"0"+(Integer.toString(this.getItem(position).getWeatherCode())); 
      } 
      else { 
       iconPath = iconPath+(Integer.toString(this.getItem(position).getWeatherCode())); 
      } 

      int resId = getResources().getIdentifier(iconPath, "drawable", getPackageName()); 

      // If the resource ID is invalid, as in the image not existing, we'll add the postfix for periods. 
      if (resId == 0){ 

       // Set the icon image source dependent on period code given. 
       if(this.getItem(position).getPeriodCode() == 3){ 
        iconPath = iconPath +"n"; 
       } 

       else if (this.getItem(position).getPeriodCode() == 2){ 

        iconPath = iconPath +"d"; 
       } 
       else { 
        iconPath = iconPath +"m"; 
       } 

       resId = getResources().getIdentifier(iconPath, "drawable", getPackageName()); 
       icon.setImageResource(resId); 
      } 
      // Or if everything checked out, we'll just run with the resource ID and find our Icon. 
      else { 
       icon.setImageResource(resId); 
      } 

      return row; 
     } 
    } 

} 

我試圖將另一個標準arrayadapter,實際上得到了相同的不必要的滾動效果,所以我不知道它是什麼的一部分我有問題與。

回答

0

解決方案被發現,顯然,它沒有什麼與我的代碼。 Class建議我們使用Intel x86而不是ARM來模擬我們的模擬器。運行ARM滾動工作就像預期一樣。

0

這樣做的方法是: 1:將ListView放置在main.xml中 2:在你的Activity中讓ListView的對象像這樣 - > private ListView listView;在onCreate連接到這樣的main.xml,listView =(ListView)R.id.listView1; //無論它叫什麼

3:next創建一個ArrayList: - > private ArrayList arrayWeather = new ArrayList();

4:用天氣數據填充數組列表,最後創建類的對象,並使其使用您的數組列表顯示數據。

例子: 公共類LISTUSER延伸BaseAdapter {

@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return arraylistData.size(); // the arraylist u created 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return arraylistData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return arraylistData.size(); 
    } 

    @Override 
    public View getView(final int position, View v, ViewGroup parent) { 
     if(v == null){ 
      LayoutInflater lf = (LayoutInflater) BuyPets.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = lf.inflate(R.layout.forecast, null); 
     } 


     // setup here, done 


     return v; 
    } 

}