简体   繁体   中英

Android: ListView & customAdapter - List when clicked showes info of 2 items / editText not clickable

I did a custom Adapter for my List View which extends a basicadapter, it was working fine until i added another row to my list_item.xml so now when in my OnItemClickListner, on click should show the description of the item in the row under it which was hidden. that works fine except that when i click on any item it shows the row under of description and another's of item in the bottom of the list, although i only pressed on one item and when i scroll down and up again the row of the item a clicked on disappears but the other one that opened by itself stays opened. i searched and found that it might be because i dont use bindView and any List takes from 5-10 items then reuses them. if that the case how can i implement a bindVIew to my customAdapter. ataached s my on click listener and getView of my customAdapter

public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {
             // getting values from selected ListItem
                if ((((LinearLayout) view.findViewById(R.id.edit)).getVisibility()) == (View.VISIBLE))
                {
                    ((LinearLayout) view.findViewById(R.id.edit)).setVisibility(View.GONE);
                }else
                {
                    ((LinearLayout) view.findViewById(R.id.edit)).setVisibility(View.VISIBLE);
                    ((EditText) view.findViewById(R.id.itemn)).setFocusable(true);              
                    new LoadCategories().execute();

                }

2nd problem: i have my description row has an editText which shows the description which is visible under the item when i press on any item. but i cant press on it to change it or edit it when it shows up.

  public View getView(final int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(l, null);
            holder  = new ViewHolder();

            holder.id = (TextView)convertView.findViewById(R.id.id);
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.price = (TextView)convertView.findViewById(R.id.price);
            holder.description = (TextView)convertView.findViewById(R.id.description);
            holder.button = (Button) convertView.findViewById(R.id.update);
            holder.btn = (Button) convertView.findViewById(R.id.order);

            holder.en = (EditText)convertView.findViewById(R.id.itemn);
            holder.ep = (EditText)convertView.findViewById(R.id.pri);
            holder.ed = (EditText)convertView.findViewById(R.id.descript);

            convertView.setTag(holder);
        }

        else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.id.setText(getId(getItem(position)));
        holder.name.setText(getName(getItem(position)));
        holder.price.setText("£ " + getPrice(getItem(position)));
        holder.description.setText(getDesc(getItem(position)));
        holder.btn.setVisibility(View.GONE);


       holder.en.setText((holder.name.getText()).toString());
       holder.ep.setText((holder.price.getText()).toString());
       holder.ed.setText((holder.description.getText()).toString());

        return convertView;
    }
    public void clear(){
        itemsList.clear();
        notifyDataSetChanged();
    }

}

here is my list_item.xml. Baically first tableRow shows all the info passed from an array of hashMap. 2nd row not used yet. Third TableRow takes the info from the first row and set it to the editText which then can be modified so afterwards it will be used to update the database with the corrected information. third row is hidden and is showed when an item is pressed.

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false" >

    <TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:visibility="gone" />

    <TextView
        android:id="@+id/quantity"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="left"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:visibility="gone" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight="3"
        android:gravity="left"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="right"
        android:paddingRight="15dp"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <Button
        android:id="@+id/order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="0"
        android:focusable="false"
        android:minHeight="0dp"
        android:minWidth="50dp"
        android:text="+"
        android:textStyle="bold" />

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tableRow1"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:visibility="gone" />

</TableRow>

<TableRow
    android:id="@+id/TableRow01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="false"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/tableRow2"
    android:gravity="center_horizontal" >

    <LinearLayout
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <EditText
            android:id="@+id/itemn"
            android:layout_width="match_parent"
            android:layout_height="69dp"
            android:ems="10"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="top|left"
            android:hint="NAME"
            android:inputType="text"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/descript"
            android:layout_width="match_parent"
            android:layout_height="82dp"
            android:ems="10"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="top|left"
            android:hint="DESCRIPTION"
            android:inputType="textMultiLine"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/pri"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:ems="10"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="top|left"
            android:hint="£ PRICE"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <Spinner
            android:id="@+id/spinne"
            style="@style/spinner_style"
            android:layout_width="352dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/dropdown"
            android:gravity="center"
            android:overScrollMode="always"
            android:prompt="@string/orders"
            android:textAlignment="center"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <Button
            android:id="@+id/update"
            android:layout_width="135dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.55"
            android:focusable="false"
            android:text="update"
            android:textAllCaps="true" />

    </LinearLayout>
</TableRow>

</RelativeLayout>

although i only pressed on one item and when i scroll down and up again the row of the item a clicked on disappears but the other one that opened by itself stays opened.

You are fighting the way that ListView's recycle Views, watch Google's Turbo-Charge your UI for a detailed explanation of how this works. The solution is simple: inside your Adapter you must remember which rows have been expanded and set them accordingly in getView() or collapse every row by default.

but i cant press on it to change it or edit it when it shows up.

I noticed that you are making an EditText focusable, try changing setFocusableInTouchMode() instead. These methods are linked in subtle ways.


Addition
One observation I have is that you have some unnecessary layouts, if a layout only has one child then you can probably remove it. For instance, you can either remove the TableLayout or the RelativeLayout and you can either remove LinearLayout edit or the TableRow.

i can't repress on the item to close or hide the expandable part after i am done with it.

I don't see a specific reason why you cannot touch anything in row one or two to hide the third row again... Perhaps you should add a Button that hides / shows this row/

what i face is that the expanded part that opens when i press on an item disappears when i scroll down

I walked another user through this: Android: ListView bug
In my answer there pay attention to how I use expanded to track whether each row should show or hide the extra layout, and notice how expanded is controlled in mName .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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