Android: Saving items selected in a multiple choice list view - android

I am trying to understand where to save the selected list view items from this code. With a dialog box you have an "ok/cancel" button option, is this possible with lists? Ideally I will be storing data from four different lists into a database on submit. In the below picture I would like to save the first three items into a database, or even an array just to start.
package com.example.lifebyfourlists;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity{
String [] seven = {
"Dark Leafy Greens" ,
"Nuts",
"Carrots",
"Green Tea",
"Whole Grains",
"Fruits"};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lstView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, seven));
}
public void onListItemClick(ListView parent, View v, int position, long id){
Toast.makeText(this, "You have selected " + seven[position], Toast.LENGTH_SHORT).show();
}
}

I am trying to understand where to save the selected list view items from this code. With a dialog box you have an "ok/cancel" button option, is this possible with lists?
You can use getCheckedItemIds() or you can write a custom Adapter to track the selected rows.

Related

How to populate Contacts from Phone to Custom Listview?

I created a Custom ListView using BaseAdapter now I want to populate this Listview with Contact Name and Number from Phone book..I am trying this..
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Rabtaye extends Activity {
ListView msgList;
ArrayList<MessageDetails> details;
AdapterView.AdapterContextMenuInfo info;
Cursor cursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
msgList = (ListView) findViewById(R.id.MessageList);
details = new ArrayList<MessageDetails>();
MessageDetails Detail = new MessageDetails();
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
startManagingCursor(cursor);
String info[] = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID };
for(String a:info){
Detail.setName(a);
}
Detail.setNumber("0313");
details.add(Detail);
// int to[] = { R.id.name, R.id.number };
// ListAdapter cursada = new SimpleCursorAdapter(this,
// android.R.layout.simple_expandable_list_item_2, cursor, info,
// to);
msgList.setAdapter(new CustomAdapter(details, this));
msgList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView s = (TextView) arg1.findViewById(R.id.name);
String abc = s.getText().toString();
Toast.makeText(Rabtaye.this, abc, Toast.LENGTH_LONG).show();
}
});
}
}
I used different ways, but I am not able to populate ListView with Contact Name or Number. In this code I have to statically add name and number...I am quite confuse here..little help would be greatly appreciated. Thanks in Advance. I am newbie so please go easy on me..:)
Hmmm. You're on the right track, but I think you're wandering a bit.
A ListView is a view object, not a BaseAdapter. As far as I can tell from your code, you don't need a custom adapter for your ListView.
What you should do is bind a regular CursorAdapter to your ListView, load the Contacts data you want from the Contacts Provider using a CursorLoader, then move the resulting Cursor to the CursorAdapter.
You can find the instructions for doing this in this Android training class:
Loading Data in the Background.
What do you want in your custom ListView that you can't get from ListView itself?
If you're putting more than just a single list of items into a ListView (it looks like you're doing name, number, etc.) you have to create your own custom adapter. Extend the BaseAdapter class either in your activity, or in a new class. Then you'll have to override the getView() method. This is what is called every time a new item gets inflated.
You pass your ArrayList into the BaseAdapter when you create it. The getView() method of the adapter is where you code your logic to take the data and present it as a list item. You'll have to define a new XML file as a layout for each list item that contains your TextViews and whatnot for your different children in each list item, and that's what you populate with your data.
There are a bunch of examples all over if you search for "baseadapter". Here's one that gives a pretty good intro.

Android ListView with CheckBox can distinguish the click

I am trying to create a ListView with CheckBox's...Th ListView should allow the user to both select an item or open that item to select other choice inside it. In other words, the ListView should be able to distinguish between the click on the checkbox and the click on the item itself.
I tried to implement it using android.R.layout.simple_list_item_multiple_choice but this one allows me to only check the checkbox even if I click outside the checkbox (on the item).
anyone can help?
Here's my code,
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewActivity extends Activity implements OnItemClickListener {
ListView directoryList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> contacts = new ArrayList<String>();
contacts.add("zaid");
contacts.add("hazem");
contacts.add("Oubai");
directoryList= (ListView) findViewById(R.id.directoryList);
final ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
contacts);
directoryList.setAdapter(arrayAdapter);
directoryList.setOnItemClickListener(this);
directoryList.setClickable(true);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
You need a customized array adapter. In your getView(), get a hold of your checkbox and set the OnCheckedChangeListener()
Here is a tutorial on how you can make your own custom adapter. The example has a clickable textbox but you can change it to work for a checkbox.
http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

(Pre)select checkboxes in a ListActivity using 'simple_list_item_checked'

