gpt4 book ai didi

android - 来自 ArrayAdapter 的 OnRatingBarChangeListener

转载 作者:行者123 更新时间:2023-11-30 02:00:47 25 4
gpt4 key购买 nike

我创建了一个带有“lineContent”布局的“InterestLine”类,该布局包含一个 TextView 和一个 RatingBar。然后我创建了一个适配器来在主布局上创建一个 InterestLines ListView ,这样用户就可以看到 TextView 列表 + 旁边的评级栏,并对每个元素进行评级。

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上介绍的所有评分,尝试了几种方法都没有成功。希望你能帮我解决这个问题。谢谢

主要 Activity 的布局:

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

最佳答案

我花了一段时间才弄明白。对于这种复杂的布局,你不应该使用 ArrayAdapter。您应该使自定义适配器从 BaseAdapter 扩展。这是有效的自定义适配器和 Activity 代码。

评级适配器

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);
}
}
}
}

主 Activity

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。然后,您可以在回调的帮助下从 Activity 中获得所需的评分值。这里棘手的部分是从适配器获取项目位置。我通过将当前项目位置存储在 viewholder 中解决了这个问题。不管怎样,我还想指出,对于这种事情,使用回收器 View 会容易得多。

关于android - 来自 ArrayAdapter 的 OnRatingBarChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com