2015-09-04 58 views
-1

您好frnds我,这是我的代码为..... JSON解析如何添加分享按钮,在此code..pls帮我如何在我的代码添加分享按钮 - ANDROID

这是从JSON TXT代码..... 我想补充在此分享按钮我的RSS提要.....

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

在一个单独的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")); 
    } 

这应显示将支持共享功能的已安装应用的列表。

编辑://

这是活动的全码: 包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")); 
    } 
} 
+0

哎Metasnarf即时通讯新android的......所以请编辑全codde ......送我的zip文件....请我的邮件编号 - [email protected] –

+0

您只需添加所有的方法对你的活动。 – MetaSnarf

+0

在我的 - 主要活动? –

相关问题