2011年7月7日木曜日

ListView の使い方

ListView の使い方
ソース:
//test.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"
    >
<ListView
   android:id="@+id/listview"
  android:layout_width="fill_parent"
   android:layout_height="fill_parent"
/>
</LinearLayout>

//row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
   android:layout_height="wrap_content">

<TextView
android:id="@+id/row_textview1"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:textSize="21sp"
 />

</RelativeLayout>

//Test.class
public class Test extends  Activity {

private ListView listview;
private ArrayList<String> listPath = new ArrayList();
private ArrayAdapter<String> adapter;

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

setContentView(R.layout.test);

listview = (ListView) findViewById(R.id.listview);

adapter = new ArrayAdapter<String>(this, R.layout.row,
R.id.row_textview1, listName);

listview.setAdapter(adapter);

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//do same thing

}
}
        });

}
}

0 件のコメント: