如何获取列表视图的选定项目以共享特定文本?
这是我的代码,我试过这个,但它没有用
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 :(得分:1)
您需要实施list.setOnItemClickListener(new OnItemClickListener())
并根据ArrayList or Array
ListItem
从ListItem
获取特定position
的值,例如:
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"));
}
});
的此链接