'ListView 삭제하기'에 해당되는 글 1건

  1. 2012.01.11 안드로이드 CustormList 예제 따라하기
JAVA/Android2012.01.11 13:10
- BaseAdapter를 활용하여 ListView에 디자인을 입혀서 ListVIew를 삭제 해보자.그리고 ListView를 내마음대로 꾸며보자.

 -Java 소스
public class Ex08_CustormListActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ArrayList<MyItem> arrayItems = new ArrayList<MyItem>();
arrayItems.add(new MyItem(R.drawable.icon, "item4", "item22"));
arrayItems.add(new MyItem(R.drawable.icon, "item5", "item33"));

CustomAdater adapter = new CustomAdater(this, R.layout.itemlayout, //내가만든 리스트뷰 디자인
arrayItems); // 

ListView list = (ListView) this.findViewById(R.id.listview);
list.setAdapter(adapter);

}

------------------------------------------------------------------------------------------------------- 
public class CustomAdater extends BaseAdapter {
Context context;
int itemlayout;
ArrayList<MyItem> arrayItems;
LayoutInflater inflater;

public CustomAdater(Context c, int sublayout, ArrayList<MyItem> list) {
this.context = c;
this.itemlayout = sublayout;
this.arrayItems = list;

inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayItems.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayItems.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int pos = position;
if (convertView == null) {
convertView = inflater.inflate(itemlayout, parent, false);
}

ImageView img = (ImageView) convertView.findViewById(R.id.itemimage1);
// img.setImageResource(arrayItems.get(position).image);
img.setImageResource(arrayItems.get(position).image);

TextView text = (TextView) convertView.findViewById(R.id.itemtext1);
text.setText(arrayItems.get(position).text);
text.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, Integer.toString(pos),
Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, MyPrint.class);
intent.putExtra("param1", Integer.toString(pos));
context.startActivity(intent);
}
});

TextView text1 = (TextView) convertView.findViewById(R.id.itemtext2);
text1.setText(arrayItems.get(position).text2);

Button btn = (Button) convertView.findViewById(R.id.itembutton1);
btn.setText("Delete");
btn.setOnClickListener(new Button.OnClickListener() { //리스트삭제하기

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
arrayItems.remove(pos); 
notifyDataSetChanged();
}

});

return convertView;
}

}
------------------------------------------------------------------------------------------------------
public class MyItem {
int image;
String text;
String text2;
public MyItem(int img, String txt, String txt2){
image = img;
text = txt;
text2 = txt2;
}
}
------------------------------------------------------------------------------------------------------- 
public class MyPrint extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myprint);
Intent intent = getIntent();
String s = intent.getStringExtra("param1");
TextView text = (TextView)this.findViewById(R.id.text1print);
text.setText(s);
}

}

-xml 소스
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>
------------------------------------------------------------------------------------------------------- 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/itemimage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/itemtext1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0" />

        <TextView
            android:id="@+id/itemtext2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0" />
    </LinearLayout>

    <Button
        android:id="@+id/itembutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0" />

</LinearLayout> 
-------------------------------------------------------------------------------------------------------<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1print"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

</LinearLayout> 

- 실행화면
 

신고
Posted by Namseungil

티스토리 툴바