Youhou! User can now add new friends !

When done, a new element is created in the friends list.

User can now add and remove friends.

Next step :
- Get real people. Write characters in xml files
This commit is contained in:
2012-10-03 18:28:23 +02:00
parent b83eb23c75
commit c071e15791
4 changed files with 72 additions and 25 deletions

View File

@@ -16,18 +16,12 @@
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FriendAdder"
android:label="@string/title_activity_friend_adder" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

View File

@@ -11,7 +11,7 @@
android:layout_height="wrap_content"
android:text="@string/friend_name"/>
<EditText android:id="@+id/title"
<EditText android:id="@+id/friend_name"
android:maxLines="1"
android:layout_marginTop="2dip"
android:layout_width="wrap_content"

View File

@@ -2,14 +2,34 @@ package fr.lengrand.knowmysize;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FriendAdder extends Activity {
private static final String TAG = "FriendAdder";
public static final String RES_FRIEND = "FRIEND";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friend_adder);
// Adds button interaction to accept friend name
Button ok = (Button) findViewById(R.id.add_ok_button);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
String new_friend = ((EditText) findViewById(R.id.friend_name)).getText().toString();
intent.putExtra(RES_FRIEND, new_friend);
setResult(RESULT_OK, intent);
finish();
}
});
}
@Override

View File

@@ -6,6 +6,7 @@ import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
@@ -17,6 +18,7 @@ import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
// This is the Adapter being used to display the list's data
SimpleCursorAdapter mAdapter;
@@ -28,16 +30,16 @@ public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.add_button);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), FriendAdder.class);
startActivityForResult(myIntent, 0);
}
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), FriendAdder.class);
startActivityForResult(myIntent, 0);
}
});
});
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends);
ListView list = (ListView) findViewById(R.id.friendList);
@@ -74,7 +76,7 @@ public class MainActivity extends Activity {
}
}
else if(v.getId() == R.id.add_button){
}
else{
System.out.println(" Unknown View!!!");
@@ -99,21 +101,52 @@ public class MainActivity extends Activity {
System.out.println("Unexpected behaviour ! What do ? ?"); //FIXME : Log instead, or message pop up
}
// Button myButton = (Button)findViewById(R.id.add_button);
// String myStr = "";
// for (int i = 0; i < friends.length; i++) {
// myStr += friends[i];
//
// }
// myButton.setText(myStr);
// Button myButton = (Button)findViewById(R.id.add_button);
// String myStr = "";
// for (int i = 0; i < friends.length; i++) {
// myStr += friends[i];
//
// }
// myButton.setText(myStr);
ListView list = (ListView) findViewById(R.id.friendList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends);
list.setAdapter(adapter); // updates list of friends
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent pData)
{
if (resultCode == Activity.RESULT_OK )
{
String new_friend = pData.getExtras().getString( FriendAdder.RES_FRIEND );
Log.v( TAG, "Retrieved Value zData is "+ new_friend );
// Button myButton = (Button)findViewById(R.id.add_button);
// myButton.setText(new_friend);
ListView list = (ListView) findViewById(R.id.friendList);
friends = addItem(new_friend);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends);
list.setAdapter(adapter); // updates list of friends
}
else{
Log.v( TAG, "Problem : Unexpected requestCode");
}
}
private String[] addItem(String new_friend) {
ArrayList<String> temp = new ArrayList<String>();
temp.add(new_friend); // Adds new friend in first position
for (int i = 0; i < friends.length; i++) {
temp.add(friends[i]);
}
return temp.toArray(new String[temp.size()]);
}
private String[] removeItem(String friend) {
//FIXME : Checks here
ArrayList<String> temp = new ArrayList<String>();