我想知道将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中插入数据,有什么建议吗?
我查看这些帖子:
答案 0 :(得分:1)
您的方法存在很多问题,而@Jyoti刚刚强调了其中一个问题。您不能简单地使用ArrayAdapter,因为它与复杂对象一样。它不会产生有用的结果。相反,您需要创建CustomAdapter。
创建自定义项视图可以在布局文件夹下说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>
按如下方式创建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;
}
}
按如下方式更新您的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);
}
}
按以下方式更新LocationNativeActivity.onCreate()
:
ListView listView = LocationNativeActivity.this.findViewById(R.id.lvlocationnative);
LocationAsyncTask myTask = new LocationAsyncTask(listView,this);
myTask.execute();
答案 1 :(得分:0)
您可以替换您的代码:
def data_gen():
while True:
x = (np.random.random([1024])-0.5) * 10
y = np.sin(x)
yield (x,y)
regressor = Sequential()
regressor.add(Dense(units=20, activation='tanh', input_dim=1))
regressor.add(Dense(units=20, activation='tanh'))
regressor.add(Dense(units=20, activation='tanh'))
regressor.add(Dense(units=1, activation='linear'))
regressor.compile(loss='mse', optimizer='adam')
regressor.fit_generator(data_gen(), epochs=3, steps_per_epoch=128)
x = (np.random.random([1024])-0.5)*10
x = np.sort(x)
y = np.sin(x)
plt.plot(x, y)
plt.plot(x, regressor.predict(x))
plt.show()
as:
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);
}
};
方法:
onPostExecute()