Farklı bir sınıfta AsyncTask oluşturma ve Java.lang.ClassCastException
arabirimini kullanarak değer döndürme girişiminde bulunamaz. Eğer aktiviteyi kullanarak aynı şeyi yapsaydım, her şey iyi çalışıyor ama Bağdaştırıcıyı kullanamamıştım. Bunun arkasındaki sebebi bilmiyorum. Birisi lütfen önerebilir.Ayrı bir sınıfta AsyncTask oluşturun ve arabirim kullanarak ondan değer döndürmeye çalışın. Java.lang.ClassCastException,
/* Separate Java class extends AsyncTask*/
public class Task extends AsyncTask<String,String,String>
{
/* Declaraing required variables */
Context context;
TaskCompleted taskCompleted;
public Task(Context context){
this.context = context;
/* The problem is right in this line */
/* Java.lang.ClassCastException cannot be cast.*/
this.taskCompleted = (TaskCompleted) context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
//Return string value
return "hello";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//add string value
taskCompleted.output(s);
}
}
/*Interface class to store asynctask value */
public interface TaskCompleted {
/* Method to get value set from asynctask */
public void output(String value);
}
/*If i did the same thing using activity
everything works fine but using Adapter i m not been able
to make it work i dont know the reason behind it. Can
someone please suggest.*/
/* Adapter Class implements Interface Class */
public class Adapter extends BaseAdapter implements
TaskCompleted {
/* Declaring the variable */
private Context context;
private List<String> list;
private LayoutInflater inflater;
/* Create Constructor to send required value */
public Adapter(Context context, List<String> list) {
/* Setting up value */
this.context = context;
this.list = list;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
/* Size of ArrayList */
return list.size();
}
@Override
public String getItem(int position) {
/* return value of particular position */
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
/*Get View from here for just to test*/
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
/* Call class to get views */
MyViewHolder mViewHolder;
//get custom layout
if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_layout,
parent, false);
//initialize view holder class
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.nameTextView.setText(list.get(position));
//add listener
mViewHolder.nameTextView.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
//Call AsyncTask class to execute
new Task(context).execute();
}
});
return convertView;
}
@Override
public void output(String value) {
/*Printing value*/
Toast.makeText(context,value+" From
Interface",Toast.LENGTH_LONG).show();
}
private class MyViewHolder {
TextView nameTextView;
public MyViewHolder(View item) {
/* Get view from layout */
nameTextView = (TextView)
item.findViewById(R.id.nameId);
}
}
}
Lütfen kod yazınız. – crashOveride
Evet kod yazmıştım .. – user3797004
Bağlamı etkinlikten bağdaştırıcıya ve bağdaştırıcıdan asynctask'a göndermeyi deneyin. –