What I am trying to do is load a URL in a webView in the main activity from a populated listView item. I am not sure how to do this and I am learning step by step, working my way to eventually adapt this to populating the listView with a dataBase once I learn this step (in this question).
The getURL variable needs to be used to load the link into mWebView in the .main activity when the list item object is clicked. (make sense??)
Thanx for your help!!
Below is what I have so far:
the listView activity (ACList.java):
public class ACList extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.ac_list);
ArrayList<SearchResult> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listItems);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Object o = lv1.getItemAtPosition(position);
SearchResult fullObject = (SearchResult)o;
Toast.makeText(ACList.this, "You have chosen: " + " " + fullObject.getLabel(), Toast.LENGTH_LONG).show();
/** -- Here is the Class I intend to start with the specified URL to the selected item
Intent i = new Intent(ACList.this, main.class);
i.putExtra("url", fullObject.getURL());
startActivity(i);
**/
}
});
}
private ArrayList<SearchResult> GetSearchResults()
{
ArrayList<SearchResult> results = new ArrayList<SearchResult>();
SearchResult sr1 = new SearchResult();
sr1.setLabel("The Lable");
sr1.setListTitle("This is the Title");
sr1.setCaption("This is what its about");
sr1.setURL("http://www.somesite.com/index.html");
results.add(sr1);
}
}
This is my Adapter (MyCustomBaseAdapter):
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResult> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResult> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtLabel = (TextView) convertView.findViewById(R.id.label);
holder.txtListTitle = (TextView) convertView.findViewById(R.id.listTitle);
holder.txtCaption = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtLabel.setText(searchArrayList.get(position).getLabel());
holder.txtListTitle.setText(searchArrayList.get(position).getListTitle());
holder.txtCaption.setText(searchArrayList.get(position).getCaption());
return convertView;
}
static class ViewHolder {
TextView txtLabel;
TextView txtListTitle;
TextView txtCaption;
}
}
…and my SearchResult.java:
public class SearchResult
{
private String label = "";
private String listTitle = "";
private String caption = "";
private String listURL = "";
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setListTitle(String listTitle) {
this.listTitle = listTitle;
}
public String getListTitle() {
return listTitle;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getCaption() {
return caption;
}
public void setURL(String listURL) {
this.listURL = listURL;
}
public String getURL() {
return listURL;
}
}
I figured out what the problem was; I was missing a string variable in my .main activity that links up w/the item in the listView activity. See below (First line).
Bonus – For whoever has use for it: The onProgressChanged in the webChromeClient shown below has a pre-loader that fires every time a new URL is loaded into it. Just add the “pd” variable below the setContent(R.layout.myActivity) and copy & past the mWebView.setWebClient block in2 your activity as well that has the webView. Enjoy!