Java 以JSON格式(Android)向webservice发送行值
任务:应用程序正在接收JSON格式的值,学生姓名和当前评级在列表视图中动态填充。现在,应用程序需要以JSON格式为每个学生发送评级和评论。 问题:评级和注释中给出的值可以在适配器类中检索(其中填充了一行),但不能在此类之外访问。我想在ListView.onClickListener方法中访问这些值 这是适配器类:Java 以JSON格式(Android)向webservice发送行值,java,android,json,listview,layout-inflater,Java,Android,Json,Listview,Layout Inflater,任务:应用程序正在接收JSON格式的值,学生姓名和当前评级在列表视图中动态填充。现在,应用程序需要以JSON格式为每个学生发送评级和评论。 问题:评级和注释中给出的值可以在适配器类中检索(其中填充了一行),但不能在此类之外访问。我想在ListView.onClickListener方法中访问这些值 这是适配器类: class StudentAdapter extends BaseAdapter { ArrayList<Student> studentList; Ar
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;
}
}
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;
}
}
class StudentAdapter扩展了BaseAdapter{
ArrayList学生名单;
ArrayList学生列表1;
充气机;
公共学生适配器(ArrayList学生列表){
this.studentList=studentList;
充气器=从(比率活动性)到(此)的充气器;
}
@凌驾
public int getCount(){
return studentList.size();
}
@凌驾
公共对象getItem(int位置){
返回学生列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图)
{
最终视图行=充气机。充气(R.layout.rowactivity3,父级,false);
//TextView id=(TextView)row.findViewById(R.id.tvId);
TextView name=(TextView)row.findViewById(R.id.tvName);
最终额定杆rv=(额定杆)行findViewById(R.id.RatingBar);
最终评级条rv1=(评级条)行。findViewById(R.id.ratingBar1);
最终按钮b1=(按钮)行。findViewById(R.id.btnDialog);
最终字符串[]值={null,null};
name.setText(studentList.get(position.getSname());
rv1.setRating(studentList.get(position.getRate());
b1.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v){
AlertDialog.Builder=新建AlertDialog.Builder(RatingActivity.this);
setTitle(“此处注释:”);
最终编辑文本输入=新编辑文本(RatingActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_TEXT);
builder.setView(输入);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
m_Text=input.getText().toString();
Log.d(“评论”,m_文本);
值[0]=m_文本;
b1.背景色(颜色:蓝色);
}
});
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
builder.show();
}
});
返回行;
}
}
这是单行的布局:
<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" />
试试这个:
public interface AdapterItemClickListener {
void AdapterItemClicked(int position, Bundle data);
}
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;
}
}
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;
}
}
class StudentAdapter扩展了BaseAdapter{
ArrayList学生名单;
ArrayList学生列表1;
充气机;
AdapterItemClickListener AdapterItemClickListener;
公共学生适配器(ArrayList学生列表){
this.studentList=studentList;
充气器=从(比率活动性)到(此)的充气器;
}
@凌驾
public int getCount(){
return studentList.size();
}
@凌驾
公共对象getItem(int位置){
返回学生列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图)
{
最终视图行=充气机。充气(R.layout.rowactivity3,父级,false);
//TextView id=(TextView)row.findViewById(R.id.tvId);
TextView name=(TextView)row.findViewById(R.id.tvName);
最终额定杆rv=(额定杆)行findViewById(R.id.RatingBar);
最终评级条rv1=(评级条)行。findViewById(R.id.ratingBar1);
最终按钮b1=(按钮)行。findViewById(R.id.btnDialog);
最终字符串[]值={null,null};
name.setText(studentList.get(position.getSname());
rv1.setRating(studentList.get(position.getRate());
b1.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v){
AlertDialog.Builder=新建AlertDialog.Builder(RatingActivity.this);
setTitle(“此处注释:”);
最终编辑文本输入=新编辑文本(RatingActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_TEXT);
builder.setView(输入);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
m_Text=input.getText().toString();
Log.d(“评论”,m_文本);
值[0]=m_文本;
b1.背景色(颜色:蓝色);
//在包中传递数据并传递适配器位置
adapterItemClickListener.AdapterItemClicked(位置,捆绑);
dialog.cancel();
}
});
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
builder.show();
}
});
返回行;
}
//创建适配器项单击侦听器并从片段或活动类调用
public void setAdapterItemClickListener(AdapterItemClickListener AdapterItemClickListener){
this.adapterItemClickListener=adapterItemClickListener;
}
}