通过发送邮件应用程序的ListView结果 [英] Send ListView results via Messaging application
问题描述
我很新的节目。我有一个具有多个视图的应用程序。主视图显示一个列表,如早餐,午餐和放大器;晚餐。当选择了一个项目,例如午餐,午餐菜单项目的列表被显示,如汉堡,乳酪,炸薯条...(从字符串阵列创建该列表lunch_menu存储在\\值\\ lunch.xml ),为用户选择他们想要的项目时,它被存储在称为myNewList新数组和磨片的用户presses的lunchList按钮被显示。所有项目的显示的用户选择。到现在为止还挺好。我创建了一个安卓的onClick =shareMyList的selecteditems.xml和按钮的作品,但不填充我的名单。我想,我需要的是一些如何将其转换为一个字符串,这是我需要帮助。
这是我现在的问题....我有我的共享按钮,当pressed,我想它从选定项目的ListView自动打开默认的消息应用程序,并填充列表。
包com.mycompany.lunch;进口的java.util.ArrayList;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.Toast;公共类LunchListMenu延伸活动{ / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.maincoarse);
最终的ListView LV =(ListView控件)findViewById(R.id.listView1);
ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(这一点,R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(适配器);
最终的ArrayList<串GT; myNewList =新的ArrayList<串GT;();
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
字符串项= lv.getItemAtPosition(ARG2)的ToString();
串itemordered;
itemordered =项目+添加到列表;
Toast.makeText(getApplicationContext(),itemordered,Toast.LENGTH_SHORT).show();
myNewList.add(项目);
}
}); //列表视图按钮
按钮btnLunchList =(按钮)findViewById(R.id.lrList);
btnLunchList.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
的setContentView(R.layout.selecteditems);
ListView控件selecteditems =(ListView控件)findViewById(android.R.id.list);
ArrayAdapter<串GT; newadapter =新ArrayAdapter<串GT;(LunchListMenu.this,android.R.layout.simple_list_item_1,myNewList);
selecteditems.setAdapter(newadapter);
}
});
}
公共无效shareMyList(视图v){
//分享选的项目按
按钮btnShareItems =(按钮)findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根 意图份额=新意图(Intent.ACTION_SEND);
share.setType(text / plain的);
share.putExtra(Intent.EXTRA_TEXT,我被送到!);
startActivity(Intent.createChooser(份额,共享文本));
}
});
}
}
下面是午餐菜单布局
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:背景=@绘制/ main_background
机器人:paddingLeft =10.0dip
机器人:paddingTop =0.0dip
机器人:paddingRight =10.0dip
机器人:paddingBottom会=10.0dip
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <按钮
机器人:ID =@ + ID / lrList
机器人:layout_width =72dip
机器人:layout_height =72dip
机器人:layout_gravity =右
机器人:背景=@绘制/目录/> < ImageView的
机器人:ID =@ + ID / LunchMenuTitle
机器人:contentDescription =@字符串/ LunchMenu
机器人:layout_width =0dip
机器人:layout_height =72dip
机器人:layout_weight =0.96
机器人:背景=@绘制/午餐
机器人:paddingLeft =10.0dip
机器人:paddingRight =10.0dip/> < / LinearLayout中> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =10.0dip
机器人:背景=@绘制/头
机器人:方向=横向/> < ListView控件
机器人:ID =@ + ID / ListView1的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:分=#FFCC00
机器人:dividerHeight =2DP>
< /&的ListView GT; <的LinearLayout
机器人:方向=横向
机器人:背景=@绘制/头
机器人:layout_width =FILL_PARENT
机器人:layout_height =10.0dip/>
< / LinearLayout中>
这是我的selecteditems.xml布局
<?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:背景=@绘制/ main_background
机器人:paddingLeft =10.0dip
机器人:paddingTop =0.0dip
机器人:paddingRight =10.0dip
机器人:paddingBottom会=10.0dip
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <按钮
机器人:ID =@ + ID / shareMyList
机器人:layout_width =72dip
机器人:layout_height =72dip
机器人:layout_gravity =右
安卓的onClick =shareMyList
机器人:背景=@绘制/股/> < ImageView的
机器人:ID =@ + ID / selectedItemsTitle
机器人:contentDescription =@字符串/ LunchTitle
机器人:layout_width =0dip
机器人:layout_height =72dip
机器人:layout_weight =0.96
机器人:背景=@绘制/标题
机器人:paddingLeft =10.0dip
机器人:paddingRight =10.0dip/> < / LinearLayout中> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =10.0dip
机器人:背景=@绘制/头
机器人:方向=横向/> < ListView控件
机器人:ID =@机器人:ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:分=#FFCC00
机器人:dividerHeight =2DP
机器人:填充=10dip
机器人:文字颜色=#FFFFFF
机器人:TEXTSIZE =20dip
机器人:文字样式=大胆/>< / LinearLayout中>
这可能是这样做的最kludgiest的方式,但它的作品。
包com.mycompany.lunch;进口java.io.BufferedReader中;
进口的java.io.File;
进口java.io.FileReader;
进口java.io.FileWriter;
进口java.io.IOException异常;
进口的java.io.PrintWriter;
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.os.Environment;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.Toast;公共类LunchListMenu延伸活动{ 串itemsordered; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.maincoarse);
最终的ListView LV =(ListView控件)findViewById(R.id.listView1);
ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(这一点,R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(适配器);
最终的ArrayList<串GT; myNewList =新的ArrayList<串GT;();
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
字符串项= lv.getItemAtPosition(ARG2)的ToString();
串itemordered;
itemordered =项目+添加到列表;
Toast.makeText(getApplicationContext(),itemordered,Toast.LENGTH_SHORT).show();
myNewList.add(项目);
}
}); //列表视图按钮
按钮btnLunchList =(按钮)findViewById(R.id.lrList);
btnLunchList.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
的setContentView(R.layout.selecteditems);
ListView控件selecteditems =(ListView控件)findViewById(android.R.id.list);
ArrayAdapter<串GT; newadapter =新ArrayAdapter<串GT;(LunchListMenu.this,android.R.layout.simple_list_item_1,myNewList);
selecteditems.setAdapter(newadapter); //获取SD卡的位置,所以我们可以创建目录和文件
文件SD卡= Environment.getExternalStorageDirectory();
文件午餐=新的文件(SD卡,午餐);
lunch.mkdirs();
档案文件=新的文件(午餐Lunch.txt);
PrintWriter的出= NULL;
尝试{
OUT =的新PrintWriter(新的FileWriter(文件));
}赶上(IOException异常五){
e.printStackTrace();
} //写入阵列中的每个串
StringBuilder的文本=新的StringBuilder();
对于(一个String:myNewList){
通过out.println(S);
}
out.close(); //读取文件
尝试{
BR的BufferedReader =新的BufferedReader(新的FileReader(文件));
串线;
而((行= br.readLine())!= NULL){
text.append(线);
text.append(,);
text.append('');
}
}
赶上(IOException异常五){
}
itemsordered =文本;
}
});
} 公共无效shareMyList(视图v){
//分享选的项目按
按钮btnShareItems =(按钮)findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
意图sendIntent =新意图(Intent.ACTION_VIEW);
sendIntent.putExtra(SMS_BODY,itemsordered);
sendIntent.setType(vnd.android-DIR / MMS短信);
startActivity(sendIntent);
}
});
}
}
I am very new to programming. I have an app that has several views. The Main view shows a list such as Breakfast, Lunch & Dinner. When a an item is selected, example Lunch, a list of lunch menu items is displayed such as Hamburger, Cheeseburger, French Fries... (this list is created from the string-array lunch_menu that is stored in \values\lunch.xml) as the user selects the items they want, it is stored in a new array called myNewList and is displayed whe the users presses the lunchList button. All of the items are displayed that the user selected. So far, So good. I created a android:onClick="shareMyList" in the selecteditems.xml and the button works, but does not populate my list. I think what i need is to some how convert it to a string, this is where I need help.
Here is my Problem now.... I have my Share button, that when pressed, I would like it to automatically open the default Messaging app and populate the list from the selected items ListView.
package com.mycompany.lunch;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class LunchListMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoarse);
final ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
final ArrayList<String> myNewList = new ArrayList<String>();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show();
myNewList.add(item);
}
});
// List View Button
Button btnLunchList = (Button) findViewById(R.id.lrList);
btnLunchList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.selecteditems);
ListView selecteditems = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);
selecteditems.setAdapter(newadapter);
}
});
}
public void shareMyList(View v){
// Share Selected Items Button
Button btnShareItems = (Button) findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));
}
});
}
}
Here is the Lunch Menu Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/main_background"
android:paddingLeft="10.0dip"
android:paddingTop="0.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="10.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/lrList"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_gravity="right"
android:background="@drawable/list" />
<ImageView
android:id="@+id/LunchMenuTitle"
android:contentDescription="@string/LunchMenu"
android:layout_width="0dip"
android:layout_height="72dip"
android:layout_weight="0.96"
android:background="@drawable/lunch"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="@drawable/head"
android:orientation="horizontal" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#FFCC00"
android:dividerHeight="2dp" >
</ListView>
<LinearLayout
android:orientation="horizontal"
android:background="@drawable/head"
android:layout_width="fill_parent"
android:layout_height="10.0dip" />
</LinearLayout>
And Here is my selecteditems.xml Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/main_background"
android:paddingLeft="10.0dip"
android:paddingTop="0.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="10.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/shareMyList"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_gravity="right"
android:onClick="shareMyList"
android:background="@drawable/share" />
<ImageView
android:id="@+id/selectedItemsTitle"
android:contentDescription="@string/LunchTitle"
android:layout_width="0dip"
android:layout_height="72dip"
android:layout_weight="0.96"
android:background="@drawable/title"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="@drawable/head"
android:orientation="horizontal" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#FFCC00"
android:dividerHeight="2dp"
android:padding="10dip"
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
</LinearLayout>
This may be the most kludgiest way of doing this, but it works.
package com.mycompany.lunch;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class LunchListMenu extends Activity {
String itemsordered;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoarse);
final ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
final ArrayList<String> myNewList = new ArrayList<String>();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show();
myNewList.add(item);
}
});
// List View Button
Button btnLunchList = (Button) findViewById(R.id.lrList);
btnLunchList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.selecteditems);
ListView selecteditems = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);
selecteditems.setAdapter(newadapter);
// Get sdCard location so we can Create Dir and File
File sdCard = Environment.getExternalStorageDirectory();
File lunch = new File(sdCard,"Lunch");
lunch.mkdirs();
File file = new File(lunch, "Lunch.txt");
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(file));
} catch (IOException e) {
e.printStackTrace();
}
// Write each string in the array
StringBuilder text = new StringBuilder();
for (String s : myNewList) {
out.println(s);
}
out.close();
// read File
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append(',');
text.append(' ');
}
}
catch (IOException e) {
}
itemsordered = text;
}
});
}
public void shareMyList(View v){
// Share Selected Items Button
Button btnShareItems = (Button) findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", itemsordered);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
});
}
}
这篇关于通过发送邮件应用程序的ListView结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!