自定义列表视图,其中edittext和按钮不起作用

时间:2016-03-22 12:40:58

标签: android listview android-edittext custom-lists

我创建了自己的多列列表视图,每行都有一个edittext和一个按钮。我正在为每个按钮设置标记值以标识已单击的行。但是在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的值吗?

这是listview 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>

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

 @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 ,并尝试获取相同的值。

答案 2 :(得分:0)

你必须传递给on点击所点击项目的位置,然后获取项目:

                  //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());
                    }