简体   繁体   English

如何从Android中的自定义列表视图中选择一个具有ID的TextView?

[英]How to select a textview having an id from a custom listview in android?

I have a custom listview having the following layout: 我有一个具有以下布局的自定义列表视图: 布局的GUI视图

I have defined an onClick method for 'qadd' button and 'qsub' button in xml using android:onClick property. 我已经使用android:onClick属性为xml中的“ qadd”按钮和“ qsub”按钮定义了onClick方法。

In my Activity class how do I select the 'quantity' textView for current position? 在我的Activity类中,如何为当前位置选择“数量” textView?

I tried this but it only updates 'quantity' textView at first position. 我试过了,但它只在第一位置更新了“数量” textView。

public void addClicked(View v) {
        final int position = list.getPositionForView((View) v.getParent().getParent());
        long rec_id = adapter.getItemId(position);

        JSONObject o = (JSONObject) fullList.get(rec_id + "");

        TextView q = (TextView)findViewById(R.id.quantity);

        int quantity = Integer.parseInt((String) q.getText());

        quantity++;

        q.setText(quantity + "");

        try {

            String price = o.getString("price");
            subtotal = subtotal + (Integer.parseInt(price) * quantity);
            total_text = (TextView) findViewById(R.id.subtotal);
            total_text.setText(subtotal + "");

            // Log.d("position", price);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

The above code goes wrong at this line: 上面的代码在此行出错:

TextView q = (TextView)findViewById(R.id.quantity);

It always selects the textView at first position. 它总是在第一个位置选择textView。 Please ask if any other piece of code is needed. 请询问是否需要其他代码。

Entire custom_list_layout.xml is as follows: 整个custom_list_layout.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/checkBox1" >

        <LinearLayout
            android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:background="@drawable/image_bg"
            android:padding="3dip" >

            <ImageView
                android:id="@+id/item_img"
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2.5"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/item_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Item name"
                android:textColor="#040404"
                android:textSize="15dip"
                android:textStyle="bold"
                android:typeface="sans" />

            <TextView
                android:id="@+id/item_desc"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Item description"
                android:textColor="#343434"
                android:textSize="10dip" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.75"
            android:gravity="right"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/rupee"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="Rs. "
                    android:textColor="#EC1669"
                    android:textSize="10dip"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/item_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dip"
                    android:gravity="right"
                    android:text="0 "
                    android:textColor="#EC1669"
                    android:textSize="10dip"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="bottom|right" >

                <Button
                    android:id="@+id/qadd"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="24dp"
                    android:minWidth="24dp"
                    android:onClick="addClicked"
                    android:text="+" />

                <TextView
                    android:id="@+id/quantity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <Button
                    android:id="@+id/qsub"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="24dp"
                    android:minWidth="24dp"
                    android:onClick="subClicked"
                    android:text="-" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

Link to my entire project. 链接到我的整个项目。

Your textview to address should be the one within the list item row you specify in XML for the list adapter. 您要处理的textview应该是您在XML中为列表适配器指定的列表项行中的文本视图。

If you're using a custom adapter, within getView you should wire up the textview as follows: 如果您使用的是自定义适配器,则应在getView中按以下方式连接textview:

// create a new TextView for each item referenced by the Adapter
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View listView = convertView;

        if (listView == null) { // if it's not recycled, initialize some attributes
            // get layout from xml
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            listView = inflater.inflate(R.layout.browse_product_item, null);
        }


        final ListProductHolder holder;

        if (listView.getTag() != null) {
            holder = (listProductHolder) listView.getTag();
        } else {
            holder = new listProductHolder();
            listView.setTag(holder);

            holder.text= (TextView) listView.findViewById(R.id.quantity);

One you have the proper TextView from within each item in the listView, then it's a simple process to wire up an OnClickListener for that view and respond to those click events. 您可以从listView的每个项目中获得合适的TextView,然后通过简单的过程为该视图连接一个OnClickListener并响应这些单击事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM