Skip to content

Instantly share code, notes, and snippets.

@KalCornes
Last active February 21, 2017 00:46
Show Gist options
  • Save KalCornes/5f5d2f627a147f37c65bec14aa8c7516 to your computer and use it in GitHub Desktop.
Save KalCornes/5f5d2f627a147f37c65bec14aa8c7516 to your computer and use it in GitHub Desktop.
리스트뷰 저장 삭제기능 예시
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="날짜"
android:textSize="30sp"
android:textColor="#F8A2A2"
android:layout_marginRight="5dp"
android:layout_gravity="center" />
<EditText
android:id="@+id/editdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="품목"
android:textSize="30sp"
android:textColor="#F8A2A2"
android:layout_marginRight="5dp"
android:layout_gravity="center"/>
<EditText
android:id="@+id/editproduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입고량"
android:textSize="30sp"
android:textColor="#F8A2A2"
android:layout_marginRight="5dp"
android:layout_gravity="center" />
<EditText
android:id="@+id/editinproduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="출고량"
android:textSize="30sp"
android:textColor="#F8A2A2"
android:layout_marginRight="5dp" />
<EditText
android:id="@+id/editoutproduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="재고량"
android:textSize="30sp"
android:textColor="#F8A2A2"
android:layout_marginRight="5dp"
android:layout_gravity="center"/>
<EditText
android:id="@+id/editsaveproduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<Button
android:id="@+id/inputdata"
android:layout_width="120dp"
android:layout_height="40dp"
android:text="입력"
android:textColor="#ffffff"
android:textSize="25sp"
android:background="@drawable/pinkbtn"
android:layout_weight="0.5"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/canceldata"
android:layout_width="120dp"
android:layout_height="40dp"
android:text="취소"
android:textColor="#ffffff"
android:textSize="25sp"
android:background="@drawable/pinkbtn"
android:layout_weight="0.5"/>
</LinearLayout>
</LinearLayout>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.lee.sheetdata.R;
/**
* Created by Administrator on 2017-02-17.
*/
public class InputActivity extends Activity {
Button inputdata;
Button canceldata;
EditText editdate;
EditText editproduct;
EditText editinproduct;
EditText editoutproduct;
EditText editsaveproduct;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_input);
editdate = (EditText)findViewById(R.id.editdate);
editproduct = (EditText)findViewById(R.id.editproduct);
editinproduct = (EditText)findViewById(R.id.editinproduct);
editoutproduct = (EditText)findViewById(R.id.editoutproduct);
editsaveproduct = (EditText)findViewById(R.id.editsaveproduct);
inputdata = (Button) findViewById(R.id.inputdata);
inputdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( editdate.getText().toString().length() == 0 ) {
Toast.makeText(InputActivity.this, "데이터를 입력하세요.", Toast.LENGTH_SHORT).show();
} else {
Intent intentdata = new Intent(InputActivity.this, TitleActivity.class);
intentdata.putExtra("datedata",editdate.getText().toString());
intentdata.putExtra("productdata",editproduct.getText().toString());
intentdata.putExtra("inproductdata",editinproduct.getText().toString());
intentdata.putExtra("outproductdata",editoutproduct.getText().toString());
intentdata.putExtra("saveproductdata",editsaveproduct.getText().toString());
setResult(RESULT_OK, intentdata);
finish();
}
}
});
canceldata = (Button)findViewById(R.id.canceldata);
canceldata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
import java.text.Collator;
import java.util.Comparator;
public class ListData {
public String mDate;
public String mProduct;
public String mInProduct;
public String mOutProduct;
public String mSaveProduct;
public static final Comparator<ListData> ALPhA_COMPARATOR = new Comparator<ListData>() {
private final Collator sCollator = Collator.getInstance();
@Override
public int compare(ListData mListdate_1, ListData mListdate_2) {
return sCollator.compare(mListdate_1.mDate, mListdate_2.mDate);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/box">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/box">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.5">
<TextView
android:text="날짜 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.5">
<TextView
android:text="품명 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/box">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.3">
<TextView
android:text="입고량 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/inproduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.3">
<TextView
android:text="출고량 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/outproduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.3">
<TextView
android:text="재고량 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/saveproduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFEBEB">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#F8A2A2"/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/backbtn"
android:id="@+id/backbtn"
android:layout_marginStart="18dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="#FFDADA" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/midlecate"
style="@style/customspiner"
android:entries="@array/bohoon"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/titlelist"
android:dividerHeight="4dp"
android:divider="#00000000"
android:layout_margin="5dp"
android:choiceMode="singleChoice"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<Button
android:id="@+id/input"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="입력"
android:textColor="#ffffff"
android:textSize="25sp"
android:background="@drawable/pinkbtn"
android:layout_weight="0.5"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/download"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="다운로드"
android:textColor="#ffffff"
android:textSize="25sp"
android:background="@drawable/pinkbtn"
android:layout_weight="0.5"/>
</LinearLayout>
</LinearLayout>
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.lee.sheetdata.R;
import java.util.ArrayList;
import java.util.Collections;
public class TitleActivity extends AppCompatActivity {
ImageView backbtn;
Button input;
Button download;
TextView datetv;
TextView producttv;
TextView inproducttv;
TextView outproducttv;
TextView saveproducttv;
private ListView mListView = null;
private ListViewAdapter mAdapter = null;
public static final String KEY_MY_PREFERENCE = "my_preference";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submain);
backbtn = (ImageView)findViewById(R.id.backbtn);
backbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
input = (Button)findViewById(R.id.input);
input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TitleActivity.this, InputActivity.class);
startActivityForResult(intent, 0);
}
});
download = (Button)findViewById(R.id.download);
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TitleActivity.this, "text",Toast.LENGTH_SHORT).show();
}
});
Spinner s = (Spinner)findViewById(R.id.midlecate);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("확인");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
mListView = (ListView)findViewById(R.id.titlelist);
mAdapter = new ListViewAdapter(this);
mListView.setAdapter(mAdapter);
datetv = (TextView)findViewById(R.id.date);
producttv = (TextView)findViewById(R.id.product);
inproducttv = (TextView)findViewById(R.id.inproduct);
outproducttv = (TextView)findViewById(R.id.outproduct);
saveproducttv = (TextView)findViewById(R.id.saveproduct);
mListView.setOnItemClickListener(new ListViewItemClickListener());
mListView.setOnItemLongClickListener( new ListViewItemLongClickListener() );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// 수행을 제대로 한 경우
if(resultCode == RESULT_OK && data != null)
{
String date = data.getStringExtra("datedata");
String product = data.getStringExtra("productdata");
String inproduct = data.getStringExtra("inproductdata");
String outproduct = data.getStringExtra("outproductdata");
String saveproduct = data.getStringExtra("saveproductdata");
mAdapter.addItem(date,product,inproduct,outproduct,saveproduct);
mAdapter.dataChange();
mAdapter.sort();
}
// 수행을 제대로 하지 못한 경우
else if(resultCode == RESULT_CANCELED)
{
Toast.makeText(TitleActivity.this,"취소되었습니다.",Toast.LENGTH_SHORT).show();
}
}
int selectedPos = -1;
private class ListViewItemClickListener implements AdapterView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
private class ListViewItemLongClickListener implements AdapterView.OnItemLongClickListener
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
selectedPos = position;
AlertDialog.Builder alertDlg = new AlertDialog.Builder(view.getContext());
alertDlg.setTitle("삭제");
// '예' 버튼이 클릭되면
alertDlg.setPositiveButton( "예", new DialogInterface.OnClickListener()
{
@Override
public void onClick( DialogInterface dialog, int which )
{
mAdapter.remove(selectedPos);
// 아래 method를 호출하지 않을 경우, 삭제된 item이 화면에 계속 보여진다.
mAdapter.notifyDataSetChanged();
mAdapter.sort();
dialog.dismiss(); // AlertDialog를 닫는다.
}
});
// '아니오' 버튼이 클릭되면
alertDlg.setNegativeButton( "아니오", new DialogInterface.OnClickListener()
{
@Override
public void onClick( DialogInterface dialog, int which ) {
dialog.dismiss(); // AlertDialog를 닫는다.
}
});
alertDlg.setMessage("삭제하시겠습니까?");
alertDlg.show();
return true;
}
}
private class ViewHolder{
public TextView mDate;
public TextView mProduct;
public TextView mInProduct;
public TextView mOutProduct;
public TextView mSaveProduct;
}
private class ListViewAdapter extends BaseAdapter{
private Context mContext = null;
private ArrayList<ListData> mListData = new ArrayList<ListData>();
public ListViewAdapter(Context mContext){
super();
this.mContext = mContext;
}
@Override
public int getCount() {
return mListData.size();
}
@Override
public Object getItem(int position) {
return mListData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem, null);
holder.mDate = (TextView) convertView.findViewById(R.id.date);
holder.mProduct = (TextView)convertView.findViewById(R.id.product);
holder.mInProduct = (TextView)convertView.findViewById(R.id.inproduct);
holder.mOutProduct = (TextView)convertView.findViewById(R.id.outproduct);
holder.mSaveProduct = (TextView)convertView.findViewById(R.id.saveproduct);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
ListData mData = mListData.get(position);
holder.mDate.setText(mData.mDate);
holder.mProduct.setText(mData.mProduct);
holder.mInProduct.setText(mData.mInProduct);
holder.mOutProduct.setText(mData.mOutProduct);
holder.mSaveProduct.setText(mData.mSaveProduct);
return convertView;
}
public void addItem(String mDate, String mProduct, String mInProduct, String mOutProduct, String mSaveProduct){
ListData addInfo = null;
addInfo = new ListData();
addInfo.mDate = mDate;
addInfo.mProduct = mProduct;
addInfo.mInProduct = mInProduct;
addInfo.mOutProduct = mOutProduct;
addInfo.mSaveProduct = mSaveProduct;
mListData.add(addInfo);
}
public void remove(int position){
mListData.remove(position);
dataChange();
}
public void sort(){
Collections.sort(mListData, ListData.ALPhA_COMPARATOR);
dataChange();
}
public void dataChange(){
mAdapter.notifyDataSetChanged();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment