gpt4 book ai didi

java - 如何使用 Android 中的 Asynctask 类更改 UI 数据?

转载 作者:行者123 更新时间:2023-12-02 11:26:31 25 4
gpt4 key购买 nike

我想知道将 Asynctask 类 (LocationAsyncTask.java) 与 Activity 一起使用来更改 UI 数据 (ListView) 的最佳方法是什么。

我有这个异常错误:

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

编辑:

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

public abstract class LocationAsyncTask extends AsyncTask{

public ArrayList<Location> locationList;

public Context context;

public LocationAsyncTask(Context context) {
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("Locality1","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(Void aVoid) {}

}

这是我的 Activity 类:

public class LocationNativeActivity extends Activity {
ArrayList<Location> locationList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

myTask.execute();

}

}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/lvlocationnative"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

这是我的位置类:

public class Location {

private String addressLocality;
private String name;
private String address;

public Location(String addressLocality,String name, String address) {
this.addressLocality = addressLocality;
this.name = name;
this.address = address;
}

public String getAddressLocality() {
return addressLocality;
}

public void setAddressLocality(String addressLocality) {
this.addressLocality = addressLocality;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return this.addressLocality;
}

}

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

我检查这些帖子:

最佳答案

您的方法存在很多问题,@Jyoti 刚刚强调了其中之一。您不能简单地使用 ArrayAdapter,因为它与复杂对象一起使用。它不会产生有用的结果。相反,您需要创建 CustomAdapter。

  1. 创建自定义项目 View ,假设布局文件夹下有 item_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>
  2. 创建 CustomAdapter 类如下:

    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;
    }
    }
  3. 按如下方式更新您的 LocationAsyncTask:

     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);
    }
    }
  4. 更新您的 LocationNativeActivity.onCreate(),如下所示:

    ListView listView = LocationNativeActivity.this.findViewById(R.id.lvlocationnative);

    LocationAsyncTask myTask = new LocationAsyncTask(listView, this);

    myTask.execute();

关于java - 如何使用 Android 中的 Asynctask 类更改 UI 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49586780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com