I have a database of webpages like this:
import android.provider.BaseColumns;
public interface ThreadDatabase extends BaseColumns {
String TABLE_NAME = "Threads";
String TITLE = "Title";
String DESCRIPTION = "Description";
String URL = "Url";
String[] COLUMNS = new String[] { _ID, TITLE, DESCRIPTION, URL };
}
And a Listview in my Activity with a SimpleCursorAdapter that displays Title and Descripton in every row.
Cursor c = dbhelper.getDatabase();
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, new String[] {
ThreadDatabase.TITLE, ThreadDatabase.DESCRIPTION },
new int[] { android.R.id.text1, android.R.id.text2, })
How could I redefine onListItemClick method so that when I click on the item the right url is opened?
protected void onListItemClick(ListView l, View v, int position, long id) {
???Missing part???
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
I'm assuming your activity extends ListActivity. If so, in onListItemClick(), all you need to do is:
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor item = (Cursor)getListAdapter().getItem(position);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
You need a reference to your SimpleCursorAdapter so initialize it to a field.
Assuming you have a field mAdapter, you can get the cursor like this:
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = (Cursor) mAdapter.getItem(position);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
Hope that helps.
Related
How to set the String URL as the data for the Intent as a Uri object?
Where am I going wrong?
protected String[] mUrls = { "http://www.teamtreehouse.com", "http://developer.android.com", "http://www.github.com" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mUrls);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(mUrls));
startActivity(intent);
}
Dont pass the array of urls. You need the select the one on each list item therefore change the code to:
protected String[] mUrls = { "http://www.teamtreehouse.com", "http://developer.android.com", "http://www.github.com" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mUrls);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(mUrls[position]));
startActivity(intent);
}
You can parse only one String with method Uri.parse(String uriString), so you can't put there the String array.
intent.setData(Uri.parse(mUrls));
This looks quite wrong. mUrls is an array of Strings and you're parsing it into one Uri, that won't work.
You'll have to parse every String from the Array into different Uris.
Atention: this is not real working code but a pseudo-code like example:
intent.setData(mUrls);
And where you receive the data:
for (String s : mUrls){
Uri u = Uri.parse(s);
}
Hi everyone:
I hope someone can help me here,The Cursor generated by a Query populate the ListView with the Layout defined by simple_list_item_2.
To create the Intent i need the first string of the clicked View but in the String to Go i have something like
android.widget.TextView#411fbfb8
Now the code.
I can't understand where is the error.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, ids, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView myList=(ListView)findViewById(R.id.listView1);
myList.setAdapter(adapter);
myList.setClickable(true);
/*
* Click Listener
*/
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Log.v("io", "start");
Intent intent = new Intent(MainActivity.this, WorkActivity.class);
View buff = myList.getChildAt(position);
String toGo = buff.findViewById(android.R.id.text1).toString();
Log.v("io",toGo);
intent.putExtra("dname", toGo);
startActivity(intent);
}
} );
try:
String toGo = ((TextView)buff.findViewById(android.R.id.text1)).getText().toString()
inside your onItemClick you can retrieve data linked to your adapter through the AdapterView argument. Calling getItemAtPosition(int) you can access data associated with the selected item
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Log.v("io", "start");
Intent intent = new Intent(MainActivity.this, WorkActivity.class);
View buff = myList.getChildAt(position);
String toGo = l.getItemAtPosition(position);
Log.v("io",toGo);
intent.putExtra("dname", toGo);
startActivity(intent);
}
} );
//
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Log.v("io", "start");
Intent intent = new Intent(MainActivity.this, WorkActivity.class);
//View buff = myList.getChildAt(position);
// String toGo = buff.findViewById(android.R.id.text1).toString();
String toGo = (TextView)v.getText();
Log.v("io",toGo);
intent.putExtra("dname", toGo);
startActivity(intent);
}
} );
To get the view do as following:
LinearLayout row = (LinearLayout) ((LinearLayout)v);
Or RelativeLayout instead of LinearLayout if that your case
then apply the follwing code to get the text of TextView based on its order:
TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();
Hope this help
I have a cursor in my main activity to query two columns of the database and display them using a simple cursor adapter.
private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT RestaurantID AS _id, RestaurantName, StoreNo "
+ "FROM RestaurantList ORDER BY RestaurantName", null));
}
public void onPostExecute() {
SimpleCursorAdapter adapter;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
doQuery(), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore },
new int[] { R.id.title, R.id.value },
0);
}
else {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
doQuery(), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore },
new int[] { R.id.title, R.id.value });
}
setListAdapter(adapter);
}
What I want to do is that when the users click on a particular item, another activity will pop up giving the users more information, which means I need to display the data stored in many other columns of the same item too. My question is how I can do that. I thought I should use onListItemClick, but I don't know how to associate the item selected in the main activity to the new activity.
Also, in the main activity, I have two textviews in the list that display colRestaurantName and colRestaurantStore respectively. When I use onListItemClick, how can I just retrieve colRestaurantName?
I can do the following to retrieve the position, but I don't know how I can use it in the new activity:
#Override
public void onListItemClick(ListView parent, View v, int position,
long id){
Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
selection = String.valueOf(c.getPosition());
Intent i = new Intent(AllRestaurants.this, RestaurantsInfo.class);
i.putExtra("restaurantSelection", selection);
startActivity(i);
}
Thanks.
implement onListItemClick for listview and get textview text and send it to next activity as:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView tv = (TextView)v.findViewById(R.id.title);
String strcolRestaurantName=tv.getText().toString();
Intent intent =new Intent(CurrentActivity.this,NextActivity.class);
Bundle bundle = new Bundle();
bundle.putString("RestaurantName",strcolRestaurantName);
intent.putExtras(bundle);
startActivity(intent);
}}
and in NextActivity get Intent as in onCreate :
Intent i = getIntent();
Bundle extras = i.getExtras();
String strRestaurantName = extras.getString("RestaurantName");
I have a ListFragment that is populated using a CursorLoader. Once the ListFragment is populated, I have an OnItemClickListener method in which I want to identify which item from the List Fragment the user chose. How do I do this? I've tried:
String item = getListAdapter().getItem(position);
String item = getListAdapter().getItem(position).toString();
and
String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(0);
where position is an integer passed to the onClickItemListener method like so:
public void onListItemClick(ListView l, View v, int position, long id)
All of these throw Cast Exceptions of one type or another. I'm stumped. This seems like a simple thing to do. For your reference, here's how the List Fragment is populated:
private SimpleCursorAdapter mAdapter;
private static final String[] PROJECTION = new String[] { "_id", "stitchname" };
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Intent myData = getActivity().getIntent();
Bundle info = myData.getExtras();
String[] dataColumns = { "stitchname" };
int[] viewIDs = { R.id.stitchlist1 };
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "stitchlevel=?";
String[] selectionArgs = new String[] {args.getString("Level")};
return (Loader<Cursor>) new CursorLoader(getActivity(), STITCHES_URI, PROJECTION, selection, selectionArgs, null);
}
Any suggestions would be most welcome, thanks!
You are overriding the wrong method. You said you are using an ListFragment, so you may have been declaring your CursorAdapter/ArrayAdapter somewhere in your Fragment. In that case, do the following:
class yourFragemt extends ListFragment{
//Member Variable
private SimpleCursorAdapter mAdapter;
....
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Cursor c = (Cursor) mAdapter.getItem(pos);
String value = c.getString(c
.getColumnIndex(ColumnIndex));
}
}
The answer is as follows:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);
String item = c.getString(1);
}
I got the answer from the second link provided to me by Georgy Gobozov in a comment, above.
I am currently having a difficulty in passing the name from the checkbox from one activity to another activity. Actually the name should be inserted into my database table after it is checked and then it will be passed to the activity mentioned earlier. Anyone have any idea on doing it? Any help provided will be greatly appreciated.
For reference, the codes for my project.
BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
ListView list = (ListView) findViewById(android.R.id.list);
//ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
Cursor cursor = null;
cursor = (Cursor) parent.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
}
});
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
int[] views = new int[] {R.id.contactCheckbox};
startManagingCursor(c);
SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
this.setListAdapter(friendsAdapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
buddyDB.open();
//long name_id;
super.onListItemClick(l, v, position, id);
Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);
/*TextView contactName = (TextView) v.findViewById(R.id.contactName);
String NameValue = contactName.getText().toString();
name_id = buddyDB.insertNames(NameValue);
Toast.makeText(getBaseContext(),
"Selected: " + buddiesList[position], Toast.LENGTH_SHORT).show();
buddyDB.close();*/
nameCheck = (CheckBox) v.findViewById(R.id.contactCheckbox);
nameCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
long name_id;
String NameValue = nameCheck.getText().toString();
name_id = buddyDB.insertNames(NameValue);
/*if(isChecked)
{
nameCheck = 1;
}else
{
nameCheck = 0;
}*/
}
});
buddyDB.close();
}
}
Again, any help from anyone will be greatly appreciated. Thanks a lot!
this may helps you
To pass the name values from oneActivity
Intent n = new Intent(actA.this , actB.class);
n.putExtra("name", NameValue);
startActivity(n);
To get the given values
Intent igetlist = getIntent();
String singleid = igetlist.getExtras().getString("name");
use this code where you want to pass parameter to another activity.
Intent n = new Intent(actA.this , actB.class);
n.putExtra("name", NameValue);
startActivity(n);