我创建了一个“InterestLine”类,其中包含一个带有TextView和RatingBar的“lineContent”布局。 然后我创建了一个适配器,在主布局上创建一个InterestLines的listView,这样用户就可以看到除此之外的文本视图列表+评级栏,并对每个元素进行评级。
public class MainActivity extends ActionBarActivity implements RatingBar.OnRatingBarChangeListener{
private String gender,age,output;
private List<InterestLine> lines;
private Button doneBtn;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] interest_list = {"//the list of text views//"};
lv=(ListView)findViewById(R.id.list_view);
lines=new ArrayList<InterestLine>();
for(String il:interest_list)
lines.add(new InterestLine(il));
ArrayAdapter<InterestLine> adapter=new ArrayAdapter<InterestLine>(this, R.layout.linecontent, R.id.tv1, lines);
lv.setAdapter(adapter);
doneBtn=(Button)findViewById(R.id.button1);
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// collect stars (interests)
for(InterestLine il : lines)
output = output + il.getInterestName() + ":" + il.getNumberOfStars() + ",";
Log.i("OUTPUT", output);
}
});
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
}
我想点击按钮来收集用户在RatingBars上引入的所有评分,尝试了几种方法但没有成功。 希望你能帮助我。 感谢
主要活动的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.86" >
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/done_btn"
android:layout_gravity="right" />
</LinearLayout>
行的布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/line_text"
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:width="120sp" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stepSize="1.0"
android:layout_gravity="center_vertical" />
</LinearLayout>
答案 0 :(得分:1)
我花了一段时间来搞清楚。对于这种复杂的布局,不应使用ArrayAdapter。您应该从BaseAdapter扩展自定义适配器。 这是正常工作的自定义适配器和活动代码。
public class RatingAdapter extends BaseAdapter{
List<InterestLine> mInterestLineArrayList;
private RatingListener ratingListener;
public RatingAdapter(List<InterestLine> interestLines) {
mInterestLineArrayList = interestLines;
}
@Override public int getCount() {
return mInterestLineArrayList.size();
}
@Override public InterestLine getItem(int i) {
return mInterestLineArrayList.get(i);
}
@Override public long getItemId(int i) {
return i;
}
@Override public View getView(int i, View view, ViewGroup viewGroup) {
RatingViewHolder ratingViewHolder;
if(view==null){
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.linecontent,viewGroup,false);
ratingViewHolder = new RatingViewHolder(view);
view.setTag(ratingViewHolder);
}else{
ratingViewHolder = (RatingViewHolder) view.getTag();
}
ratingViewHolder.titleView.setText(getItem(i).getInterestName());
ratingViewHolder.ratingBar.setNumStars(getItem(i).getNumberOfStars());
ratingViewHolder.currentPosition = i;
return view;
}
public void setOnItemRatingChangeListener(RatingListener ratingListener){
this.ratingListener = ratingListener;
}
public interface RatingListener{
void onRatingBarClicked(RatingBar ratingBar, float v, boolean b,int position);
}
class RatingViewHolder implements RatingBar.OnRatingBarChangeListener{
private TextView titleView;
private RatingBar ratingBar;
private int currentPosition;
public RatingViewHolder(View view){
titleView = (TextView) view.findViewById(R.id.tv1);
ratingBar = (RatingBar) view.findViewById(R.id.ratingBar1);
ratingBar.setOnRatingBarChangeListener(this);
}
@Override public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
if(ratingListener!=null){
ratingListener.onRatingBarClicked(ratingBar,v,b,currentPosition);
}
}
}
}
public class MainActivity extends AppCompatActivity{
private String gender, age, output;
private List<InterestLine> lines;
private Button doneBtn;
private ListView lv;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] interest_list = { "Hope","it's","ok" };
lv = (ListView) findViewById(R.id.list_view);
lines = new ArrayList<>();
for (String il : interest_list) {
InterestLine interestLine = new InterestLine();
interestLine.setInterestName(il);
lines.add(interestLine);
}
RatingAdapter adapter = new RatingAdapter(lines);
lv.setAdapter(adapter);
adapter.setOnItemRatingChangeListener(new RatingAdapter.RatingListener() {
@Override public void onRatingBarClicked(RatingBar ratingBar, float v, boolean b,int position) {
lines.get(position).setNumberOfStars((int)v);
}
});
doneBtn = (Button) findViewById(R.id.button1);
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// collect stars (interests)
output = "";
for (InterestLine il : lines) {
output = il.getInterestName() + " " + il.getNumberOfStars();
Log.i("OUTPUT", output);
}
}
});
}
}
基本上,您可以创建一个自定义适配器,您可以在其中获取每个评级栏的实例。然后在该评级栏上设置OnRatingChangeListener。然后,您可以在回调的帮助下从活动中获得所需的评级值。这里棘手的部分是从适配器获取项目位置。我通过在viewholder中存储当前项位置来解决这个问题。无论如何,我还想指出,使用Recycler视图来做这件事要容易得多。