2015-08-24 30 views
-3

我想顯示在另一活動的一個列表視圖中輸入的所有值,並且在單擊該按鈕後在單行以前活動的文本視圖顯示如何將列表視圖中的所有值傳遞到單行中另一個活動的文本視圖

這裏是我的代碼添加到列表視圖:

public class AddAttendees extends Activity{ 

    AutoCompleteTextView et; 
    Button b, Add; 
    ListView lv; 
    ArrayList<String> al; 
    ArrayAdapter<String> aa; 
    int position; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.attendees); 

     Add = (Button) findViewById(R.id.button2); 
     et = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 
     b = (Button) findViewById(R.id.button1); 
     lv = (ListView) findViewById(R.id.listView1); 
     al = new ArrayList<String>(); 
     aa = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       al); 
     lv.setAdapter(aa); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      @SuppressLint("ShowToast") 
      @Override 
      public void onItemClick(AdapterView<?> parent, 
        View v, int arg2, 
        long arg3) { 
       String item = al.get(arg2); 
       Toast.makeText(getApplicationContext(), item, 0).show(); 
      } 
     }); 

     b.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String item = et.getText().toString(); 
       al.add(0, item); 

       aa.notifyDataSetChanged(); 

       et.setText(""); 
      } 
     }); 

     Add.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v){ 
       Intent intent = new Intent(getApplicationContext(), MeetingAgenda.class); 

       String name = al.getText().toString(); 
       intent.putExtra("name", name); 
        /* Start LoginSuccess Activity */ 
        finish(); 


      } 

     }); 

    } 
} 

後點擊添加按鈕的項目添加到列表視圖將顯示在這TextView的在之前的活動。

+0

到目前爲止你有嘗試過什麼嗎? – PPartisan

+0

看到如何問:http://stackoverflow.com/help/how-to-ask –

回答

-2

使用靜態變量,經過從列表視圖整個陣列,將其添加到靜態變量,然後在TextView中像

tv.setText(NameOfYouStaticClass.yourVariable); 
+0

哪個變量列表視圖或陣列適配器 –

0

我不知道我得到的意思顯示它,但我想你只需要在單個電視的行中顯示值。

所以,你有一個活動,把它ActivityWithTextView,第二個(稱之爲ActivityWithListBox

不要在ActivityWithListBox

string text = ""; 
foreach(var listBoxItem in ListBox){ 
    text += listBoxItem; 
    //if you want to add a separator do it here 
    //for example 
    text += ", "; 
} 
//now you got a string with item1, item2, item3, 
//but it end with ", " 
//so remove it: 
if(text != ""){ 
    text = text.substring(0, text.length - 3); 
    //-3 because 2 is length of ", " 
    //and 1 is for the difference between length and index 
    //(length 2 means last index is 1) 
} 
//now set text: 
ActivityWithTextView.TextViewName.setText(text); 

以下,和你做。

我認爲這應該工作,我從這裏寫了而沒有嘗試。如果你有問題告訴我:)

0
  1. 定義在xml中填充適配器的arrayExample
  2. 使用StringBuilder類來連接一個for循環中的數組元素連接成一個單一的String

    StringBuilder temp = new StringBuilder(); 
    String[] array = getResources().obtainTypedArray(R.array.*array_id*); 
    String tempToString; 
    
    for (String element: array) { 
        temp.append(element); 
        temp.append(", "); 
    } 
    
    tempToString = temp.toString(); 
    
  3. 添加到您的TextViewonClick()Example
相關問題