Android: listview array adapter does not load content correctly

I have a Theme.Dialog function that represents a list of devices for a user. this operation uses a ListView with a dynamically created ArrayAdapter to display a list of devices. For some reason, however, the ListView (and therefore the activity itself) does not resize for its correct contents. Instead, it acts as if it has a typing width of about half the screen (and this is saved on different devices with different screen sizes). However, I cannot find out for life where this is installed.

Can anyone see what I am missing? (I tried to remove unnecessary bits from the following code)

Here is my activity:

public class DeviceListActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Setup the window setContentView(R.layout.device_list); // Initialize array adapters mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); // Find and set up the ListView for paired devices ListView pairedListView = (ListView) findViewById(R.id.paired_devices); pairedListView.setAdapter(mPairedDevicesArrayAdapter); //the following doesn't do anything //getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); mPairedDevicesArrayAdapter.add("asdfasdfa"); } } 

Here is device_list.xml:

 <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@id/paired_devices" android:layout_width="wrap_content" android:layout_height="fill_parent" android:stackFromBottom="true" android:background="#0000FF"/> <!-- the colour shows the listview stretching wider than the text it contains --> </LinearLayout> 

Here is the relevant part of my AndroidManifest.xml:

 <activity android:name=".DeviceListActivity" android:configChanges="keyboardHidden|orientation" android:label="too wide" android:theme="@android:style/Theme.Dialog"> </activity> 

And here is device_name.xml (which is used to initialize the ArrayAdapter:

 <?xml version="1.0" encoding="UTF-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5.0dip" android:textSize="18.0sp" /> 

And if you are stuck with reading the question so far, I'm already thankful.

Here's the end result (with the rest of my application in the background): I can't figure out why the dialog isn't wrapping the text.

0
source share
3 answers

I understand that this is essentially a closed-ended question, but I believe that by setting windowIsFloating to true in the subject, as shown in fooobar.com/questions/1135755 / ... in this answer, your problem will be fixed.

+1
source

I am sure that it is because of android:theme="@android:style/Theme.Dialog" , these annoying dialogs with their own width and height.

Try the following: Create a Fragment that represents your ListActivity as a Listview . Placing this on top of other fragments creates conversational behavior.

I am 100% sure the problem is Theme.Dialog. Your best shot will change your strategy for displaying this list. (E.g. using fragments)

0
source

i finally was able to get this to work. Listview is subclassed and its dimension has changed. It seems that listview will initially measure only the first child. Therefore, he uses the width of the first child. I changed it to look for children of listviews for the widest row.

so that your xml for the contents of the dialog box looks like this:

 <?xml version="1.0" encoding="UTF-8"?> 

 <com.mypackage.myListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="match_parent" /> 

and myListView will look like this:

 public class MyListView extends ListView{ public MyListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int maxWidth = meathureWidthByChilds() + getPaddingLeft() + getPaddingRight(); super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.EXACTLY), heightMeasureSpec); } public int meathureWidthByChilds() { int maxWidth = 0; View view = null; for (int i = 0; i < getAdapter().getCount(); i++) { view = getAdapter().getView(i, view, this); //this measures the view before its rendered with no constraints from parent view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); if (view.getMeasuredWidth() > maxWidth){ maxWidth = view.getMeasuredWidth(); } } return maxWidth; } } 
0
source

Source: https://habr.com/ru/post/1496951/


All Articles