【问题标题】:Custom listview with edittext and button not working带有edittext和按钮的自定义列表视图不起作用
【发布时间】:2016-03-22 12:40:58
【问题描述】:

我创建了自己的多列列表视图,每行都有一个编辑文本和一个按钮。我正在为每个按钮设置标签值,以识别单击了哪一行。但是在 onClick 方法中,我试图读取值并将其转换为 int。但是,此 edittext 的值始终是一个空字符串。代码如下:

 wrapper.mySimpleAdapter = new SimpleAdapter(Chores.this, chores, R.layout.parent_list_format, new String[]{"chore","child_name","points"},new int[]{R.id.value_name,R.id.child_name,R.id.point_value })
                {
                    @Override
                    public View getView (final int position, View convertView, ViewGroup parent)
                    {
                        //Setting the tag for each button of the list to the chore name
                        //to be able to identify which chore is to be claimed
                        final View v = super.getView(position, convertView, parent);
                        Button b=(Button)v.findViewById(R.id.rate);
                        String names = ( v.findViewById(R.id.child_name)).toString();
                        names += ("::") + ( v.findViewById(R.id.value_name)).toString();
                        b.setTag(names);

                        //When one of the list buttons have been clicked
                        b.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {

                                try {
                                    //Sends the chore data to the php file to update the database and reload page
                                    String names = (String) view.getTag();
                                    String child = names.substring(0,names.indexOf(":"));
                                    String chore = names.substring(names.lastIndexOf(":"), names.length()-1);
                                    **EditText points = (EditText) findViewById(R.id.pointsGiven);
                                    int pointsGiven = Integer.parseInt (points.getText().toString());**
                                    Chore chores = new Chore(chore,pointsGiven,accountConnect.user.username,child);


                                    uploadChore(chores);
                                    startActivity(new Intent(v.getContext(), Chores.class));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        return v;
                    }
                };

星星表示我正在谈论的两条线。我猜它在创建每一行时正在读取这个edittext,这就是为什么它总是返回一个空字符串。有什么方法可以在单击按钮时获取edittext的值?

这是列表视图 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/value_name"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_weight="1"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"  />

<TextView
    android:id="@+id/child_name"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_weight="1"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"  />

<TextView
    android:id="@+id/point_value"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_weight="1"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"   />

<EditText
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="points"
    android:ems="10"
    android:id="@+id/pointsGiven"
    />

<Button
    android:id="@+id/rate"
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:text = "Rate"
    android:layout_weight="1" />


</LinearLayout>

【问题讨论】:

  • pointsGiven id 在什么布局中?您当前正在尝试在活动中找到它,而不是在列表视图行布局中
  • pointsGiven 在 listview 行布局 xml 文件中
  • 那你需要改用v.findViewById
  • 另外,你想在( v.findViewById(R.id.child_name)).toString();做什么?这不是您从 Textview 获取文本的方式

标签: android listview android-edittext custom-lists


【解决方案1】:

假设您的EditText points 位于您的行内,您需要在您的行视图中找到它:

@Override
public void onClick(View view) {
    //your code
    EditText points = (EditText) view.findViewById(R.id.pointsGiven);
    //your code
}

【讨论】:

  • 执行此操作时出现此错误:java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on an null object reference
  • 这意味着您的布局文件中没有带有给定id 的EditText。也许编辑您的原始帖子并显示行布局?
  • @Amy - “view”是确实没有任何子视图的按钮(--> NPE)。你需要 "v" 就像 cricket_007 说的:EditText points = (EditText) v.findViewById(R.id.pointsGiven);
【解决方案2】:
 @Override
public View getView (final int position, View convertView, ViewGroup parent)
{
    //Setting the tag for each button of the list to the chore name
    //to be able to identify which chore is to be claimed
    final View v = super.getView(position, convertView, parent);
    Button b=(Button)v.findViewById(R.id.rate);
    String names = ( v.findViewById(R.id.child_name)).toString();
    names += ("::") + ( v.findViewById(R.id.value_name)).toString();
    b.setTag(names);
    EditText points = (EditText) v.findViewById(R.id.pointsGiven);
    //When one of the list buttons have been clicked
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            try {
                //Sends the chore data to the php file to update the database and reload page
                String names = (String) view.getTag();
                String child = names.substring(0,names.indexOf(":"));
                String chore = names.substring(names.lastIndexOf(":"), names.length()-1);

                int pointsGiven = Integer.parseInt (points.getText().toString());**
                Chore chores = new Chore(chore,pointsGiven,accountConnect.user.username,child);


                uploadChore(chores);
                startActivity(new Intent(v.getContext(), Chores.class));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    return v;
}

在适配器中的 onClick 之前初始化 EditText 并尝试获取相同的值。

【讨论】:

    【解决方案3】:

    您必须将被点击项目的位置传递给点击后获取该项目:

                      //When one of the list buttons have been clicked
    
                        b.setTag(position);
                        b.setOnClickListener(new View.OnClickListener() {
    
                            @Override
                            public void onClick(View view) {
                            Item item = getItem((int)view.getTag());
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 2016-01-21
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多