ソート機能を実装しました
1 parent d870557 commit 5380ce038f1b40ae7a045274f36dfc8c14b3b5b8
a-hongo authored on 4 Jun 2019
Showing 3 changed files
View
33
app/src/main/java/com/example/cosmosclient/views/RequestList.java
package com.example.cosmosclient.views;
 
public class RequestList {
private String product;
private String deadline;
private String location;
 
public RequestList(String product, String deadline, String location) {
this.product = product;
this.deadline = deadline;
this.location = location;
}
 
public String getProduct() {
return product;
}
 
public void setProduct(String product) {
this.product = product;
}
 
public String getDeadline() {
return deadline;
}
 
public void setDeadline(String deadline) {
this.deadline = deadline;
}
 
public String getLocation() {
return location;
}
 
public void setLocation(String location) {
this.location = location;
}
}
View
173
app/src/main/java/com/example/cosmosclient/views/RequestListActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
 
import com.example.cosmosclient.R;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
 
import static android.graphics.Color.BLACK;
 
public class RequestListActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
 
Toast toast; //動作テスト用
boolean productColorFlag = true; //買うもの色制御
boolean deadlineColorFlag = true; //購入期限色制御
boolean locationColorFlag = true; //場所色制御
 
ArrayList<RequestList> requestList = new ArrayList<>();
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
 
LinearLayout linerlayoutView = (LinearLayout) findViewById(R.id.product);
linerlayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(RequestListActivity.this, "product sorted", Toast.LENGTH_SHORT).show();
ImageView product = (ImageView) findViewById(R.id.imageView);
product.setColorFilter(BLACK);
}
});
requestList.add(new RequestList("わさび", "明日まで", "スーパー"));
requestList.add(new RequestList("ケーキ" , "12/25", "ダニエル"));
requestList.add(new RequestList("からし", "12/22", "コンビニ"));
 
AddRequestList(requestList);
 
LinearLayout productView = (LinearLayout) findViewById(R.id.product);
productView.setOnClickListener(productOnClick);
 
LinearLayout deadlineView = (LinearLayout)findViewById(R.id.deadline);
deadlineView.setOnClickListener(deadlineOnClick);
 
LinearLayout locationView = (LinearLayout)findViewById(R.id.location);
locationView.setOnClickListener(locationOnClick);
 
}
 
// @Override
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
 
private void AddRequestList(ArrayList<RequestList> requestList) {
TableLayout requesttable = (TableLayout)findViewById(R.id.RequestList);
 
for (int i = 0; i < requestList.size(); i++) {
TableRow tableRow = new TableRow(this);
 
TextView textProduct = new TextView(this);
textProduct.setText(requestList.get(i).getProduct());
textProduct.setTextSize(20);
textProduct.setHeight(150);
textProduct.setGravity(Gravity.CENTER);
tableRow.addView(textProduct);
 
TextView textDeadline = new TextView(this);
textDeadline.setText(requestList.get(i).getDeadline());
textDeadline.setTextSize(20);
textDeadline.setHeight(150);
textDeadline.setGravity(Gravity.CENTER);
tableRow.addView(textDeadline);
 
TextView textLocation = new TextView(this);
textLocation.setText(requestList.get(i).getLocation());
textLocation.setTextSize(20);
textLocation.setHeight(150);
textLocation.setGravity(Gravity.CENTER);
tableRow.addView(textLocation);
 
requesttable.addView(tableRow);
}
}
 
private void DeleateRequestList() {
TableLayout requesttable = (TableLayout) findViewById(R.id.RequestList);
 
int childCount = requesttable.getChildCount();
 
// Remove all rows except the first one
if (childCount > 1) {
requesttable.removeViews(1, childCount - 1);
}
}
 
public View.OnClickListener productOnClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(RequestListActivity.this, "product sorted", Toast.LENGTH_SHORT).show();
ImageView product = (ImageView) findViewById(R.id.imageProduct);
if (!productColorFlag) {
product.setColorFilter(null);
productColorFlag = true;
DeleateRequestList();
AddRequestList(requestList);
} else {
product.setColorFilter(BLACK);
productColorFlag = false;
DeleateRequestList();
productSort();
}
}
};
 
