你好frnds这是我的代码..... for json解析如何在这段代码中添加分享按钮..请帮助我
这是来自json txt代码的rss feed ..... 我想在此添加分享按钮.....
public class MainActivity extends Activity {
ArrayList<Actors> actorsList;
ActorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actorsList = new ArrayList<Actors>();
new JSONAsyncTask().execute("link here");
ListView listview = (ListView) findViewById(R.id.list);
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("actors");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDescription(object.getString("description"));
actor.setDob(object.getString("dob"));
actor.setCountry(object.getString("country"));
actor.setHeight(object.getString("height"));
actor.setSpouse(object.getString("spouse"));
actor.setChildren(object.getString("children"));
actor.setImage(object.getString("image"));
actorsList.add(actor);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
在单独的xml文件中创建您的共享项(可选择将其命名为:name_of_xml_menu.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="@+id/action_share"
android:title="Share"
android:icon="@mipmap/icon_share_white"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
然后在您的活动onCreateOptionsMenu
上使用它 @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.name_of_xml_menu, menu);
return true;
}
现在,只要点击该共享图标,就会调用onOptiosItemSelected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_share) {
openShareMenu();
}
else if(id == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
现在我的openShareMenu方法包含一个支持共享的应用程序列表。
public void openShareMenu(){
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);//android.content.Intent.ACTION_SEND
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
String shareText = "this is the share text";
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareText);
//display apps that support the intent
List<ResolveInfo> respondingApps = getPackageManager().queryIntentActivities(sharingIntent,0);
for(ResolveInfo ri : respondingApps){
ComponentName name = new ComponentName(ri.activityInfo.packageName,ri.activityInfo.name);
Log.v(TAG,"-->"+ri.loadLabel(getPackageManager()));
}
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
这应显示支持共享功能的已安装应用列表。
编辑://
这是活动的完整代码: package com.test.shareproject;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.name_of_xml_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_share) {
openShareMenu();
}
else if(id == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
public void openShareMenu(){
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);//android.content.Intent.ACTION_SEND
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
String shareText = "this is the share text";
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareText);
//display apps that support the intent
List<ResolveInfo> respondingApps = getPackageManager().queryIntentActivities(sharingIntent,0);
for(ResolveInfo ri : respondingApps){
ComponentName name = new ComponentName(ri.activityInfo.packageName,ri.activityInfo.name);
Log.v("TAG", "-->" + ri.loadLabel(getPackageManager()));
}
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
}