'안드로이드 리스트뷰'에 해당되는 글 2건

  1. 2012.01.11 안드로이드 CustormList 예제 따라하기
  2. 2012.01.09 안드로이드 ListView, GridView 예제 따라하기
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
JAVA/Android2012.01.09 17:38

- EditText에 글을 치고 추가하면 리스트가 추가되는 간단한 예제이다.
- 해당 리스트 뷰를 클릭하면 Intent로 값을 넘겨서 실행 시켜보자.

-Java 소스
public class Ex07_ListView1Activity extends Activity implements
  OnItemClickListener, OnClickListener {
 /** Called when the activity is first created. */
 ArrayList<String> s;
 ListView lv;
 GridView grid;
 ArrayAdapter<String> adapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);
  s = new ArrayList<String>();
  s.add("itemt0");
  s.add("itemt1");

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

  lv = (ListView) this.findViewById(R.id.listview); //리스트뷰 뿌리기
  lv.setAdapter(adapter);
  lv.setOnItemClickListener(this);

  grid = (GridView) this.findViewById(R.id.grid1); // 그리드뷰 뿌리기
  grid.setAdapter(adapter);
  grid.setOnItemClickListener(this);

  Button bt = (Button) this.findViewById(R.id.button1);
  bt.setOnClickListener(this);

 }

 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  if (arg0.getId() == R.id.button1) {
   EditText et = (EditText) this.findViewById(R.id.edittext1);
   String s11 = et.getText().toString();
   s.add(s11);
   adapter.notifyDataSetChanged();
   // lv.setAdapter(adapter);
  }
 }

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  // TODO Auto-generated method stub
  // Toast.makeText(this, s.get(arg2), Toast.LENGTH_LONG).show();

  Intent intent = new Intent(this, MyPrint.class); // 해당 리스트를 눌렸을때 Intent넘기기
  intent.putExtra("param1", s.get(arg2)); //s.get(arg2)가 눌렸을때의 번호
  startActivity(intent);
 }

}
-------------------------------------------------------------------------------------------------------
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 소스
//main.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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edittext1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="추가하세요" />

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="추가" />
    </LinearLayout>

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

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

        <GridView
            android:id="@+id/grid1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:numColumns="auto_fit" >
        </GridView>
    </LinearLayout>

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

-실행화면

 

신고
Posted by Namseungil

티스토리 툴바