-2

How can I set two text blocks in the listView, of which the first is on the left, the other on the right? I am tried to create a new layout with two textViews. But I don't know how I can connect textViews with listView and how I can set texts on textViews. May anybody help me?

I would like to have a list like this

Sergey
  • 43
  • 7

2 Answers2

0

Assuming you have the list and your layout with those 2 textview is ready just use this adapter and set this adapter to recycler view of the activity. let me know if you face any issues

 public class CountryCodeAdapter extends RecyclerView.Adapter<CountryCodeAdapter.ViewHolder> {

private CountryCodeActivity activity;
ArrayList<CountryCodeModel> list = new ArrayList<>();

int selected_pos = 0;

public CountryCodeAdapter(CountryCodeActivity activity, ArrayList<CountryCodeModel> list) {
    this.activity = activity;
    this.list = list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country_listing, parent, false);
    return new ViewHolder(rootView);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

    holder.tv_row_CountryCodeActivity_countrycode.setText(list.get(holder.getAdapterPosition()).getDial());
    holder.tv_row_CountryCodeActivity_countryname.setText(list.get(holder.getAdapterPosition()).getName());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("country_code", list.get(holder.getAdapterPosition()).getDial());
            activity.setResult(activity.RESULT_OK, intent);
            activity.finish();
        }
    });
}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView tv_row_CountryCodeActivity_countryname,
            tv_row_CountryCodeActivity_countrycode;

    public ViewHolder(View itemView) {
        super(itemView);
        tv_row_CountryCodeActivity_countryname = itemView.findViewById(R.id.tv_row_CountryCodeActivity_countryname);
        tv_row_CountryCodeActivity_countrycode = itemView.findViewById(R.id.tv_row_CountryCodeActivity_countrycode);

    tv_row_CountryCodeActivity_countryname.setTypeface(AppClass.Lato_Regular);
        tv_row_CountryCodeActivity_countrycode.setTypeface(AppClass.Lato_Regular);

    }
}
}
Mohit patel
  • 296
  • 1
  • 8
0

1)You should use RecyclerView insteaad of listView, and for recyclerView you can achieve what you want with item decorator. 2)But if you have to use ListView(which is less possible case) you can do this by checking list item position and set the corresponding margin to the layout which is not recomended. 3)Also there is another way to achieve this, which is to use different layout resource xml files, but I would not use the last two variants. I would prefer the first.

Rob
  • 886
  • 9
  • 16