如何在RecycleView中显示循环数组的理想结果

时间:2018-07-30 23:30:07

标签: java android android-recyclerview

今天我得到两个数组的结果,这是不可取的结果。 例如,我有这样的数据:

<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>

,当我尝试使用循环数组获取数据时,结果如下所示: undesirable result

我希望我可以获取数据并像这样显示它们: Desirable result

您可以解决此问题吗?

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 个答案:

答案 0 :(得分:0)

您在'DoaPagi.java'中的getData方法不正确。您的“ for()”循环  嵌套。应这样声明:

for (int i = 0; i < data.length; i++) {
    list.add(new ModelDoa(data[i], baca[i], ModelDoa.DOA_PAGI));
}