I'm having this error:
Caused by: java.lang.UnsupportedOperationException: addView (View) is not supported in AdapterView
I researched and the errors that were cited did not solve my problem.
I'm not understanding this problem.
Code of my Adapter:
public class ListaQuizAdapter extends ArrayAdapter<String> {
private ArrayList<Quiz> listaQuiz;
private MainActivity mainActivity;
private Context context;
public ListaQuizAdapter(Context context, ArrayList<Quiz> listaQuiz,MainActivity mainActivity) {
super(context, R.layout.layout_listview_quiz);
this.listaQuiz = listaQuiz;
this.mainActivity = mainActivity;
this.context = context;
}
@Override
public int getCount() {
return listaQuiz.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
try {
if (convertView == null) {
v = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_listview_quiz, parent,false);
v.nome = (TextView) convertView.findViewById(R.id.nomeQuiz);
v.numero = (TextView) convertView.findViewById(R.id.numeroQuestoes);
v.nome.setText(listaQuiz.get(position).getNome());
v.numero.setText(listaQuiz.get(position).getNumeroQuestoes());
v.iniciar = (Button) convertView.findViewById(R.id.iniciar);
v.iniciar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainActivity.getSupportFragmentManager().beginTransaction().replace(R.id.container, new ResponderQuizFragment()).addToBackStack(null).commit();
}
});
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
}catch (Exception e){
e.printStackTrace();
//Log.e("Exception", e.toString());
}
return convertView;
}
public class ViewHolder {
TextView nome, numero;
Button iniciar;
}
}
MainActivity Code
public class MainActivity extends ActionBarActivity {
public Toolbar actionToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionToolbar = (Toolbar) findViewById(R.id.toolbar);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, new QuizFragment()).commit();
}
}
}
Fragment Quiz:
public class QuizFragment extends android.support.v4.app.Fragment {
@Bind(R.id.lista_quiz)
ListView listaQuiz;
private MainActivity mainActivity;
public QuizFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_quiz, container, false);
ButterKnife.bind(this, view);
mainActivity = (MainActivity) getActivity();
mainActivity.actionToolbar.setTitle("Quiz");
Quiz q = new Quiz();
q.setNome("Rebimboca");
q.setNumeroQuestoes("18Questoes");
ArrayList<Quiz> quiz = new ArrayList<>();
quiz.add(q);
ListaQuizAdapter listaQuizAdapter = new ListaQuizAdapter(getActivity(), quiz, mainActivity);
listaQuiz.setAdapter(listaQuizAdapter);
listaQuiz.setFocusable(false);
return view;
}
}
ListView XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="br.net.getinfo.treinamentos.fragment.QuizFragment">
<ListView
android:id="@+id/lista_quiz"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Any ideas on this error?