private void productSort() {
ArrayList<RequestList> requestList1 = (ArrayList<RequestList>) requestList.clone();
 
Collections.sort(requestList1, new Comparator<RequestList>() {
@Override
public int compare(RequestList r1, RequestList r2) {
return r1.getProduct().compareTo((r2.getProduct()));
}
});
 
TableLayout requesttable = (TableLayout)findViewById(R.id.RequestList);
AddRequestList(requestList1);
//
// for (int i = 0; i < requestList1.size(); i++) {
// TableRow tableRow = new TableRow(this);
//
// TextView textProduct = new TextView(this);
// textProduct.setText(requestList1.get(i).getProduct());
// textProduct.setTextSize(20);
// textProduct.setHeight(150);
// textProduct.setGravity(Gravity.CENTER);
// tableRow.addView(textProduct);
//
// TextView textDeadline = new TextView(this);
// textDeadline.setText(requestList1.get(i).getDeadline());
// textDeadline.setTextSize(20);
// textDeadline.setHeight(150);
// textDeadline.setGravity(Gravity.CENTER);
// tableRow.addView(textDeadline);
//
// TextView textLocation = new TextView(this);
// textLocation.setText(requestList1.get(i).getLocation());
// textLocation.setTextSize(20);
// textLocation.setHeight(150);
// textLocation.setGravity(Gravity.CENTER);
// tableRow.addView(textLocation);
//
// requesttable.addView(tableRow);
// }
}
 
public View.OnClickListener deadlineOnClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(RequestListActivity.this, "deadline sorted", Toast.LENGTH_SHORT).show();
ImageView deadline = (ImageView) findViewById(R.id.imageDeadline);
if (!deadlineColorFlag) {
deadline.setColorFilter(null);
deadlineColorFlag = true;
} else {
deadline.setColorFilter(BLACK);
deadlineColorFlag = false;
}
}
};
 
public View.OnClickListener locationOnClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(RequestListActivity.this, "location sorted", Toast.LENGTH_SHORT).show();
ImageView location = (ImageView) findViewById(R.id.imageLocation);
if (!locationColorFlag) {
location.setColorFilter(null);
locationColorFlag = true;
} else {
location.setColorFilter(BLACK);
locationColorFlag = false;
}
}
};
}
View
29
app/src/main/res/layout/app_bar_request_list.xml
app:backgroundTint="@android:color/background_light"
app:maxImageSize="70dp" />
 
<TableLayout
android:id="@+id/RequestList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="70dp">
 
<TableRow
android:id="@+id/tableRow1"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
 
<LinearLayout
android:layout_height="match_parent"
android:orientation="horizontal">
 
<TextView
android:id="@+id/textView"
android:id="@+id/textProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center_horizontal"
android:gravity="center|center_horizontal"
android:text=" 買うもの "
android:textSize="20sp" />
 
<ImageView
android:id="@+id/imageView"
android:id="@+id/imageProduct"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
app:srcCompat="@android:drawable/arrow_down_float" />
</LinearLayout>
 
<LinearLayout
android:id="@+id/deadline"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
 
<TextView
android:id="@+id/textView2"
android:id="@+id/textDeadline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center_horizontal"
android:gravity="center|center_horizontal"
android:text=" 購入期限 "
android:textSize="20sp" />
 
<ImageView
android:id="@+id/imageView2"
android:id="@+id/imageDeadline"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
app:srcCompat="@android:drawable/arrow_down_float" />
</LinearLayout>
 
<LinearLayout
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
 
<TextView
android:id="@+id/textView3"
android:id="@+id/textLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center_horizontal"
android:gravity="center|center_horizontal"
android:text=" 場所 "
android:textSize="20sp" />
 
<ImageView
android:id="@+id/imageView3"
android:id="@+id/imageLocation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
app:srcCompat="@android:drawable/arrow_down_float" />