How to Display a Desirable Result of Looping Array in RecycleView
How to Display a Desirable Result of Looping Array in RecycleView
today I get result of two array, which is this result is undesirable.
For example, I have a data like this:
<string-array name="player">
<item>CR7</item>
<item>Sergio Ramos</item>
<item>Ozil</item>
</string-array>
<string-array name="posisi">
<item>Stricker</item>
<item>Defender</item>
<item>Winger</item>
</string-array>
and when I try to fetch data using looping array, the result is like this:
undesirable result
I wish, I can fetch the data, and display them like this:
Desirable result
Can you fix this problem?
ModelDoa.java
public class ModelDoa {
public static final int DOA_PAGI = 0;
public static final int DOA_SORE = 1;
private String mName;
private String bName;
private int mType;
public ModelDoa(String name, String butong, int type) {
this.mName = name;
this.bName = butong;
this.mType = type;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public int getType() {
return mType;
}
public void setType(int type) { this.mType = type; }
public String ambilName() {
return bName;
}
public void setNama(String butonk) {
this.bName = butonk;
}
}
DoaPagi.java
public class DoaPagi extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doa_pagi);
// toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//this line shows back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
List<ModelDoa> rowListItem = getData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DoaPagi.this);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(linearLayoutManager);
AdapterDoa rcAdapter = new AdapterDoa(rowListItem);
mRecyclerView.setAdapter(rcAdapter);
}
private List<ModelDoa> getData() {
String data = getResources().getStringArray(R.array.player);
String baca = getResources().getStringArray(R.array.posisi);
List<ModelDoa> list = new ArrayList<ModelDoa>();
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < baca.length; j++) {
list.add(new ModelDoa(data[i], baca[j], ModelDoa.DOA_PAGI));
}
}
return list;
}
// Agar back button pada halaman induk settings berfungsi
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
AdapterDoa.java
public class AdapterDoa extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ModelDoa> mList;
public AdapterDoa(List<ModelDoa> list) {
this.mList = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case DOA_PAGI:
View vieu = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
PagiViewHolder rcv = new PagiViewHolder(vieu);
return rcv;
case DOA_SORE:
View doa = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
SoreViewHolder mdoa = new SoreViewHolder(doa);
return mdoa;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ModelDoa object = mList.get(position);
if (object != null) {
switch (object.getType()) {
case DOA_PAGI:
((PagiViewHolder) holder).mTitle.setText(object.getName());
break;
case DOA_SORE:
((SoreViewHolder) holder).mTitle.setText(object.getName());
break;
}
}
}
@Override
public int getItemCount() {
if (mList == null)
return 0;
return mList.size();
}
@Override
public int getItemViewType(int position) {
if (mList != null) {
ModelDoa object = mList.get(position);
if (object != null) {
return object.getType();
}
}
return 0;
}
}
PagiViewHolder.java
public class PagiViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public int posisi = 0;
public int posisi1 = 1;
public Button tombolbaca;
public Button tombolshare;
public PagiViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(mainViewClickListener);
mTitle = (TextView) itemView.findViewById(R.id.titleTextView);
tombolbaca = (Button) itemView.findViewById(R.id.buttonbaca);
tombolshare = (Button) itemView.findViewById(R.id.buttonshare);
tombolbaca.setOnClickListener(bacaClickListener);
tombolshare.setOnClickListener(shareClickListener);
}
private View.OnClickListener bacaClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do button click handling here
}
};
private View.OnClickListener shareClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do button click handling here
if ( posisi == getAdapterPosition() ) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, mTitle.getText().toString() + mTitle.getText().toString() );
sendIntent.setType("text/plain");
Intent.createChooser(sendIntent,"Share via");
v.getContext().startActivity(sendIntent);
}
}
};
private View.OnClickListener mainViewClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do button click handling here
Toast.makeText(v.getContext(), "ssss = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
};
}
1 Answer
1
Your getData method in 'DoaPagi.java' is incorect. Your 'for()' loops
are nested.They should be declared like this :
for (int i = 0; i < data.length; i++) {
list.add(new ModelDoa(data[i], baca[i], ModelDoa.DOA_PAGI));
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code is work,, It's awesome,, very awesome.. big thanks!
– Sa'ad Abdurrazzaq
2 hours ago