회사는 정말 싫어욧

private List<Food> parseXMLJsoup() {
List<Food> foods = new ArrayList<>();

try {
InputStream is = getAssets().open("food.xml");
Document document = Jsoup.parse(is,"UTF-8","http://example.com");
Elements element = document.select("food");
for (Element e : element) {
Food food = new Food();
food.setName(e.select("name").get(0).text());
food.setDescription(e.select("description").get(0).text());
food.setPrice(e.select("price").get(0).text());
food.setCalories(e.select("calories").get(0).text());
foods.add(food);
}
} catch (IOException e) {
e.printStackTrace();
}
return foods;
}

food.xml을 못찾으면 http://example.com으로 간다함

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
public class Food {
private String name;
private String price;
private String description;
private String calories; //DTO


public class MainActivity extends AppCompatActivity {
List<Food> foodList;
List<String> list = new ArrayList<>();
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = findViewById(R.id.listView);

foodList = parseXML();

for (Food food : foodList) {
list.add(food.getName() + "(" + food.getPrice() + ")");
}

ArrayAdapter<String > adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),foodList.get(position).toString(),Toast.LENGTH_SHORT).show();
}
});
}

private List<Food> parseXML() {
List<Food> foods = new ArrayList<>();
boolean isName = false, isDesc = false, isCalories = false, isPrice = false;
String name, desc, price, calories, tagName;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(getResources().openRawResource(R.raw.food), "UTF-8");

Food food = null;
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
tagName = parser.getName();
switch (tagName) {
case "food":
food = new Food();
break;
case "name" :
isName = true;
break;
case "description" :
isDesc = true;
break;
case "price" :
isPrice = true;
break;
case "calories" :
isCalories = true;
break;
}
break;
case XmlPullParser.END_TAG:
tagName = parser.getName();
switch (tagName) {
case "food":
foods.add(food);
break;
case "name" :
isName = false;
break;
case "description" :
isDesc = false;
break;
case "price" :
isPrice = false;
break;
case "calories" :
isCalories = false;
break;
}
break;
case XmlPullParser.TEXT:
if (isName) food.setName(parser.getText());
if (isDesc) food.setDescription(parser.getText());
if (isPrice) food.setPrice(parser.getText());
if (isCalories) food.setCalories(parser.getText());
break;
}
eventType = parser.next();
}

} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Log.d("읽은 내용" , foods.toString());
return foods;
}
}


public void start1(View view) {
Button button = (Button) view;

button.setEnabled(false);
}


버튼에 메소드 만들경우 매개변수 view로 버튼 사용가능


Save {

String text = editText.getText().toString();

editText.setText("");

try {

OutputStream os = openFileOutput("data.txt",MODE_PRIVATE);

PrintWriter pw = new PrintWriter(os);

pw.append(text + "\n");

pw.flush();

pw.close();

os.close();


} catch (Exception e) {

}



Load{

try {

InputStream is = openFileInput("data.txt");

Scanner sc = new Scanner(is,"UTF-8");

while (sc.hasNextLine()) {

textView.append(sc.nextLine() +"\n");


}

} catch (Exception e) {

}

private Context context;
private List<데이터타입> flagList;

생성자 만들기


오버라이딩 된 메소드에 내용 채우기


@Override
public int getCount() {
return flagList.size();
}

@Override
public Object getItem(int position) {
return flagList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Flag data = flagList.get(position);
FlagView flagView = null;

if (convertView == null) {
flagView = new FlagView(context,data);
}
else {
flagView = (FlagView) convertView;
flagView.setFlag(data);
}
return flagView;
}


http://websystique.com/spring-security/spring-security-4-hello-world-annotation-xml-example/ 참고




web.xml


<!-- Security 필터 -->

<filter>

<filter-name>springSecurityFilterChain</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>


<filter-mapping>

<filter-name>springSecurityFilterChain</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 인코딩 필터만 추가!!! -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>



root-context (namespace security 체크)


<security:http auto-config="true" >

        <security:intercept-url pattern="/" access="permitAll" />

        <security:intercept-url pattern="/home" access="permitAll" />

        <security:intercept-url pattern="/admin**" access="hasRole('ADMIN')" />

        <security:intercept-url pattern="/dba**" access="hasRole('ADMIN') or hasRole('DBA')" />

        <security:form-login  authentication-failure-url="/Access_Denied" />

    </security:http>

  

    <security:authentication-manager >

        <security:authentication-provider>

            <security:user-service>

                <security:user name="bill"  password="abc123"  authorities="ROLE_USER" />

                <security:user name="admin" password="root123" authorities="ROLE_ADMIN" />

                <security:user name="dba"   password="root123" authorities="ROLE_ADMIN,ROLE_DBA" />

            </security:user-service>

        </security:authentication-provider>

    </security:authentication-manager>


pom.xml

<properties>

<org.springframework-version>4.1.6.RELEASE</org.springframework-version>

<springsecurity-version>4.0.1.RELEASE</springsecurity-version>

</properties>


<dependency>

            <groupId>org.springframework.security</groupId>

            <artifactId>spring-security-web</artifactId>

            <version>${springsecurity-version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.security</groupId>

            <artifactId>spring-security-config</artifactId>

            <version>${springsecurity-version}</version>

        </dependency>



InputStream is = getResources().openRawResource(R.raw.flag_names);
Scanner sc = new Scanner(is,"UTF-8");
int i = 0;

while (sc.hasNextLine()) {
Item item = new Item();
item.setImageID(R.drawable.flag_afghanistan + i);
item.setFlagName(sc.nextLine());
list.add(item);
i++;
}


Scanner와 Scanner의 nextLine, hasNextLine을 이용


그냥 gson쓰자

Dependencies에서 Library 검색으로 RecyclerView 추가


ListView보다 메모리 적게 잡아먹는다고 함 :)


Adapder 만들 때는 RecyclerView.Adapter<커스텀 뷰 홀더> 를 상속받는다


public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemViewHolder>{

class ItemViewHolder extends RecyclerView.ViewHolder {

public ItemViewHolder(View itemView) {
super(itemView);
}
}
}

먼저 RecyclerAdapter에 extends RecyclerView.Adapder<>를 작성하고


내부에 class ItemViewHolder extends RecyclerView.ViewHolder 작성


ItemViewHolder에 Alt+Enter로 생성자 추가(안드로이드 스튜디오에서 사용)


위에 비어있는 <>안에 어뎁터클래스이름.ItemViewHolder으로 작성 후 Alt+Enter로 메소드 오버라이딩



이때 만든 ItemViewHolder 클래스가 보여질 뷰에 대한 내용이다

class ItemViewHolder extends RecyclerView.ViewHolder {

TextView textView;


public ItemViewHolder(View itemView) {
super(itemView);

textView = itemView.findViewById(R.id.tv);
}
}

recyclerView가 사용할 layout에 있는 사용할 변수를 모두 선언하여 ItemViewHolder 작성


@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item,parent,false);

return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
DataDTO data = list.get(position);
String msg = data.getH() + "(" + data.getK() + "):"+data.getT();
holder.textView.setText(msg);
}