I'm using the ListActivity class in conjunction with the simple_list_item_checked-layout which implements simple list items with checkboxes. Everything is working fine - clicking added items calls onListItemClick() and I can check/uncheck respective checkboxes of entries via the 'View v' parameter.
However what I wasn't able to figure out yet is, how to (pre)select checkboxes without any userinteraction?
Minimal so far working code snippet showing my intend:
package org.test;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
public class TestActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
list_elems.add("foobar");
//TODO: check added entry labeled "foobar"
setListAdapter(list_elems);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
CheckedTextView check = (CheckedTextView)v;
check.setChecked(!check.isChecked());
}
}
Thanks a lot in advance!
daten
This works for me:
You have to set the choicemode of the underlying ListView to single or multiple.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, Android.R.layout.simple_list_item_checked);
list_elems.add("foobar");
//TODO: check added entry labeled "foobar"
setListAdapter(list_elems);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemChecked(0, true);
}
Use a SimpleListAdapter as the ListAdapter for your ListActivity. Use two columns (one for your string and the other for the checked value), and the system should take care of it by itself. Here is a good example

Dynamically add items in list view

I want to make a dynamic list view which gets the user credentials when I login for the first time and displays it in a list the next time I start the app. I know how to send the username from one intent to another. i haven't focused on the SQLite part yet, will do that later. I'm facing problems in creating the dynamic list view.
Found one very useful thread - Dynamically add elements to a listView Android
he used a button on the screen and called the method onClick to populate the list. Can i do it without the button? I want it to automatically happen once i am able to login.
how can i use the statements in my code?
listItems.add(value);
adapter.notifyDataSetChanged();
here value is the username i am getting from some other intent.
please help. thanks!
For this Just use the example given below:
For Instance you are Adding Some Strings into your List
So Create a ListArray like this
ArrayList<String> listItems = new ArrayList<String>();
now whenever you want to add certain string into list just do this thing
EditText editText = (EditText) findViewById(R.id.edit);
listItems.add("my string"); OR
listItems.add(editText.getText.toString()); //incase if you are getting string value from editText and adding it into the list
Use this Xml inside your linear layout in main.xml
<EditText android:id="#+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Now when you have added one item dynamically then call this
adapter.notifyDataSetChanged();
The above will update your list and display the upadted list.
For more info about this see the following links:
http://www.androidpeople.com/android-custom-listview-tutorial-part-1
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
http://www.androidpeople.com/android-custom-dynamic-listview-%E2%80%93part3
In these tutorials you can replace String[] with ArrayList as given at the top of the answer ook and when you want to add any item just simply use the second code snippet.
Thanks
sHaH
The best way to do this will be to use ArrayAdapter. When modifying the adapter it automatically refresh itself so you don't have to call notifyDataSetChanged.
You can try out this code to add elements dynamically to list view.
You can do it with out button click also.
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
//step2 : create all the variables.
EditText et;
Button b;
ListView lv;
ArrayList<string> al;
ArrayAdapter<string> aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//step3 : intitalize all the variables.
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
lv = (ListView) findViewById(R.id.listView1);
al = new ArrayList<string>();//initialize array list
aa = new ArrayAdapter<string>(this,
android.R.layout.simple_list_item_1,
al);//step4 : establish communication bw arraylist and adapter
//step5 : establish communication bw adapter and dest (listview)
lv.setAdapter(aa);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent,
View v, int arg2,
long arg3) {
String item = al.get(arg2);
Toast.makeText(getApplicationContext(), item, 0).show();
}
});
//step6 : button click logic
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//step i: take text from et and add to arraylist
String item = et.getText().toString();
al.add(0, item);
//step ii: notify to adapter
aa.notifyDataSetChanged();
//step iii: clr edit text
et.setText("");
}
});
}
}
For complete code check this list view example

Modifying this List Linstners (custom adapter)

Can someone please tell me how to create a custom adpater for this list, as i dont want toast to display when a user clicks the list item, but, When a user clicks on Google, he will be navigated to "www.google.com" and "www.yahoo.com" and same for msn.com,,
cant figure out at all, stuck for last 1 week, even though i know how to create a intent and call a URI but not working or right for this,
can someone just modify this please ?
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FurtherEducationCourses extends ListActivity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, FURTHER_EDUCATION));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] FURTHER_EDUCATION = new String[] {
"GOOGLE", "YAHOO", "MSN"
};
}
xml file, dunno why u required :s
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
The trick is to get the item at that position, then depends on the position (or even the value at the position), you call the correct link.
So you need to index the value like this, put this code after your static final... statement.
HashMap<String, String> valueToLink = new HashMap<String, String>;// key is Google, Yahoo, value is www.google.com
valueToLink.put("GOOGLE", "www.google.com");
//add yahoo,.etc.
In onItemClick() function, replace the toast by this:
String link = valueToLink.get(((TextView) view).getText());
//code to open the link here

Categories

Resources