gpt4 book ai didi

java - 以 JSON 格式将行值发送到 Web 服务 (Android)

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

Rating Activity

任务:应用程序正在接收 JSON 格式的值,并且学生姓名和当前评分在 ListView 中动态填充。现在,应用程序需要以 JSON 格式发送个别学生的评分和评论。问题: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" />

最佳答案

试试这个:

  1. 创建界面

    public interface AdapterItemClickListener {
    void AdapterItemClicked(int position, Bundle data);
    }
  2. 然后在您的适配器类中添加代码:

    class StudentAdapter extends BaseAdapter {

    ArrayList<Student> studentList;
    ArrayList<Student> studentList1;
    LayoutInflater inflater;
    AdapterItemClickListener adapterItemClickListener;

    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);
    //pass your data in bundle and pass adapter position
    adapterItemClickListener.AdapterItemClicked(position,bundle);
    dialog.cancel();
    }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    dialog.cancel();
    }
    });
    builder.show();
    }
    });
    return row;
    }

    // create adapter item click listener and call from fragment or activity class
    public void setAdapterItemClickListener(AdapterItemClickListener adapterItemClickListener){
    this.adapterItemClickListener=adapterItemClickListener;
    }

    }
  3. 然后从 fragment 或 Activity 调用 studentAdapter.setAdapterItemClickListener(this) 并覆盖 void AdapterItemClicked(int position, Bundle data); .您可以通过此方法获取 bundle 中的数据。

注意:此代码未经测试。因此,如果您遇到任何问题,请调试并检查。我希望这会奏效。

关于java - 以 JSON 格式将行值发送到 Web 服务 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44386966/

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