i want to do something that when i click on button he will openfor me something like listview with the name of all the contacts in my phone...how can i do it?
i know how to get the names of all the contacts in my phone and put them into string array,however how can i open a new windows with a listview of all the contacts name when i click on button?
thanks
In your first Activity when you click on button:
startActivity(new Intent(this, ContactsActivity.class));
Then in your ContactsActivity:
public class ContactsActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.contacts_view);
ListAdapter adapter = createAdapter();
setListAdapter(adapter);
}
/**
* Creates and returns a list adapter for the current list activity
* #return
*/
protected ListAdapter createAdapter()
{
// List with strings of contacts name
contactList = ... someMethod to get your list ...
// Create a simple array adapter (of type string) with the test values
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactList);
return adapter;
}
}
XML file for your ContactsActivity (name it to contacts_view.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="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
</LinearLayout>
Can use a listActivity
listActivity on android developer
On the button click you need to start new Activity(or ListActivity) that contains list of all contacts.So you need to set onClicklistener then in onClick function you need to write code for starting Activity
On second activity Create a CustomAdapter that will initialised with say List
class Contact
{
private String number;
private String name
}
Thanks.
Related
I have a listview class.listview opening and a user can click any listview item. I wanna set the text on a xml. I have strings like st1-st2-st3. If I clicked any listview item, how can I get it to open the xml and set Textview1 for st1/#String ?
my listview working like this ;
public class strateji extends Activity {
private static final String LOG_TAG = "Alert Dialog Activity";
private ListView lv1;
private TextView status;
private String lv_arr[]={"listview1-list2-list3-list4-list5-list6-etc..."};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ana);
// Look up the AdView as a resource and load a request.
Log.i(LOG_TAG, "Alert Dialog Activity Started");
lv1=(ListView)findViewById(R.id.listView1);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
setContentView(R.layout.texts); //--->opening texts.xml and setText
status.setText(String);--------->>> like this i want send a string for TextView
}
if(position == 1)
{
Intent myIntent = new Intent(strateji.this, mat2.class);
startActivityForResult(myIntent, 0);
}
}
}
Ok this is getting a bit confusing so let me just do this.
If you have a layout like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:packpurchase="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17sp"
android:padding="15dp" />
<View
android:id="#+id/divider_one"
android:layout_width="match_parent"
android:layout_height="1dp" />
<ListView
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="#drawable/list_divider_inset"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
In your activity, create the variables in order to have a reference to your views and adapter:
private ListView lv;
private ArrayAdapter<String> mAdapter;
private TextView tv;
And then in onCreate() once you are done calling super.onCreate() and setContentView(), set your adapter to your ListView while getting your references
lv = (ListView) findViewById(android.R.id.list);
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
lv1.setAdapter(mAdapter);
tv = (TextView) findViewById(R.id.your_textview_id);
lv1.setOnItemClickListener(new OnitemClickListener(){
#Override
public void onItemClicked(...){
// GET YOUR STRING
String mString = mAdapter.getItem(position);
// APPLY YOUR STRING TO YOUR TEXT_VIEW
tv.setText(mString);
}
};
Again this is poorly explained and I am not sure if it is correct to what you need. It is just to get you started (or at least to help us understand better what you need).
Note that you don't have to keep a reference to your adapter as you can just call
lv1.getAdapter().getItem(position);
I just keep a reference to it and often see a reference to the adapter kept. Same with TextView tv. You don't have to keep a reference, but you can and once you have that reference you can do null checks to avoid doing to many findViewById() calls which can get heavy.
If I understand you correctly, you want a new activity to open and a textview in that activity to display text thats dependent on what list view item they clicked?
you would add an intent extra into your intent that you are using to launch the new activity and put the string you want displayed into it, and then in the new activity get that extra and then set the text view to it.
Intent i = new Intent(activity1, activity2);
i.putExtra(stringkey, stringvalue);
then in your oncreate of the activity you call
String str = getIntent.getStringExtra(stringkey);
then just set the textview to that string
Hi I have created a simple application for displaying contents from database to list view, but my list view is not displaying any data I am a beginner and I need some assistance.
Giving my list view class below
public class Show extends Activity {
//SQLiteDatabase db;
//Datahelper dh;
Context context=this;
Dataclass dc;
private ListView mainListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
mainListView=(ListView)findViewById(R.id.list);
//getDetails();
dc=new Dataclass(this);
Bundle bundle=getIntent().getExtras();
String message=bundle.getString("MSG");
List<String> friendlist=dc.getDetails(message);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1,friendlist);
mainListView.setAdapter(adapter);
}
public void onDestroy() {
super.onDestroy();
dc.close();
}
}
I have passed data from one activity to this activity using bundle and I have got the data , but no data is retrieved from the database..
giving m y getdetails function from dataclass below
public List<String> getDetails(String message) {
List<String> Friendlist = new ArrayList<String>();
String selectQuery="SELECT * FROM Mytables WHERE Name='" + message + "' ";
db = dh.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
while(cursor.moveToNext()) {
Friendlist.add(cursor.getString(1));
}
return Friendlist;
}
giving my xml layout for list activity below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
try set listview's background color to red, see it is NO-CONTENT or DIDN'T DISPLAY.
From your layout I can see that you don't have an ListView element with id "list" mainListView=(ListView)findViewById(R.id.list);. Since all the operations you are running assume the existence of this element, probably this is the reason why nothing appears on the screen.
I also suggest that instead of inheriting from Activity you should inherit from ListActivity and use existing methods to deal with ListView like getListView(), setListAdapter(adapter) and keep your ListView with the default id android:id="#android:id/list".
I am designing a simple app from tutorials that I have seen, and I am attempting to simply display two text views in each row of a listview, a name and a price. This worked and I could select the row and it would activate an intent. However I then changed my xml code so that the listview would be placed in a linearLayout in order for me to have a header at the top of the screen. Now when I click on any of the rows they are highlighted but nothing else happens. I have already tried to making the textviews set to clickable = false in the xml but still no luck. I am hoping I am just missing something simple in the onCreate method. `public class ViewMenuListing extends ListActivity {
public static final String ROW_ID = "row_id"; // Intent extra key
private ListView contactListView; // the ListActivity's ListView
private CursorAdapter contactAdapter; // adapter for ListView
private String tableName;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // get the built-in ListView
contactListView.setOnItemClickListener(viewContactListener);
setContentView(R.layout.viewmenu);
//Get table name of menu clicked.
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[] { "name", "price" };
int[] to = new int[] { R.id.itemTextView, R.id.priceTextView };
//int[] to = new int[] { R.id.itemTextView};
contactAdapter = new SimpleCursorAdapter(
ViewMenuListing.this, R.layout.menu_list_item, null, from, to);
setListAdapter(contactAdapter); // set contactView's adapter
}`
The only thing I changed in this code was that I used the setContentView(R.layout.viewmenu) now when I didnt before and the list would just be the content view.
Here is my viewMenu file:
`
<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="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>`
and my menu_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemTextView"
android:padding="8dp"
android:clickable = "false"
android:textSize="20sp" android:textColor="#android:color/white"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/priceTextView"
android:clickable = "false"
android:textColor="#android:color/white"/>
</LinearLayout>
Thank you for your help!
Looks like you are explicitly setting an onItemClickListener for your ListView. This really isn't necessary since you are extending ListActivity and ListActivity has a method you can override called onListItemClick(). I would override the onListItemClick() method instead of explicitly setting an onItemClickListener. http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView, android.view.View, int, long)
Please correct following things
contactListView = getListView(); and setListAdapter(contactAdapter);
The above syntax can be only used if your class is extending ListActivity .In your case you are extending Activity,So above Approach will not work.
Your menu_list_item.xml looks perfectly fine to me.
in viewmenu.xml file make following correction while specifying id for listview.
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Please see to this corrected code.
public class SampleActivity extends ListActivity
{
public static final String ROW_ID = "row_id"; // Intent extra key
private ListView contactListView; // the ListActivity's ListView
private CursorAdapter contactAdapter; // adapter for ListView
private String tableName;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
contactListView = (ListView) findViewById(R.id.list); // get the
// built-in
// ListView
contactListView.setOnItemClickListener(this);
setContentView(R.layout.viewmenu);
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[]
{ "name", "price" };
int[] to = new int[]
{ R.id.itemTextView, R.id.priceTextView };
contactAdapter = new SimpleCursorAdapter(SampleActivity.this,
R.layout.menu_list_item, null, from, to);
contactListView.setAdapter(contactAdapter);
}
#Override
public void onListItemClick(AdapterView<?> adapterView, View view,
int position, long arg3)
{
}
}
Now in onListItemClick method you can write whatever logic you want.
Hope this help.
I am creating an application that saves GPS coordinates; I have created a button which launches a new Activity to display, as a list all the save coordinates. The program crashes a soon as I hit the button, here is what I am doing.
class 1:
public void openLatLonData(View view)
{
Intent intent = new Intent(this, GpsDataList.class);
ArrayList<String> allLocalStrings = new ArrayList<String>();
for(int i =0; i< AllLocals.size();i++)
{
allLocalStrings.add(AllLocals.get(i).getLat());
allLocalStrings.add(AllLocals.get(i).getLon());
}
intent.putStringArrayListExtra("data", allLocalStrings);
startActivity(intent);
}
This method calls the new activity:
public class GpsDataList extends ListActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle bundleIn)
{
super.onCreate(bundleIn);
Intent intent = getIntent();
ArrayList<String> listData = new ArrayList<String>();
listData = intent.getStringArrayListExtra("data");
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listData));
getListView().setTextFilterEnabled(true);
//finish();
}
}
the button contains the following XML:
<Button android:layout_height="wrap_content"
android:text="Show Collected Data"
android:id="#+id/view_lat_lon_data"
android:layout_width="wrap_content"
android:onClick="openLatLonData">
</Button>
And I have added a new .xml file named gpsdatalist.xml under the res/layout folder:
<?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">
</LinearLayout>
Lastly the manifest contains this: .
I believe I have all the pieces in play- Hopefully someones see the mistake
Thanks in advance guys!
Did you define the activity in the AndroidManifest.xml file?
Anyway, your best clue resides in the logcat of your exception. You will solve it in 2 seconds after cheking the logcat.
I am creating an android application in which there are two activites. One activity contains a button. On the button click, i want to switch to other activity which displays a list view containing few options.
Two switch between the screens or activities , i am using the following code
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);
Since my Activity2 class extends the 'ListActivity', this code doesn't seem to work. On my button click, i want to display a list view containing some data.
Any help would be appreciated
#Siddharth
i seem to be doing almost the same thing
Here is my actual code
From Activity 1
public void onClick(View v) {
Intent intent = new Intent(View_Data.this,CategoryList.class);
startActivity(intent);
}
In Activity 2
public class CategoryList extends ListActivity {
public TextView selection;
public String[] categories;
ArrayList<String> type_of_category;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list);
getCategories();
}
public void getCategories() {
DBHelper dbhelper = new DBHelper(this);
type_of_category = new ArrayList<String>();
type_of_category = dbhelper.getTypesOfQuotes();
String[] items = new String[100];
for(int i=0;i<type_of_type_of_category.size();i++)
{
items[i] = type_of_type_of_category.get(i);
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}
}
Here is my XML File
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
In My 2nd Activity, i get the error in this line
setContentView(R.layout.category_list);
Depending on whether your data in the second activity is from a database or static data, this code should work for you. I am assuming from your post that you dont need to send data from the 1st activity to the 2nd activity. This is actual code from my application which is database driven. If you are not using a database, parts of this code can be changed to use that instead of a database. It should get you started:
From the 1st Activity:
public void onClickListContacts(View target)
{
Intent listContacts = new Intent(this, com.dzinesunlimited.quotetogo.ContactsList.class);
startActivity(listContacts);
}
The 2nd activity:
public class ContactsList extends ListActivity {
DBAdapter dbContactList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
dbContactList = new DBAdapter(this);
// Calls the method to display the ListView of all Contacts
fillData();
}
private void fillData() {
// Pull the data from the database
String[] fields = new String[] { dbContactList.TABLE_CON_NAME };
int[] views = new int[] { android.R.id.text1 };
Cursor c = dbContactList.getAllContacts();
startManagingCursor(c);
// Set the ListView
ListAdapter prjName = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, fields, views);
setListAdapter(prjName);
dbContactList.close();
}
For a static list, you can also refer to this tutorial: http://www.vogella.de/articles/Android/article.html#lists
Hope this helps.
PS: It would be great help if you could post code of the 2nd activity which has problems.
Add this to your XML and see if that helps:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>