ServerListActivity: Show "Add server" item if no server in database yet

This commit is contained in:
Sebastian Kaspari 2010-09-18 10:53:39 +02:00
parent 8775d93020
commit a535d74a85
4 changed files with 240 additions and 161 deletions

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Yaaic - Yet Another Android IRC Client
Copyright 2009-2010 Sebastian Kaspari
This file is part of Yaaic.
Yaaic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Yaaic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_add"
android:layout_gravity="center_vertical"
android:paddingRight="5px" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/add_server_list"
android:textSize="14px" />
</LinearLayout>
</LinearLayout>

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="add_server_menu">Add server</string>
<string name="add_server_list">Add server</string>
<string name="settings_menu">Settings</string>
<string name="about_menu">About</string>
<string name="exit_menu">Exit</string>

View File

@ -139,6 +139,12 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
protected void onListItemClick(ListView l, View v, int position, long id) {
Server server = adapter.getItem(position);
if (server == null) {
// "Add server" was selected
startActivityForResult(new Intent(this, AddServerActivity.class), 0);
return;
}
Intent intent = new Intent(this, ConversationActivity.class);
if (server.getStatus() == Status.DISCONNECTED) {
@ -157,6 +163,11 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
{
final Server server = adapter.getItem(position);
if (server == null) {
// "Add server" view selected
return true;
}
final CharSequence[] items = {
getString(R.string.connect),
getString(R.string.disconnect),

View File

@ -62,7 +62,14 @@ public class ServerListAdapter extends BaseAdapter
*/
public int getCount()
{
return servers.size();
int size = servers.size();
// Display "Add server" item
if (size == 0) {
return 1;
}
return size;
}
/**
@ -72,6 +79,10 @@ public class ServerListAdapter extends BaseAdapter
*/
public Server getItem(int position)
{
if (servers.size() == 0) {
return null; // No server object for the "add server" view
}
return servers.get(position);
}
@ -82,6 +93,10 @@ public class ServerListAdapter extends BaseAdapter
*/
public long getItemId(int position)
{
if (servers.size() == 0) {
return 0;
}
return getItem(position).getId();
}
@ -97,6 +112,12 @@ public class ServerListAdapter extends BaseAdapter
Server server = getItem(position);
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (server == null) {
// Return "Add server" view
return inflater.inflate(R.layout.addserveritem, null);
}
View v = inflater.inflate(R.layout.serveritem, null);
((TextView) v.findViewById(R.id.title)).setText(server.getTitle());