【发布时间】:2017-06-06 10:00:17
【问题描述】:
任务:应用程序正在接收 JSON 格式的值,学生姓名和当前评分在列表视图中动态填充。现在,应用程序需要以 JSON 格式发送单个学生的评分和 cmets。 问题:Rating 和 comment 中给出的值可以在 Adapter 类(填充单行)中检索,但不能在该类之外访问。我想在 ListView.onClickListener 方法中访问这些值。
这是适配器类:
class StudentAdapter extends BaseAdapter {
ArrayList<Student> studentList;
ArrayList<Student> studentList1;
LayoutInflater inflater;
public StudentAdapter(ArrayList<Student> studentList) {
this.studentList = studentList;
inflater = LayoutInflater.from(RatingActivity.this);
}
@Override
public int getCount() {
return studentList.size();
}
@Override
public Object getItem(int position) {
return studentList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final View row = inflater.inflate(R.layout.rowactivity3, parent, false);
// TextView id = (TextView) row.findViewById(R.id.tvId);
TextView name = (TextView) row.findViewById(R.id.tvName);
final RatingBar rv=(RatingBar)row.findViewById(R.id.ratingBar);
final RatingBar rv1=(RatingBar)row.findViewById(R.id.ratingBar1);
final Button b1=(Button)row.findViewById(R.id.btnDialog);
final String[] values = {null,null};
name.setText(studentList.get(position).getSname());
rv1.setRating(studentList.get(position).getRate());
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(RatingActivity.this);
builder.setTitle("Comment here:");
final EditText input = new EditText(RatingActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
Log.d("comment",m_Text);
values[0]=m_Text;
b1.setBackgroundColor(Color.BLUE);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
return row;
}
}
这是单行布局:
<TextView
android:layout_margin="8dp"
android:id="@+id/tvName"
android:text="enter name.."
android:padding="12dp"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
<RatingBar
android:id="@+id/ratingBar"
style="@style/customRatingBar"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:numStars="5"
android:stepSize="0.1" />
<Button
android:id="@+id/btnDialog"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="Click"/>
<RatingBar
android:id="@+id/ratingBar1"
android:layout_marginLeft="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="true"
android:stepSize="0.1" />
【问题讨论】:
-
你可以试试 ListView.setOnItemClickListener
-
我试过了,当评分栏和按钮排成一行时,它不起作用。
标签: java android json listview layout-inflater