Question

When developing an android application in java, I have a mainActivity which sets up a list adapter in the following way:

private void refreshDisplay() 
{
adapter = new ItemAdapter(this, R.layout.listview_item_row, myList);
setListAdapter(adapter);
}

This method refreshDisplay is called when data making up the list is changed via user input and the mainactivity is returned to after completing the edit activity.

The list itself is composed of a ListView inside the MainActivity.xml file, which loads a list_item_row_layout thus:

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>

and inside the list_item_row.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/TitleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"/>

<TextView
    android:id="@+id/DetailText"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:clickable="true" />
</RelativeLayout>

Inside the list adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    myHolder holder;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new itemHolder();
        holder.titleText = (TextView)row.findViewById(R.id.TitleText);
    holder.textDetail = (TextView)row.findViewById(R.id.DetailText);
        row.setTag(holder);
    }
    else
    {
        holder = (myHolder)row.getTag();
    }
    myItem = data.get(position);
    holder.titleText.setText(myItem.getText());
    holder.textDetail.setText(myItem.getDetail());
    if (holder.textDetail.getVisibility()==0) {
        holder.titleText.setVisibility(8);
        holder.textDetail.setVisibility(0);
    }
    else if (holder.titleText.getVisibility()>0) {
        holder.textDetail.setVisibility(8);
        holder.titleText.setVisibility(0);
    }
    else {
        holder.titleText.setVisibility(0);
        holder.textDetail.setVisibility(8);
    }
    return row;
}

When the list is generated it displays without problem, and even runs correctly as far as accepting the first click which is supposed to expand the TitleText into the more verbose DetailText. So the DetailText starts with a Visibility of GONE and the TitleText is set to VISIBLE. If the user clicks on the listitem then it does successfully switch to the DetailText field. But the problem arises when the same item is clicked on again - the TitleText field does not replace the detailed version. I did initially think the DetailText was not clickable - but what puzzles me now is that if one of the other list items are clicked on then the original expanded one reverts back to the title and the clicked on one expands correctly! But how can I make it so that the expanded list item can be contracted into the title text without expanding another list item? Is there some problem that means a list item that is set to VISIBLE in code cannot be then set back to GONE or INVISIBLE even?

Was it helpful?

Solution

Had to use a bit of a hack in the end since was nearing the end of my usable hair to tear out haha.

Since reverting back to all todos with titles displayed was also just the way things appeared when the activity was first created - I put the following code in where the second click happened.

There is a bit of a pause though before this diplays on my HTC One, it remains to be seen when I test it on slower phones if its an acceptable solution.

Intent intentstart = new Intent(MainActivity.this, MainActivity.class); 

intentstart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intentstart);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top