【问题标题】:Sending value of rows to webservice in JSON format (Android)以 JSON 格式将行的值发送到 Web 服务(Android)
【发布时间】:2017-06-06 10:00:17
【问题描述】:

Rating Activity

任务:应用程序正在接收 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


【解决方案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. 然后从片段或活动中调用 studentAdapter.setAdapterItemClickListener(this) 并覆盖 void AdapterItemClicked(int position, Bundle data); .您可以在此方法中以捆绑方式获取数据。

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

【讨论】:

  • 请告诉如何在activity中实现studentAdapter.setAdapterItemClickListener(this)。
  • 从您设置StudentAdapter 的活动中调用studentAdapter.setAdapterItemClickListener(this)。然后studentAdapter.setAdapterItemClickListener(this) 显示覆盖 AdapterItemClicked(int position, Bundle data) 方法的错误。所以你必须重写AdapterItemClicked(int position, Bundle data) 方法。
  • 现在完成。谢谢。但是先生可以再问一个问题吗?
  • 是的。你可以问
  • 我希望评分栏值(输入的)即使在 ListView 滚动时也保持不变(截至目前,当 ListView 滚动时评分会消失)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-21
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多