2014-02-27 38 views
0

如何獲取列表視圖的選定項目以共享相關文本?在社交網站上共享來自列表視圖的新聞鏈接

這裏是我的代碼,我已經試過,但它不工作

public class MainActivity extends ListActivity { 

TextView content; 
ListView list; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // list = (ListView) findViewById(R.id.list1); 
    content = (TextView) findViewById(R.id.output); 

    // listView = (ListView) findViewById(R.id.list); 
    String[] values = new String[] { "Android Example ListActivity", 
      "Adapter implementation", "Simple List View With ListActivity", 
      "ListActivity Android", "Android Example", 
      "ListActivity Source Code", 
      "ListView ListActivity Array Adapter", 
      "Android Example ListActivity" }; 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, values); 

    // Assign adapter to List 
    setListAdapter(adapter); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    super.onListItemClick(l, v, position, id); 

    // ListView Clicked item index 
    int itemPosition = position; 

    // ListView Clicked item value 
    String itemValue = (String) l.getItemAtPosition(position); 

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain"); 

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
      "Subject Here"); 
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, itemValue); 
    startActivity(Intent.createChooser(sharingIntent, "Share via")); 

} 

}

+0

您從XML中獲取數據並將其存儲到ArrayList中? –

+0

@M D取自rss – poojagupta

+0

@PG並存儲在哪裏? –

回答

1

您需要實現list.setOnItemClickListener(new OnItemClickListener())並獲取從ArrayList or Array特定ListItem值每ListItemposition,如:

list.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
       long arg3) { 
      // TODO Auto-generated method stub 

      String txt = text.getText().toString(); 
      Intent sharingIntent = new Intent(
        android.content.Intent.ACTION_SEND); 
      sharingIntent.setType("text/plain"); 
      String shareBody = "Here is the share content body"; 
      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
        "Subject Here"); 
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, yourarraylist.get(position)); 
      startActivity(Intent.createChooser(sharingIntent, "Share via")); 

     } 

    }); 

更新:轉到此鏈接Create_Listview_With_ListActivity_-_Android_Example

+0

@MDI已經嘗試了這個,但對於Facebook它不工作的Gmail和它的所有工作 – poojagupta

+0

@poojagupta然後它不是故障有關ListView項目等待我告訴你一些Facebook和Twitter的你需要設置他們的意圖類去這個:[http://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654](http://stackoverflow.com/questions/6827407/how-to-customize -share-intent-in-android/9229654#9229654) –

+0

@poojagupta轉到此帖:[http://stackoverflow.com/questions/8308666/how-to-force-share-intent-to-open-a- specific-app?rq = 1](http://stackoverflow.com/questions/8308666/how-to-force-share-intent-to-open-a-specific-app?rq=1)也是我一直在這工作,並得到了Facebook官方應用程序不提供意圖直接支持 –

相關問題