CardView içerisindeki RadioButton otomatik olarak seçiliyor
27.07.2018 - 07:02
Geliştirmekte olduğum test çözme özelliğinde şöyle garip bir hata ile karşı karşıyayım. WebService'den Json formatında verileri çekiyorum ve çektiğim array i recyclerview ile listeliyorum. Soru ve 4 adet şıktan oluşan her item'ı cardviewler içerisinde gösteriyorum. Şıkları radiobutton olarak göstermeyi tercih ettim. 20 tane soruyu cardviewler içerisinde listeledim ve birinci sorunun A şıkkını seçtiğim zaman aynı anda 7. 13. ve 19.sorularında A şıkkı kendiliğinden seçiliyor. 6 soruda bir tekrar eden bu hatayı kodlarda 6 ile ilgili bir şeyler arayarak bulmaya çalıştım fakat 6 ile alakalı hiç bir kodum yok. Test sorularının listelendiği activity ve CardAdapter ı şöyle :
TestActivity.class
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork;
import com.ogrenciasistani.lgsasistan.R;
import com.ogrenciasistani.lgsasistan.adapters.CardAdapter;
import com.ogrenciasistani.lgsasistan.configs.Config;
import com.ogrenciasistani.lgsasistan.models.SuperHero;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
public class TestActivity extends AppCompatActivity {
//Creating a List of superheroes
private List<SuperHero> listSuperHeroes;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
//Volley Request Queue
private RequestQueue requestQueue;
//The request counter to send ?page=1, ?page=2 requests
private int requestCount;
private String CONFIG_URL;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_fragment);
String testNo = getIntent().getExtras().getString("testno").toString();
setTitle(testNo);
String whichSubject = getIntent().getExtras().getString("whichSubject").toString();
if (whichSubject.equals("Turkce")) {
CONFIG_URL = Config.DATA_URL;
} else if (whichSubject.equals("Math")) {
CONFIG_URL = Config.DATA_URL_MATH;
} else if (whichSubject.equals("Science")) {
CONFIG_URL = Config.DATA_URL_SCIENCE;
} else if (whichSubject.equals("History")) {
CONFIG_URL = Config.DATA_URL_HISTORY;
} else if (whichSubject.equals("English")) {
CONFIG_URL = Config.DATA_URL_ENGLISH;
} else {
Toast.makeText(this, "whichSubject is null", Toast.LENGTH_SHORT).show();
}
requestCount = getIntent().getExtras().getInt("testnoint");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//Initializing our superheroes list
listSuperHeroes = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getApplicationContext());
//Calling method to get data to fetch data
getData();
/*
//Adding an scroll change listener to recyclerview
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
recyclerView.setOnScrollChangeListener(this);
}
*/
//initializing our adapter
adapter = new CardAdapter(listSuperHeroes, getApplicationContext());
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//Displaying Progressbar
progressBar.setVisibility(View.VISIBLE);
setProgressBarIndeterminateVisibility(true);
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(CONFIG_URL + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
//Hiding the progressbar
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(getApplicationContext(), "Daha fazla soru yok.", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
//This method will get data from the web api
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
//requestCount++; // kaldırmayı dene ne olacak gör ?
}
//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the superhero object
SuperHero superHero = new SuperHero();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
superHero.setSorutitle(json.getString(Config.TAG_SORU_TITLE));
superHero.setSoruoption1(json.getString(Config.TAG_SORU_OPTION_1));
superHero.setSoruoption2(json.getString(Config.TAG_SORU_OPTION_2));
superHero.setSoruoption3(json.getString(Config.TAG_SORU_OPTION_3));
superHero.setSoruoption4(json.getString(Config.TAG_SORU_OPTION_4));
superHero.setSorucevap(json.getString(Config.TAG_SORU_CEVAP));
} catch (JSONException e) {
e.printStackTrace();
}
//Adding the superhero object to the list
listSuperHeroes.add(superHero);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}
/*
//Overriden method to detect scrolling
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//Ifscrolled at last then
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
}
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
} else if (id == R.id.end){
} else if (id == R.id.answerkey){
}
return super.onOptionsItemSelected(item);
}
}
CardAdapter.class
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.ogrenciasistani.lgsasistan.R;
import com.ogrenciasistani.lgsasistan.helper.CustomVolleyRequest;
import com.ogrenciasistani.lgsasistan.models.SuperHero;
import java.util.List;
/**
* Created by cumakesici on 01/06/2017.
*/
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
SuperHero superHero;
private Context context;
//List to store all superheroes
List<SuperHero> superHeroes;
//Constructor of this class
public CardAdapter(List<SuperHero> superHeroes, Context context) {
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.superheroes_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
//Getting the particular item from the list
superHero = superHeroes.get(position);
/*
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImage(), ImageLoader.getImageListener(holder.imageView, android.R.drawable.ic_dialog_alert, android.R.drawable.ic_dialog_alert));
*/
//Showing data on the views
//holder.imageView.setImageUrl(superHero.getImage(), imageLoader);
holder.soruTitle.setText(superHero.getSorutitle());
holder.soruOption1.setText(superHero.getSoruoption1());
holder.soruOption2.setText(superHero.getSoruoption2());
holder.soruOption3.setText(superHero.getSoruoption3());
holder.soruOption4.setText(superHero.getSoruoption4());
String soruCevap = superHero.getSorucevap().toString();
holder.soruOption1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(context, position+1 +".sorunun A şıkkına tıklandı.", Toast.LENGTH_SHORT).show();
}
});
holder.soruOption2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(context, position+1 +".sorunun B şıkkına tıklandı.", Toast.LENGTH_SHORT).show();
}
});
holder.soruOption3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(context, position+1 +".sorunun C şıkkına tıklandı.", Toast.LENGTH_SHORT).show();
}
});
holder.soruOption4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(context, position+1 +".sorunun D şıkkına tıklandı.", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView soruTitle;
public RadioButton soruOption1;
public RadioButton soruOption2;
public RadioButton soruOption3;
public RadioButton soruOption4;
public TextView soruCevap;
//Initializing Views
public ViewHolder(final View itemView) {
super(itemView);
soruTitle = (TextView) itemView.findViewById(R.id.soruTitle);
soruOption1 = (RadioButton) itemView.findViewById(R.id.soruOption1);
soruOption2 = (RadioButton) itemView.findViewById(R.id.soruOption2);
soruOption3 = (RadioButton) itemView.findViewById(R.id.soruOption3);
soruOption4 = (RadioButton) itemView.findViewById(R.id.soruOption4);
}
/*
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "position = " + getPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra("title",superHero.getTitle());
intent.putExtra("detail",superHero.getDetail());
intent.putExtra("image",superHero.getImage());
intent.putExtra("author_name",superHero.getAuthor_name());
intent.putExtra("author_avatar",superHero.getAuthor_avatar());
intent.putExtra("date",superHero.getDate());
intent.putExtra("share_link", superHero.getShareLink());
context.startActivity(intent);
}
*/
}
}
SuperHero.class
public class SuperHero {
private String sorutitle;
private String soruoption1;
private String soruoption2;
private String soruoption3;
private String soruoption4;
private String sorucevap;
public String getSorutitle() {
return sorutitle;
}
public void setSorutitle(String sorutitle) {
this.sorutitle = sorutitle;
}
public String getSoruoption1() {
return soruoption1;
}
public void setSoruoption1(String soruoption1) {
this.soruoption1 = soruoption1;
}
public String getSoruoption2() {
return soruoption2;
}
public void setSoruoption2(String soruoption2) {
this.soruoption2 = soruoption2;
}
public String getSoruoption3() {
return soruoption3;
}
public void setSoruoption3(String soruoption3) {
this.soruoption3 = soruoption3;
}
public String getSoruoption4() {
return soruoption4;
}
public void setSoruoption4(String soruoption4) {
this.soruoption4 = soruoption4;
}
public String getSorucevap() {
return sorucevap;
}
public void setSorucevap(String sorucevap) {
this.sorucevap = sorucevap;
}
}
13
Görüntülenme
0 Beğeni