Use a ListView to store discovered child devices

The previous approach was to list buttons in a TableLayout.
However, this approach does not work well if there are more
items than can fit on the screen.

To allow scrolling of items, and also a better presentation
of said items, use a ListView.
This commit is contained in:
Branden Archer
2015-12-29 16:19:50 -05:00
parent 68d9c69a01
commit a4f8adf7a5
3 changed files with 76 additions and 35 deletions

View File

@@ -29,12 +29,13 @@
android:text="@string/selectChildDevice"
android:textSize="20sp" />
<TableLayout
<ListView
android:id="@+id/ServiceTable"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.32"
android:padding="10dp" >
</TableLayout>
</ListView>
</LinearLayout>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/availableChildrenList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />

View File

@@ -24,9 +24,10 @@ import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DiscoverActivity extends Activity
{
@@ -69,6 +70,29 @@ public class DiscoverActivity extends Activity
{
final NsdManager nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
final ListView serviceTable = (ListView) findViewById(R.id.ServiceTable);
final ArrayAdapter<ServiceInfoWrapper> availableServicesAdapter = new ArrayAdapter<ServiceInfoWrapper>(this,
R.layout.available_children_list);
serviceTable.setAdapter(availableServicesAdapter);
serviceTable.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
ServiceInfoWrapper info = (ServiceInfoWrapper) parent.getItemAtPosition(position);
Intent i = new Intent(getApplicationContext(), ListenActivity.class);
Bundle b = new Bundle();
b.putString("address", info.getAddress());
b.putInt("port", info.getPort());
b.putString("name", info.getName());
i.putExtras(b);
startActivity(i);
}
});
// Instantiate a new DiscoveryListener
_discoveryListener = new NsdManager.DiscoveryListener()
{
@@ -111,35 +135,7 @@ public class DiscoverActivity extends Activity
@Override
public void run()
{
final TableLayout serviceTable = (TableLayout) findViewById(R.id.ServiceTable);
final TableRow row = new TableRow(DiscoverActivity.this.getApplicationContext());
serviceTable.addView(row);
// If there is more than one service on the network, it will
// have a number at the end, but will appear as the following:
// "ProtectBabyMonitor\\032(number)
// Replace \\032 with a ""
final String fixedServiceName = serviceInfo.getServiceName().replace("\\\\032", " ");
final Button serviceButton = new Button(DiscoverActivity.this.getApplicationContext());
serviceButton.setText(fixedServiceName);
row.addView(serviceButton);
serviceButton.setTextSize(15);
serviceButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(), ListenActivity.class);
Bundle b = new Bundle();
b.putString("address", serviceInfo.getHost().getHostAddress());
b.putInt("port", serviceInfo.getPort());
b.putString("name", fixedServiceName);
i.putExtras(b);
startActivity(i);
}
});
availableServicesAdapter.add(new ServiceInfoWrapper(serviceInfo));
}
});
}
@@ -186,3 +182,37 @@ public class DiscoverActivity extends Activity
serviceType, NsdManager.PROTOCOL_DNS_SD, _discoveryListener);
}
}
class ServiceInfoWrapper
{
private NsdServiceInfo _info;
public ServiceInfoWrapper(NsdServiceInfo info)
{
_info = info;
}
public String getAddress()
{
return _info.getHost().getHostAddress();
}
public int getPort()
{
return _info.getPort();
}
public String getName()
{
// If there is more than one service on the network, it will
// have a number at the end, but will appear as the following:
// "ProtectBabyMonitor\\032(number)
// Replace \\032 with a ""
return _info.getServiceName().replace("\\\\032", " ");
}
@Override
public String toString()
{
return getName();
}
}