瀏覽代碼

new list adapter + pass some data back and forth

David 9 年之前
父節點
當前提交
b5bba007ad

+ 52 - 0
app/src/main/java/com/example/david/libretasker/LazyAdapter.java

@@ -0,0 +1,52 @@
+package com.example.david.libretasker;
+
+import android.app.Activity;
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+
+/**
+ * Created by david on 06/01/17.
+ */
+
+public class LazyAdapter extends BaseAdapter {
+    private ArrayList<String> data; //HashMap<String, String>
+    private static LayoutInflater inflater = null;
+
+    public LazyAdapter(Activity a, ArrayList<String> d) {
+        data = d;
+        inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+    }
+
+    public int getCount() {
+        return data.size();
+    }
+
+    public Object getItem(int position) {
+        return position;
+    }
+
+    public long getItemId(int position) {
+        return position;
+    }
+
+    public View getView(int position, View convertView, ViewGroup parent) {
+        View vi=convertView;
+        if(convertView==null)
+            vi = inflater.inflate(R.layout.profile_row, null);
+
+        TextView title = (TextView)vi.findViewById(R.id.profileRowTitle);
+        String song = data.get(position);
+
+        title.setText(song);
+
+        return vi;
+    }
+}

+ 26 - 8
app/src/main/java/com/example/david/libretasker/MainActivity.java

@@ -13,6 +13,12 @@ import java.util.ArrayList;
 
 
 public class MainActivity extends AppCompatActivity {
+    final static int EDIT_PROFILE = 0;
+    final static int NEW_PROFILE = 1;
+    ArrayList<String> profileList = new ArrayList<>();
+    //ArrayAdapter profileAdapter;
+    LazyAdapter profileAdapter;
+    ListView listview;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -25,17 +31,16 @@ public class MainActivity extends AppCompatActivity {
             @Override
             public void onClick(View view) {
                 Intent i = new Intent(getApplicationContext(), NewEventActivity.class);
-                startActivity(i);
+                i.putExtra("MODE", NEW_PROFILE);
+                startActivityForResult(i, NEW_PROFILE);
             }
         });
 
-        ArrayList<String> list = new ArrayList<>();
-        list.add("Test1");
-        list.add("Test2");
-        ArrayAdapter adapter = new ArrayAdapter( this, android.R.layout.simple_list_item_1, list);
+        //profileAdapter = new ArrayAdapter( this, android.R.layout.simple_list_item_1, profileList);
+        profileAdapter = new LazyAdapter(this, profileList);
 
-        ListView listview = (ListView) findViewById(R.id.listview2);
-        listview.setAdapter(adapter);
+        listview = (ListView) findViewById(R.id.listview2);
+        listview.setAdapter(profileAdapter);
         listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 
             @Override
@@ -51,4 +56,17 @@ public class MainActivity extends AppCompatActivity {
         startService(new Intent(this, MainService.class));
     }
 
-}
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        switch (resultCode) {
+            case NEW_PROFILE:
+                Bundle b = data.getExtras();
+                profileList.add(b.getString("title"));
+                profileAdapter.notifyDataSetChanged();
+                Helpers.setListViewHeightBasedOnChildren(listview, 1);
+                break;
+
+        }
+    }
+}

+ 17 - 1
app/src/main/java/com/example/david/libretasker/NewEventActivity.java

@@ -6,6 +6,7 @@ import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
+import android.widget.EditText;
 import android.widget.ListView;
 
 import com.example.david.libretasker.inputSelector.Event;
@@ -19,11 +20,14 @@ public class NewEventActivity extends AppCompatActivity {
     ArrayList<String> events = new ArrayList<>();
     ArrayAdapter adapter;
     ListView listview;
+    int mode;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_new_event);
+        mode = getIntent().getIntExtra("MODE", -9999);
+
         Button event = (Button) findViewById(R.id.addEventButton);
         event.setOnClickListener(new View.OnClickListener() {
             @Override
@@ -48,6 +52,18 @@ public class NewEventActivity extends AppCompatActivity {
         listview = (ListView) findViewById(R.id.eventList);
         listview.setAdapter(adapter);
         Helpers.setListViewHeightBasedOnChildren(listview, 1);
+
+
+        Button save = (Button) findViewById(R.id.saveProfileButton);
+        save.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                Intent intent = new Intent();
+                intent.putExtra("title", ((EditText)findViewById(R.id.editText)).getText().toString());
+                setResult(mode, intent);
+                finish();
+            }
+        });
     }
 
     @Override
@@ -58,7 +74,7 @@ public class NewEventActivity extends AppCompatActivity {
         }
         switch (requestCode) {
             case NEW_EVENT_CODE:
-                Bundle b = data.getBundleExtra("data");
+                Bundle b = data.getExtras();
                 events.add(b.getString("title"));
                 adapter.notifyDataSetChanged();
                 Helpers.setListViewHeightBasedOnChildren(listview, 1);

+ 1 - 3
app/src/main/java/com/example/david/libretasker/inputSelector/Event.java

@@ -99,9 +99,7 @@ public class Event extends AppCompatActivity {
             @Override
             public void onClick(View view) {
                 Intent intent = new Intent();
-                Bundle b = new Bundle();
-                b.putString("title", selectedType);
-                intent.putExtra("data", b);
+                intent.putExtra("title", selectedType);
                 setResult(mode, intent);
                 finish();
             }

+ 6 - 0
app/src/main/res/layout/activity_new_event.xml

@@ -75,5 +75,11 @@
         android:id="@+id/addActionButton"
         android:layout_weight="1" />
 
+    <Button
+        android:text="Save profile"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:id="@+id/saveProfileButton" />
+
 </LinearLayout>
 </ScrollView>

+ 32 - 0
app/src/main/res/layout/profile_row.xml

@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:orientation="horizontal" android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:id="@+id/row">
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:id="@+id/profileRowTitle"
+        android:layout_weight="1" />
+
+    <ImageButton
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        app:srcCompat="@android:drawable/ic_menu_edit"
+        android:id="@+id/imageButton"
+        android:layout_weight="1"
+        style="?android:attr/borderlessButtonStyle"
+        />
+
+    <ImageButton
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        app:srcCompat="@android:drawable/ic_delete"
+        android:id="@+id/imageButton2"
+        android:layout_weight="1"
+        style="@android:style/Widget.Material.Light.Button.Borderless"
+        />
+
+</LinearLayout>