Errors in Android project - java

I'm having problems working my way through an old tutorial, essentially i'm trying to pull the data returned in JSON format from www.amazingjobs.co.uk/app/marc/api/candidates/ into a listview.
I literally started learning android/java 24 hours ago so its likely it may be very simple but I just can't quite seem to figure it out, lines with errors are in bold!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.google.gson.Gson;
public class Searchjobs extends Activity {
// url to make request
private static String url = "http://www.amazingjobs.co.uk/app/marc/api/candidates/";
// JSON Node names
private static final String TAG_RESULTS = "results";
// Jobs JSONArray
JSONArray jobs = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchjobs);
Button Searchbutton = (Button) findViewById(R.id.Searchbutton);
Searchbutton.setOnClickListener(searchClick);
}
private OnClickListener searchClick = new OnClickListener() {
public void onClick(View v) {
// Hashmap for ListView
ArrayList<HashMap<String, String>> JobList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl("http://www.amazingjobs.co.uk/app/marc/api/candidates/");
// Getting Array of Contacts
try {
jobs = json.getJSONArray(TAG_RESULTS);
for(int i = 0; i < jobs.length();i++){
JSONObject j = jobs.getJSONObject(i);
String title = j.getString("job_title");
HashMap<String, String> map = new HashMap<String, String>();
map.put("Title", title);
JobList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**ListAdapter adapter = new SimpleAdapter(this, JobList,R.layout.list_item,
new String[] { "Title" }, new int[] {R.id.title});**
**ERROR: The constructor SimpleAdapter(new View.OnClickListener(){}, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined**
**setListAdapter(adapter);
The method setListAdapter(ListAdapter) is undefined for the type new View.OnClickListener(){}**
// selecting single ListView item
**ListView lv = getListView();**
**ERROR: The method getListView() is undefined for the type new View.OnClickListener(){}**
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
};
};
EDIT Ok new code is below, errors are, which seems odd because the activity errors relate to an activity that definately exists, have tried restart/clean in eclipse too.
Description Resource Path Location Type
activity_searchjobs cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 47 Java Problem
activity_searchjobs cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 105 Java Problem
Searchbutton cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 48 Java Problem
list_item cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 83 Java Problem
public class Searchjobs extends ListActivity {
// url to make request
private static String url = "http://www.amazingjobs.co.uk/app/marc/api/candidates/";
// JSON Node names
private static final String TAG_RESULTS = "results";
// Jobs JSONArray
JSONArray jobs = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchjobs);
Button Searchbutton = (Button) findViewById(R.id.Searchbutton);
Searchbutton.setOnClickListener(searchClick);
}
private OnClickListener searchClick = new OnClickListener() {
public void onClick(View v) {
// Hashmap for ListView
ArrayList<HashMap<String, String>> JobList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl("http://www.amazingjobs.co.uk/app/marc/api/candidates/");
// Getting Array of Contacts
try {
jobs = json.getJSONArray(TAG_RESULTS);
for(int i = 0; i < jobs.length();i++){
JSONObject j = jobs.getJSONObject(i);
String title = j.getString("job_title");
HashMap<String, String> map = new HashMap<String, String>();
map.put("Title", title);
JobList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(getApplication(), JobList,R.layout.list_item,
new String[] { "Title" }, new int[] {R.id.title});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
};
};

You need to extend ListActivity not Activity to make methods like getListView() avaialable or just replace it with findViewById() if you do not want to use ListActivity (it's not mandatory).

ListAdapter adapter = new SimpleAdapter(this, JobList,R.layout.list_item,
new String[] { "Title" }, new int[] {R.id.title});
should be changed to:
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), JobList,R.layout.list_item,
new String[] { "Title" }, new int[] {R.id.title});
Second,
ListView lv = getListView();
will be resolved if you extend ListActivity instead of Activity.

Related

Connect to MySQL?

I used this code to connect to MySQL using WAMP.
But it doesn't work
package com.first.ismael.material_drawer;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by ismae_000 on 11/20/2015.
*/
public class all_meals extends Fragment {
public ProgressDialog pDialog ;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// url to get all products list
private static String url_all_products = "http://192.168.1.7/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "component";
private static final String TAG_PID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_TYPE ="type";
ListView lv;
// products JSONArray
JSONArray products = null;
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.all,
container, false);
productsList = new ArrayList<HashMap<String, String>>();
lv = (ListView)view.findViewById(R.id.listView2);
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
#Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Toast.makeText(getActivity(),"There is nothing",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item, new String[]{TAG_PID,
TAG_NAME},
new int[]{R.id.pid, R.id.name});
// updating listview
lv.setAdapter(adapter);
}
});
}
}
}
I used it in a Fragment.
Please help
If you are using WAMP means using localhost for database MySql. Ip address for localhost should 10.0.2.2 not 192.168.1.7
try to replace
_http://192.168.1.7/get_all_products.php
with
_http://10.0.2.2/get_all_products.php
and check your output get_all_products.php, that's should be json format
read this
- why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client
check json format use this
- _http://jsonlint.com

