Issue
I have an array list of items presented in a list view. What I am trying to do is get the string value of the parameters of an array list value to put into a share intent when I click a share button.
Here is my REVISED code based on the inputs and suggestions I have recieved so far:
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
//public String term = null;
public String url;
public EditText editTextInput;
ArrayList<HashMap<String, String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = (EditText) findViewById(R.id.editTextInput);
itemList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
//new GetSearchItems().execute();
}
public void onSearchClick(View v)
{
// String term = editTextInput.getText().toString();
// url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
if (lv== null) {
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
else {
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
}
public void page1(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page2(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=11";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page3(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=21";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page4(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=31";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page5(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=41";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page6(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=51";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page7(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=61";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
// public void onShareClick(View v) {
//
// Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
// sharingIntent.setType("text/plain");
// String shareSub = String.valueOf(v.findViewById(R.id.title));
// String shareBody = String.valueOf(v.findViewById(R.id.link));
//
// sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
// sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
// startActivity(Intent.createChooser(sharingIntent, "Share using"));
// }
public class GetSearchItems extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"SearchResults are downloading",Toast.LENGTH_LONG).show();
}
@Override
public Void doInBackground(Void... arg0)
{
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
//String url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null)
{
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray items = jsonObj.getJSONArray("items");
// looping through All results
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
String title = c.getString("title");
String link = c.getString("link");
String displayLink = c.getString("displayLink");
String formattedUrl = c.getString("formattedUrl");
String snippet = c.getString("snippet");
// tmp hash map for single result
HashMap<String, String> item = new HashMap<>();
// adding each child node to HashMap key => value
item.put("title", title);
item.put("link", link);
item.put("displayLink", displayLink);
item.put("formattedUrl", formattedUrl);
item.put("snippet", snippet);
// adding contact to result list
itemList.add(item);
}
} catch (final JSONException e)
{
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else
{
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(MainActivity.this, itemList,
R.layout.list_item,
new String[] { "title","link", "snippet" }, new int[] {
R.id.title, R.id.link, R.id.snippet });
lv.setAdapter(adapter);
//setListAdapter(adapter);
// selecting single ListView item
//ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
//@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// getting values from selected ListItem
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
String snippet = ((TextView) view.findViewById(R.id.snippet)).getText().toString();
Button shareButton = (Button) view.findViewById(R.id.sharebutton);
shareButton.setOnClickListener((View.OnClickListener) shareButton);
{
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, link + " " + snippet );
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
});
}
}
}
I am getting the following error in the debugger console when I click the share button:
java.lang.IllegalStateException: Could not find method onItemClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'sharebutton'
I cannot find out what I did incorrect based on the suggestions I received. What did I do wrong, and what's the best way to fix it?
Solution
This is how you will get the text values from your array list items and put them in an intent and further display them in a new activity if necessary. (Refer to code)
//-----------------------------------------------------------------------
// MainActivity.java
// This is how you will get the text values out of the array list items
// And create a share intent
//-----------------------------------------------------------------------
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(MainActivity.this, itemList,
R.layout.list_item,
new String[] { "title","link", "snippet" }, new int[] {
R.id.title, R.id.link, R.id.snippet });
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) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
String snippet = ((TextView) view.findViewById(R.id.snippet)).getText().toString();
// Starting Share intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, link + " " + snippet );
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
});
}
Answered By - nocholla
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.