我试图从go * gle找到任何教程,但我还没有找到我需要的东西。
我创建了这样的布局:
|------------------|
| 1.TextView |
| * * * * * |
|------------------|
| 2.TextView |
| * * * * * |
|------------------|
| 3.TextView |
| * * * * * |
|------------------|
| |
| Btn | TextView |
但我希望当button
被按下时,它会收集每个列表中的任何项目,并将setText设置为TextView
,如下所示:
|-------------------------|
| 1.TextView |
| * * * * * |
|-------------------------|
| 2.TextView |
| * * * |
|-------------------------|
| 3.TextView |
| * * * * |
|-------------------------|
| |
| Btn |"No : 1 - Star = 5"|
| "No : 2 - Star = 3"|
| "No : 3 - Star = 4"|
|-------------------------|
我只需使用以下代码setText一个列表:
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);
}
}
});
我尝试过循环,但我无法弄清楚我应该怎样做才能获得每个ListView列表的id和值。
我已经坚持使用此代码而尚未解决错误
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);
}
});
是否有任何想法或参考让我阅读代码流程?
修改
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;
}
}
答案 0 :(得分:1)
问题是setText发生在循环内部。因此,使用StringBuilder使用append()和循环退出后在循环中准备文本:
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);
}
});
现在,要将数据发送到MySQL?如果你的意思是数据库,你需要创建一个名为&#34; Rating&#34;并插入行。
答案 1 :(得分:0)
内部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());
答案 2 :(得分:0)
您需要一个linear layout
垂直方向,并且可以在该线性布局中动态添加textView
,并使用{{在新创建的listView
上显示textView
数据1}}。
它会根据需要为您提供所需的输出