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);
}
Related
this is the script :
public class LecturAct extends ListActivity {
private static final String AR_ID = "id_lec";
private static final String AR_LC = "lec_nm";
private static final String AR_CLS = "class";
private static final String EXTRA_DATA = "idr";
JSONArray lec = null;
ArrayList<HashMap<String, String>> lec_list = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_lec);
Intent quiz = getIntent();
String id = quiz.getStringExtra(EXTRA_DATA);
String link_url = "http://xxxx/xxxx.php?id="+id;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.gtJson(link_url);
try {
lec = json.getJSONArray("lec");
for(int i = 0; i < lec.length(); i++){
JSONObject ar =lec.getJSONObject(i);
String lectid = ar.getString(AR_ID);
String lecname = ar.getString(AR_LC);
String class = ar.getString(AR_CLS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(AR_ID, lectid);
map.put(AR_LC, lecname);
map.put(AR_CLS, class);
lec_list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.adapter_listview();
}
ive tried to make a clickable item on my listview, this is the script:
public void adapter_listview() {
ListAdapter adapter = new SimpleAdapter(this, lec_list,
R.layout.list_item,
new String[] { AR_ID, AR_CLS, AR_LC}, new int[] {
R.id.title, R.id.content, R.id.code});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cd = ((TextView) view.findViewById(R.id.code)).getText().toString();
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
});
}
but when i click an item, it keep take me to the wrong direction, always take me to the last item. if the items on the list are
physic
biology
sport
social
math
then when i click biology or whatever i choose, it will always lead me to the "math" page. so what should i do? am i using a wrong adapter? please help me, give me solution to make this right :(
This answer is like Amin's but with few differences. Sample code:
#Override
protected void onListItemClick (ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mlecture p = (String) l.getItemAtPosition(position);
...
in.putExtra(AR_ID, lecture);
startActivity(in);
}
Notes:
Get the ListView parameter (l), and get the row/item position to begin with.
You cannot use view.findViewById method since ListView (l) already has the UI object.
Use the override onListItemClick since ListActivity is used.
Just override protected void onListItemClick (ListView l, View v, int position, long id) in LecturAct for example
protected void onListItemClick (ListView l, View v, int position, long id) {
String cd = getListAdapter().getItem(position);
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
I reviewed your code more closely. I suspect the data is not arranged correctly.
Change FROM:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.title, R.id.content, R.id.code});
TO:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.code, R.id.title, R.id.content });
I switched R.id.code. I think this is compatible with in.putExtra(AR_ID, cd). There is no good way for me to make sure since I don't know Quiz.class.
My example like yours.Just change your object casting.Or String.
catalogListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object obj=(Object)((String) parent.getAdapter().getItem(position));
String ss=obj.toString();
Intent i = new Intent(getActivity(), PdfReader.class);
i.putExtra("catalogPdfUrl", "" +ss;
getActivity().startActivity(i);
}
});
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
Hi I have a android application and i use a sqlite database and a listview in my Activity.
Now I want to use onListItemClick but I don_t know how I can get the value that I click and open a new activity with this value :(
here my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
mHelper = new DatenbankManager(this);
mDatenbank = mHelper.getReadableDatabase();
ladeDaten();
}
my ladeDaten method:
private void ladeDaten() {
Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null);
startManagingCursor(KlassenCursor);
android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
KlassenCursor,
new String[] {"name"},
new int[] {
android.R.id.text1
});
setListAdapter(KlassenAdapter);
}
Here my onListItemClick that don't work :(
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
I think you using cursor to get data from the database.Instead of using onListItemClick() use onItemClickListener() To get the listView item details use the following code
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String itemid = cursor.getString(cursor.getColumnIndex("ColumnName");//Repeat for other values
//Start the activity here
Intent todayreview = new Intent(ReviewPayment.this,
ReviewandResend.class);
todayreview.putExtra("iteid", itemid);
startActivity(todayreview);
}
});
To use onListItemClick Add extends ListActivity.
public class MainActivity extends ListActivity
But i think it is better to use onItemClick() like Vino said
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 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.