2012-07-26 39 views
0

我對編程非常陌生。我有一個有幾個視圖的應用程序。主視圖顯示了一份清單,如早餐,午餐&晚餐。當選擇一個項目時,例如午餐,會顯示午餐菜單項目列表,如漢堡包,芝士漢堡,薯條......(該列表是從存儲在\ values \ lunch.xml中的字符串數組lunch_menu創建的)當用戶選擇他們想要的項目時,它被存儲在一個名爲myNewList的新數組中,並且當用戶按下lunchList按鈕時被顯示。顯示用戶選擇的所有項目。到現在爲止還挺好。我在selecteditems.xml中創建了一個android:onClick =「shareMyList」,該按鈕可以工作,但不會填充我的列表。我想我需要的是如何將其轉換爲字符串,這是我需要幫助的地方。通過消息應用程序發送ListView結果

這是我的問題現在....我有我的共享按鈕,當按下時,我想它會自動打開默認的消息應用程序,並從選定的項目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> 
+0

沒有人有任何線索? – Sobo 2012-07-27 22:52:07

回答

0

這可能是這樣做的最kludgiest的方式,但它的作品。

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

對不起,這麼晚了。但是,當然,您需要將值存儲在某個地方以重用它們......我建議您使用SQLite。但就你而言,這並不是太有必要,因爲你沒有節省大量的價值。因此,可以像寫完一樣寫入文本文件來處理它。 – yahya 2012-07-30 05:25:57

相關問題