首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android -不能向上滚动

Android -不能向上滚动
EN

Stack Overflow用户
提问于 2014-10-01 22:04:55
回答 2查看 260关注 0票数 1

首先,我三周前第一次关注Java,所以如果这段代码很糟糕的话,请原谅我。这是一个学校的任务,我要建立一个原型应用程序,并给它一个UI,所以适配器基本上是我所做的一切。

我的问题是,一旦我触摸卷轴,我就会被抛到列表的底部,如果不被向下推,我就不能向上滚动。

代码语言:javascript
运行
复制
/**
 * 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;
        }
    }

}

我试着应用另一个标准的数组适配器,实际上得到了相同的不想要的滚动结果,所以我不知道它是什么部分,我有问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-02 11:27:16

找到了解决方案,显然这与我的代码无关。类建议我们使用英特尔x86而不是ARM作为模拟器。运行它与手臂滚动工作,正如预期。

票数 0
EN

Stack Overflow用户

发布于 2014-10-01 22:57:47

方法是: 1:在ListView中放置main.xml 2:在您的活动中使ListView的对象像so ->专用的listView listView;在onCreate中,像这样将它连接到main.xml,ListView = (ListView) R.id.listView1;//不管它被称为

3:接下来创建一个ArrayList:->私有ArrayList arrayWeather =新ArrayList();

4:用天气数据填充数组,然后最后创建您创建的类的对象,并使其使用数组列表显示数据。

示例:公共类ListUser扩展BaseAdapter{

代码语言:javascript
运行
复制
    @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;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26151463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档