How should I switch to new activity from a listview that contains an array of data?

I want to switch to a new activity from the listview that contains an array of data from a mysql database?
File: TampilGambarActivity.java
package com.tes.menu;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
//import android.content.Intent;
import android.os.Bundle;
//import android.view.View;
import android.widget.ListView;
//import android.widget.Toast;
public class TampilkanGambarActivity extends Activity {
static String in_idmenu = "id_menu";
static String in_jenismenu = "jenis_menu";
static String in_image_menu = "image_menu";
JSONArray str_json = null;
public String lo_Koneksi,isi ;
ListView list;
LazyAdapter adapter;
ArrayList<HashMap<String, String>> data_map = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Koneksi lo_Koneksi = new Koneksi();
isi = lo_Koneksi.isi_koneksi();
String link_url = isi+"menu.php";
JSONParser jParser = new JSONParser();
JSONObject json = jParser.AmbilJson(link_url);
try {
str_json = json.getJSONArray("menu");
for(int i = 0; i < str_json.length(); i++){
JSONObject ar = str_json.getJSONObject(i);
String id_menu = ar.getString("id_menu");
String image_menu = ar.getString("image_menu");
String jenis_menu = ar.getString("jenis_menu");
HashMap<String, String> map = new HashMap<String, String>();
map.put(in_idmenu, id_menu);
map.put(in_image_menu, image_menu);
map.put(in_jenismenu, jenis_menu);
data_map.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, data_map);
list.setAdapter(adapter);
}
}
Set your Listview on Item Click Listener
listView.setOnItemClickListener(this);
The method should look like this.
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//you can send data here based on the position
}
Of course let the class implement the listener, and then within the onItem click use the Intent
Intent intent = new Intent(this, LazyAdapter.class);
intent.putExtra("hashMap", map);
startActivity(intent);
Get the data from the Second activity
Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("hashMap");
Now you have the data in the second activity, use it the same way.

printing an string array to listview

