Java 如何在Android中使用Asynctask类更改UI数据?

Java 如何在Android中使用Asynctask类更改UI数据?,java,android,android-activity,android-asynctask,Java,Android,Android Activity,Android Asynctask,我想知道使用Asynctask类(LocationAsyncTask.java)和活动来更改UI数据(ListView)的最佳方法是什么 我有一个异常错误: Error:(40, 5) error: method does not override or implement a method from a supertype 编辑: 我有一个Asynctask类(LocationAsyncTask.java): 使用此代码我无法在Listview中插入数据,有什么建议吗 我查看这些帖子:

我想知道使用Asynctask类(
LocationAsyncTask.java
)和活动来更改UI数据(
ListView
)的最佳方法是什么

我有一个异常错误:

Error:(40, 5) error: method does not override or implement a method from a supertype
编辑:

我有一个Asynctask类(
LocationAsyncTask.java
):

使用此代码我无法在Listview中插入数据,有什么建议吗

我查看这些帖子:


您可以替换代码:

LocationAsyncTask myTask = new LocationAsyncTask(this);
作为:


    你的方法有很多问题,@Jyoti刚刚强调了其中一个问题。不能像对复杂对象那样简单地使用ArrayAdapter。它不会产生有用的结果。相反,您需要创建CustomAdapter

  • 创建自定义项目视图,在布局文件夹下显示
    Item\u location.xml
    ,并放置以下代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent" >
        <TextView
            android:id="@+id/tvaddressLocality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Address Locality" />
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:text="Name" />
    
        <TextView
            android:id="@+id/tvAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Address" /></LinearLayout>
    
  • 按如下方式更新您的LocationNativeActivity.onCreate():

    import android.content.Context;    
    import android.support.annotation.NonNull;    
    import android.view.LayoutInflater;    
    import android.view.View;    
    import android.view.ViewGroup;    
    import android.widget.ArrayAdapter;    
    import android.widget.TextView;            
    import java.util.ArrayList;
    
    public class CustomLocationAdapter  extends ArrayAdapter<Location> {
    
        public CustomLocationAdapter(@NonNull Context context, ArrayList<Location> locations) {
            super(context,0, locations);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            Location location = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_location, parent, false);
            }
            // Lookup view for data population
            TextView tvAddressLocality = (TextView) convertView.findViewById(R.id.tvaddressLocality);
            TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
            TextView tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
            // Populate the data into the template view using the data object
            tvAddressLocality.setText(location.getAddressLocality());
            tvName.setText(location.getName());
            tvAddress.setText(location.getAddress());
            // Return the completed view to render on screen
            return convertView;
        }
    }
    
     public class LocationAsyncTask extends AsyncTask {
    
        private ArrayList<Location> locationList;
        private final WeakReference<ListView> listViewWeakReference;
    
        private Context context;
    
        public LocationAsyncTask(ListView listView, Context context) {
            this.listViewWeakReference = new WeakReference<>(listView);
            this.context = context;
        }
    
        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                //These lines are an example(I will obtain the data via internet)
                Location ejemplo = new Location("Locality1s", "name", "address");
                Location ejemplo2 = new Location("Locality2", "name2", "address2");
                locationList = new ArrayList<Location>();
                locationList.add(ejemplo);
                locationList.add(ejemplo2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            ArrayAdapter<Location> adapter = new CustomLocationAdapter(context, locationList);
            listViewWeakReference.get().setAdapter(adapter);
        }
    }
    
    ListView ListView=LocationNativeActivity.this.findViewById(R.id.lvlocationnative)

    LocationAsyncTask myTask=新建LocationAsyncTask(listView,this)

    myTask.execute()


  • 使用
    处理程序
    。。。。或者只需使用
    onPostExecute()
    。在onPreExecute和onProgress方法中的onPostExecute中,您可以更新您的ui。您可以在问题中添加Location类吗?@Sagar yes,已添加!谢谢为什么要创建一个没有添加到任何内容的新ListView?我需要在哪里编写代码?在我的activity?Thanks的onCreate()中,我删除了onPostExecute方法,并将上下文和数组列表变量更改为public,但我对activity中的@Override行有问题。我没有说要删除onPostExecute方法。只需删除方法中的行。我还更新了,现在我对onPostExecute的@Override有问题:“Method不从其超类重写Method”你能发布你的代码吗,这样我就可以检查它了谢谢!我更改了您的代码,现在正在运行。我在您的代码中删除了一个},并通过LocationNativeActivity更改了MainActivity,现在正在工作。@JavierC。没问题。谢谢你的更正!
    LocationAsyncTask myTask = new LocationAsyncTask(this);
    
    LocationAsyncTask myTask = new LocationAsyncTask(this){
                @Override
                protected void onPostExecute(Void aVoid) {
                      ListView s = (ListView)(findViewById(R.id.lvlocationnative));
                      ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(context, android.R.layout.simple_list_item_1, locationList);
                      s.setAdapter(adapter);  
                }
    };
    
    protected void onPostExecute(Void aVoid) {}
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent" >
        <TextView
            android:id="@+id/tvaddressLocality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Address Locality" />
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:text="Name" />
    
        <TextView
            android:id="@+id/tvAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Address" /></LinearLayout>
    
    import android.content.Context;    
    import android.support.annotation.NonNull;    
    import android.view.LayoutInflater;    
    import android.view.View;    
    import android.view.ViewGroup;    
    import android.widget.ArrayAdapter;    
    import android.widget.TextView;            
    import java.util.ArrayList;
    
    public class CustomLocationAdapter  extends ArrayAdapter<Location> {
    
        public CustomLocationAdapter(@NonNull Context context, ArrayList<Location> locations) {
            super(context,0, locations);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            Location location = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_location, parent, false);
            }
            // Lookup view for data population
            TextView tvAddressLocality = (TextView) convertView.findViewById(R.id.tvaddressLocality);
            TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
            TextView tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
            // Populate the data into the template view using the data object
            tvAddressLocality.setText(location.getAddressLocality());
            tvName.setText(location.getName());
            tvAddress.setText(location.getAddress());
            // Return the completed view to render on screen
            return convertView;
        }
    }
    
     public class LocationAsyncTask extends AsyncTask {
    
        private ArrayList<Location> locationList;
        private final WeakReference<ListView> listViewWeakReference;
    
        private Context context;
    
        public LocationAsyncTask(ListView listView, Context context) {
            this.listViewWeakReference = new WeakReference<>(listView);
            this.context = context;
        }
    
        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                //These lines are an example(I will obtain the data via internet)
                Location ejemplo = new Location("Locality1s", "name", "address");
                Location ejemplo2 = new Location("Locality2", "name2", "address2");
                locationList = new ArrayList<Location>();
                locationList.add(ejemplo);
                locationList.add(ejemplo2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            ArrayAdapter<Location> adapter = new CustomLocationAdapter(context, locationList);
            listViewWeakReference.get().setAdapter(adapter);
        }
    }