【问题标题】:Send ListView results via Messaging application通过消息应用程序发送 ListView 结果
【发布时间】:2012-07-25 08:26:52
【问题描述】:

我对编程很陌生。我有一个有多个视图的应用程序。主视图显示早餐、午餐和晚餐等列表。 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 ) 当用户选择他们想要的项目时,它存储在一个名为 myNewList 的新数组中,并在用户按下 lunchList 按钮时显示。显示用户选择的所有项目。到目前为止,一切都很好。我在 selecteditems.xml 中创建了一个 android:onClick="shareMyList" 并且该按钮有效,但没有填充我的列表。我认为我需要一些如何将其转换为字符串的方法,这就是我需要帮助的地方。

这是我现在的问题......我有我的分享按钮,当按下它时,我希望它自动打开默认的消息应用程序并从所选项目 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"));
                            }


                        });
                    }
}

这是午餐菜单布局

<?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>

这是我的 selecteditems.xml 布局

<?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>

【问题讨论】:

    标签: android string listview android-arrayadapter arrays


    【解决方案1】:

    这可能是最笨拙的方法,但它确实有效。

    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);
                                }
                            });
                        }
    }
    

    【讨论】:

    • 抱歉这么晚了。但是,是的,您肯定需要将值存储在某个地方以重用它们……我建议您使用 SQLite。但是在您的情况下,这并不是那么必要,因为您没有保存很多值。所以它可以像你做的那样写成一个文本文件来处理。
    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    相关资源
    最近更新 更多