Their are several occations when you want to list something in your app, for example you the contacts in your android phone. In this tutorial you're gonna learn how to create a simple listview using ArrayAdapter.
Copy the below code to your activity class:
Java:
XML:
simplerow.xml
main_activity.xml
That's all, now run your app!
Copy the below code to your activity class:
Java:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Accounts
extends Activity {
private static String DBNAME = "ROLLING.db";
String itemValue;
private ArrayAdapter<String> listAdapter;
private ListView mainListView;
SQLiteDatabase mydb;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main_activity);
mainListView = (ListView)findViewById(R.id.list);
Object[] arrobject = new String[]{};
ArrayList arrayList = new ArrayList();
arrayList.addAll(Arrays.asList(arrobject));
listAdapter = new ArrayAdapter(this, R.layout.simplerow, arrayList);
listAdapter.add("Item 1 ");
listAdapter.add(Item 2);
listAdapter.add(Item 3);
listAdapter.add(Item 4);
listAdapter.add(Item 5);
mainListView.setAdapter(listAdapter);
mainListView.setOnItemClickListener((AdapterView.OnItemClickListener)new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> adapterView, View view, int n, long l) {
itemValue = (String)mainListView.getItemAtPosition(n);
log.d("you clicked on ===", itemValue);
}
});
}
XML:
simplerow.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10.0dip"
android:textSize="16.0sp" />
main_activity.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" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
That's all, now run your app!
No comments:
Post a Comment