Issue
I want to know what is the best way to use a Asynctask class(LocationAsyncTask.java
) with an Activity to change UI data(ListView
).
I have this exception error:
Error:(40, 5) error: method does not override or implement a method from a supertype
EDITED:
I have this Asynctask class(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) {}
}
And this is my Activity class:
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();
}
}
And this is my layout:
<?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>
This is my Location class:
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;
}
}
With this code I can not insert data in the Listview, any suggestions?
I check these post:
Solution
There are plenty of issues with your approach and @Jyoti has just highlighted one of them. You cannot simply use ArrayAdapter as it is with complex object. It won't yield useful results. Instead you need to create CustomAdapter.
Create Custom Item view lets say
item_location.xml
under layout folder and put following code:<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>
Create CustomAdapter class as follows:
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; } }
Update your
LocationAsyncTask
as follows: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); } }
Update you
LocationNativeActivity.onCreate()
as follows:ListView listView = LocationNativeActivity.this.findViewById(R.id.lvlocationnative);
LocationAsyncTask myTask = new LocationAsyncTask(listView, this);
myTask.execute();
Answered By - Sagar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.