package com.fyp.khursani;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.NameValuePair;
import test.fyp.khursani.library.JSONParser;
import com.fyp.khursani.activity.SingleListItem;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewDishes extends ListFragment {
JSONParser jsonParser = new JSONParser();
private static String url_menu = "http://192.168.56.1/khursaani/get_menu_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "menuItemName";
private static final String TAG_PRICE = "menuItemPrice";
private static final String TAG_DESC = "menuItemDescription";
private static final String TAG_UNIT = "menuItemUnit";
private static final String TAG_IMG = "menuItemImage";
private static final String TAG_MENU = "menu";
//progress dialogue
private ProgressDialog pDialog;
JSONArray events = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_viewdishes, container, false);
// storing string resources into Array
// String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
// this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, adobe_products));
new asyncmenu().execute();
return rootView;
}
class asyncmenu extends AsyncTask<String, String, String[]> {
protected void onPreExecute() {
super.onPreExecute();
}
protected String[] doInBackground(String... params) {
ArrayList<NameValuePair> postparameters2send = new ArrayList<NameValuePair>();
JSONObject json = jsonParser.makeHttpRequest(url_menu, "POST", postparameters2send);
Log.d("response from php", json.toString());
int status = 0;
try {
int success = json.getInt(TAG_SUCCESS);
JSONArray menu = null;
menu = json.getJSONArray(TAG_MENU);
String[] menuarray = new String[100];
for (int i = 0; i < menu.length(); i++) {
JSONObject c = menu.getJSONObject(i);
menuarray[i] = c.getString(TAG_NAME);
Log.d("menu", menuarray[i]);
}
return menuarray;
} catch (JSONException e) {
e.printStackTrace();
}
String[] menuarray = new String[10];
return menuarray;
}
protected void onPostExecute(String[] result) {
Log.d("onPostExecute", result[0]);
ViewDishes.this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, result));
}
}
}
In this file above , there was previously an array R.array.adobe_products which looked like this :
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="adobe_products">
<item>Tandoori Chicken</item>
<item>Chicken Tikka</item>
<item>Sheikh Kebab</item>
<item>Tandoori Prawn</item>
<item>Chicken Curry</item>
<item>Chicken Do Pizza</item>
<item>Nepal Ice</item>
<item>Corona Beer</item>
<item>Coca Cola</item>
<item>Ginger Ale</item>
<item>Vanilla Ice Cream</item>
<item>Mango Melba</item>
<item>Masala Chai</item>
</string-array>
</resources>
This was output to listfragment like this:
this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, adobe_products));
I now have an array of strings named result (in function onPostExecute) that is received after parsing a json file from a remote server, which is a plain array (as opposed to adobe_products, which is a fancy xml).
The array adobe_products is displayed as is expected. To display the new array (result), i've tried passing the new array in place of `adobe_products like so:
ViewDishes.this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, result));
which doesn't seem to work[crashes].
LogCat output:
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
How can I display the array as a list of items?
I always prefer using an ArrayList instead of an Array (specially to populate List adapters, there's a reason they both end in "List" and not array), because they can grow after being declared.
Refer to this thread.

Android ActivityThread error

I cannot figure out what is causing this Thread error. Here is my code. I commented out almost all the code and it still won't work:
package com.dd.relay;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
public final static String EXTRA_MESSAGE = "com.dd.relay.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.activity_main);
//ListView lv = (ListView)this.findViewById(R.id.relaylist);
// Make a GET request for data
/* String url = "http://localhost.com/contacts";
String res = null;
try {
HttpRequest request = new HttpRequest();
request.execute(new URL(url));
Log.v(EXTRA_MESSAGE, res);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
String[] from = new String[] {"First", "Last", "Number"};
int[] to = new int[] {R.id.first, R.id.last, R.id.number};
// Create list for contacts
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> temp = new HashMap<String, String>();
int id = 0;
while(id<10)
{
RelayContact contact = new RelayContact("Matt", "Hintzke", "2062259311", id);
temp.put("First",contact.first);
temp.put("Last", contact.last);
temp.put("Number", contact.num);
data.add(temp);
id++;
}
ListAdapter adapter = new SimpleAdapter(this, data, R.layout.list, from, to); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
lv.setAdapter(adapter);
*/
// CharSequence msg = "List loaded";
// Context context = this.getApplicationContext();
//Toast mytoast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
//mytoast.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is the error from the console
[2013-07-20 19:18:36 - ddms] null
java.lang.NullPointerException
at org.eclipse.debug.internal.ui.DebugUIPlugin.launchInBackground(DebugUIPlugin.java:1286)
at org.eclipse.debug.ui.DebugUITools.launch(DebugUITools.java:753)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.debugRunningApp(AndroidLaunchController.java:176)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.clientChanged(AndroidLaunchController.java:1733)
at com.android.ddmlib.AndroidDebugBridge.clientChanged(AndroidDebugBridge.java:912)
at com.android.ddmlib.Device.update(Device.java:591)
at com.android.ddmlib.Client.update(Client.java:868)
at com.android.ddmlib.HandleWait.handleWAIT(HandleWait.java:88)
at com.android.ddmlib.HandleWait.handleChunk(HandleWait.java:66)
at com.android.ddmlib.MonitorThread.callHandler(MonitorThread.java:414)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:322)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

android linkify in listview

I have this code:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.romantic.aacplay.R;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
// import android.text.Html;
// import android.text.Html;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class PodCast extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://mysite.net/andro/pcast.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "podcast";
private static final String TAG_LINK = "link";
private static final String TAG_NAME = "nume";
JSONArray products = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poadcast);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Color.RED,Color.BLACK});
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundDrawable(gd);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PodCast.this);
pDialog.setMessage("Se incarca. Asteapta...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
Log.d("Ultimele 10 piese: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String link = c.getString(TAG_LINK);
String name = c.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
// String href = String.format(" %s ", link, name);
// String cici = Html.fromHtml(href).toString();
map.put(TAG_NAME, name);
map.put(TAG_LINK, "" + Html.fromHtml(link));
// map.put(TAG_LINK, link);
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
PodCast.this, productsList,
R.layout.list_item, new String[] {
TAG_NAME, TAG_LINK },
new int[] { R.id.link, R.id.name });
setListAdapter(adapter);
}
});
}
}
}
Now i'm trying to figure out how can i make the links in listview clickable. I`m trying to linkify a textview that is in found here:
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
PodCast.this, productsList,
R.layout.list_item, new String[] {
TAG_NAME, TAG_LINK },
new int[] { R.id.link, R.id.name });
setListAdapter(adapter);
}
});
}
R.layout.list_item which is a different .xml layout for the listview.
So can you guys help me solve this? Thanks in advance!
I feel instead of using SimpleAdapter,Please extend the BaseAdapter and make the textview you are using as linkify.so it will automatically get click.
please refer:
Android dev blog
put this line to ur textview
android:autoLink="web"
i hope this will solve ur query
I wanted as well to create a linkeable textView on my list adapter so I used:
Linkify.addLinks(textViewLocationPhone, Linkify.PHONE_NUMBERS);
textViewLocationPhone.setLinksClickable(true);
and it worked.

Categories

Resources