결과화면 

 

package com.example.containerdemo;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewDemo3 extends Activity {
 Button add, delete;
 ListView listview;
 EditText item_name;
 ArrayAdapter<String> adapter;
 int deletePos = -1;;
 TextView message;
 ArrayList<String> alist;

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

  add = (Button)findViewById(R.id.add);
  message = (TextView)findViewById(R.id.message);
  delete = (Button)findViewById(R.id.delete);
  listview = (ListView)findViewById(R.id.listview1);
  item_name = (EditText)findViewById(R.id.item_name);

  alist = new ArrayList<String>();
  alist.addAll(Arrays.asList(getResources().getStringArray(R.array.phone)));

  adapter = new ArrayAdapter<String>
  (this, android.R.layout.simple_list_item_1, alist);

  listview.setAdapter(adapter);

  add.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    alist.add(0, item_name.getText().toString());
    adapter.notifyDataSetChanged();
   }
  });

  delete.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if(deletePos == -1){
     message.setText("삭제할 Item(항목)을 선택하세요.");
    } else {
     alist.remove(deletePos);
     adapter.notifyDataSetChanged();
     message.setText("");
    }
   }
  });

  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    deletePos = position;
    message.setText(alist.get(position) + "를 삭제하시겠습니까?");
   }
  });
 }
}

 

 

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ListView에 추가할 항목 : " />

    <EditText
        android:id="@+id/item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="추가" />

    <Button
        android:id="@+id/delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="삭제" />

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

 

 

 

//values폴더에 arrays.xml 생성

 

 

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="phone">
        <item>Iphone4</item>
        <item>Iphone4s</item>
        <item>Iphone5</item>
        <item>Iphone5s</item>
        <item>Iphone5c</item>
        <item>galaxy S</item>
        <item>galaxy S2</item>
        <item>galaxy S2 HD</item>
        <item>galaxy S3 3G</item>
        <item>galaxy S3 LTE</item>
        <item>galaxy S4</item>
        <item>galaxy S4 LTE-A</item>
        <item>vega R3</item>
        <item>vega IRON</item>
        <item>vega LTE-A</item>
        <item>optimus G</item>
        <item>optimus G PRO</item>
        <item>optimus GK</item>
        <item>G2</item>
    </string-array>
</resources>

 

'2020년도 이전 > [WebSig] Android' 카테고리의 다른 글

ExpandableList ViewDemo  (0) 2013.08.14
ListView Demo5  (0) 2013.08.14
ListView Demo2 - extends ListActivity 이용...  (0) 2013.08.14
ListView Demo1  (0) 2013.08.14
HScrollView Demo  (0) 2013.08.14

+ Recent posts