오버라이딩된 메소드에 내용 작성




---------------------------------------



Item = int ImageId, String flagName을 가지고 있음

item.xml = ImageView (id:iv), TextView (id:tv)를 가지고 있음


public class RecyclerAdapter
extends RecyclerView.Adapter<RecyclerAdapter.ItemViewHolder>{
List<Item> list;

public RecyclerAdapter(List<Item> list) {
this.list = list;
}

@NonNull
@Override
public ItemViewHolder onCreateViewHolder
(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item,parent,false);

return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder
(@NonNull ItemViewHolder holder, int position) {

Item data = list.get(position);
holder.imageView.setImageResource(data.getImageID());
holder.textView.setText(data.getFlagName());
}

@Override
public int getItemCount() {
return list.size();
}

class ItemViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;

public ItemViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.iv);
textView = itemView.findViewById(R.id.tv);
}
}
}

주황색으로 된 부분은 만든 클래스나 만든 Layout XML 파일을 참고하여 작성하면 된다

메소드는 상속(extends)가 끝난 후 안드로이드 스튜디오의 Alt+Enter를 이용하여 메소드 오버라이딩





Adapter에 notifysetchanged 하여 데이터 추가되었음을 View가 확인할 수 있게 해야함


안그러면 데이터 추가한걸로 리스트가 안돌아감

복붙해서 만든 DTO로만 몇군데 수정해주면 된다 (오라클 DB일 경우 사용)


public class Paging {

private List<BoardDTO> list;

private int totalCount;

private int pageSize;

private int currentPage;

private int totalPage;

private int startNo;

private int endNo;

private int startPage;

private int endPage;

public Paging(int totalCount, int pageSize, int currentPage) {

this.totalCount = totalCount;

this.pageSize = pageSize;

this.currentPage = currentPage;

calc();

}


private void calc() {

if(this.totalCount>0){

this.totalPage = (this.totalCount-1)/this.pageSize + 1;

if(this.currentPage<1 || this.currentPage>this.totalPage)

this.currentPage = 1;

this.startNo = (this.currentPage-1) * this.pageSize + 1;

this.endNo = this.startNo + this.pageSize -1;

if(this.endNo>this.totalCount) this.endNo = this.totalCount;

this.startPage = (this.currentPage-1)/this.pageSize * this.pageSize+1;

this.endPage = this.startPage+this.pageSize - 1;

if(this.endPage>this.totalPage) this.endPage=this.totalPage;

}else{

totalCount=0;

}

}


public List<BoardDTO> getList() {

return list;

}


public void setList(List<BoardDTO> list) {

this.list = list;

}


public int getTotalCount() {

return totalCount;

}


public void setTotalCount(int totalCount) {

this.totalCount = totalCount;

calc();

}


public int getPageSize() {

return pageSize;

}


public void setPageSize(int pageSize) {

this.pageSize = pageSize;

calc();

}


public int getCurrentPage() {

return currentPage;

}


public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

calc();

}


public int getTotalPage() {

return totalPage;

}


public int getStartNo() {

return startNo;

}


public int getEndNo() {

return endNo;

}


public int getStartPage() {

return startPage;

}


public int getEndPage() {

return endPage;

}

}