dondo
dondo

Reputation: 99

Android - looping listView item using button for setText the TextView

I've tried to find any tutorial from go*gle, but I'm still not yet find out what I need.

I have create a layout like this :

|------------------|
| 1.TextView       |
| * * * * *        |
|------------------|
| 2.TextView       |
| * * * * *        |
|------------------|
| 3.TextView       |
| * * * * *        |
|------------------|
|                  |
| Btn | TextView   |

but I want when the button pressed, it's collecting any item in each list, and setText to TextView like this :

|-------------------------|
| 1.TextView              |
| * * * * *               |
|-------------------------|
| 2.TextView              |
| * * *                   |
|-------------------------|
| 3.TextView              |
| * * * *                 |
|-------------------------|
|                         |
| Btn |"No : 1 - Star = 5"|
|      "No : 2 - Star = 3"|
|      "No : 3 - Star = 4"|
|-------------------------|

i just can setText one list with this code :

kirim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float tanya = adapter2.getItem(3).getRatingStar(); //getItem("list id")
            TextView Star = (TextView) findViewById(R.id.star);
            if (Star != null) {
                Star.setText("Star : "+tanya);
            }
        }
    });

I've tried to looping, but I can't figure out what should I do to get the id and value each list of ListView.

I've stuck at this code and not yet fix the error

kirim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float bintang = adapter2.getItem(3).getRatingStar();
            TextView Star = (TextView) findViewById(R.id.star);
            for(int i = 0; i<=adapter2.getCount(); i++)
                Star.setText("id 1" + i + "\nStar : "+bintang);
        }
    });

any idea or reference to let me read the flow of code please?

EDIT

PertanyaanAdapter.java

class PertanyaanAdapter extends ArrayAdapter<Pertanyaan> {

    private AppCompatActivity activity;
    private List<Pertanyaan> movieList;

    PertanyaanAdapter(AppCompatActivity context, int resource, List<Pertanyaan> objects) {
        super(context, resource, objects);
        this.activity = context;
        this.movieList = objects;
    }

    @Override
    public Pertanyaan getItem(int position) {
        return movieList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
            //holder.ratingBar.getTag(position);
        }

        holder.ratingBar.setOnRatingBarChangeListener(onRatingChangedListener(position));

        holder.ratingBar.setTag(position);
        holder.ratingBar.setRating(getItem(position).getRatingStar());
        holder.movieName.setText(getItem(position).getAsk());

        return convertView;
    }

    private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final int position) {
        return new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                Pertanyaan item = getItem(position);
                assert item != null;
                item.setRatingStar(v);
                Log.i("Adapter", "star: " + v);
            }
        };
    }

    private static class ViewHolder {
        private RatingBar ratingBar;
        private TextView movieName;

        ViewHolder(View view) {
            ratingBar = (RatingBar) view.findViewById(R.id.rate_img);
            movieName = (TextView) view.findViewById(R.id.text);
        }
    }
}

Pertanyaan.java

package flix.yudi.pertanyaan3;
public class Pertanyaan {

    private float ratingStar;
    private String ask;

    Pertanyaan(int ratingStar, String ask) {
        this.ratingStar = ratingStar;
        this.ask = ask;
    }

    float getRatingStar() {
        return ratingStar;
    }

    void setRatingStar(float ratingStar) {
        this.ratingStar = ratingStar;
    }

    public String getAsk() {
        return ask;
    }

}

Upvotes: 0

Views: 747

Answers (3)

Vinodh
Vinodh

Reputation: 1129

The problem was setText was happening inside loop. So, using a StringBuilder preparing the text in loop using append() and after loop exit :

    kirim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            TextView Star = (TextView) findViewById(R.id.star);
            StringBuilder text = new StringBuilder();
            for(int i = 0; i<=adapter2.getCount(); i++) {
                float bintang = adapter2.getItem(i).getRatingStar();
                text.append("id 1" + i + "\nStar : " + bintang);
            }
            Star.setText(text);
        }
    });

Now, to send data to MySQL ? if you mean database, you need to create a column named "Rating" and insert the row.

Upvotes: 1

pankaj khedekar
pankaj khedekar

Reputation: 126

you will need one linear layout with vertical orientation and can addtextView dynamically in that linear layout and show the listView data on the newly created textView by using for loop. It will give you desired output as you want

Upvotes: 0

Nigam Patro
Nigam Patro

Reputation: 2770

Inside onClick()

int size = adapter2.getCount();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < size; i++) {
    builder.append("No : " + (i + 1) + " - " + "Star = " + adapter2.getItem(i).getRatingStar() + "\n");
}
TextView Star = (TextView) findViewById(R.id.star);
star.setText(builder.toString());

Upvotes: 0

Related Questions