如何在setText中获取字符串的输出?

时间:2011-09-19 07:47:21

标签: android

这是我的代码......

package sortarray.com;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SortArray extends Activity {
    ArrayList<String[]> matchedFruits = new ArrayList<String[]>();
TextView selection;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String fruits[] = new String[7];//Sorted array
    fruits[0] = "apple";
    fruits[1] = "capricot";
    fruits[2] = "banana";
    fruits[3] = "mango";
    fruits[4] = "melon";
    fruits[5] = "pineapple";
    fruits[6] = "peach";
    char currChar=fruits[0].charAt(0);//Get first char of first element

    boolean match=false;
    int len=fruits.length;
    List<String> tmp = new ArrayList<String>();

    for(int i=1;i<len;i++)
    {
    Log.d("Comparing ", fruits[i].charAt(0)+","+currChar);
    if (fruits[i].charAt(0)==currChar)
    {
    if (match==false)//new match?
    {
    match=true;//Reset search
    tmp.clear();//clear existing items
    tmp.add(fruits[i-1]);
    Log.d("Started new list ", fruits[i-1]);
    }
    else
    {
    tmp.add(fruits[i-1]);
    Log.d("Added to list ", fruits[i-1]);
    }
    }
    else
    {
    match=false;
    tmp.add(fruits[i-1]);
    matchedFruits.add(tmp.toArray(new String[tmp.size()]));//add to final list
    Log.d("Finished a list ", fruits[i-1]);
    tmp.clear();//clear existing items

    }
    currChar=fruits[i].charAt(0);


    }
    tmp.add(fruits[len-1]);
    matchedFruits.add(tmp.toArray(new String[tmp.size()]));//add left over items
    printList();
    }

    void printList()
    {
    //Print the list 
    for(int i=0;i<matchedFruits.size();i++)
    {
    String tmp2[]= matchedFruits.get(i);
    for (int j=0;j<tmp2.length;j++)
    {
     //Log.d("Final list", "Array #"+i+"["+j+"],"+tmp2[j]);
        **//selection.setText(tmp2[j].toString());**
    }
    }
    }

    }

我想打印字符串的输出 使用selection.setText(); // selection.setText(tmp2 [j] .toString()); ** 但是不能这样做....

请帮助我..

2 个答案:

答案 0 :(得分:1)

您必须将您的java代码中的textViewR.layout.main中的<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tv" /> </LinearLayout> 绑定。

在布局中,main.xml:

TextView selection = (TextView) findViewById(R.id.tv);
String mssg;
for (int j=0;j<tmp2.length;j++)
{
    mssg += tmp2[j].toString());
}

selection.setText(mssg);

在java中:

{{1}}

答案 1 :(得分:0)

import java.util.ArrayList;
import java.util.List;

 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 import android.widget.TextView;

 public class TestActivity extends Activity {
ArrayList<String[]> matchedFruits = new ArrayList<String[]>();
TextView selection;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String fruits[] = new String[7];// Sorted array
    fruits[0] = "apple";
    fruits[1] = "capricot";
    fruits[2] = "banana";
    fruits[3] = "mango";
    fruits[4] = "melon";
    fruits[5] = "pineapple";
    fruits[6] = "peach";
    char currChar = fruits[0].charAt(0);// Get first char of first element

    boolean match = false;
    int len = fruits.length;
    List<String> tmp = new ArrayList<String>();

    for (int i = 1; i < len; i++) {
        Log.d("Comparing ", fruits[i].charAt(0) + "," + currChar);
        if (fruits[i].charAt(0) == currChar) {
            if (match == false)// new match?
            {
                match = true;// Reset search
                tmp.clear();// clear existing items
                tmp.add(fruits[i - 1]);
                Log.d("Started new list ", fruits[i - 1]);
            } else {
                tmp.add(fruits[i - 1]);
                Log.d("Added to list ", fruits[i - 1]);
            }
        } else {
            match = false;
            tmp.add(fruits[i - 1]);
            matchedFruits.add(tmp.toArray(new String[tmp.size()]));// add to
                                                                    // final
                                                                    // list
            Log.d("Finished a list ", fruits[i - 1]);
            tmp.clear();// clear existing items
enter code here
        }
        currChar = fruits[i].charAt(0);

    }
    tmp.add(fruits[len - 1]);
    matchedFruits.add(tmp.toArray(new String[tmp.size()]));// add left over
                                                            // items
    printList();

}

void printList() {
    // Print the list
// create obj of TextView 
    selection =new TextView(this);
    int k=0;
    for (int i = 0; i < matchedFruits.size(); i++) {
        String tmp2[] = matchedFruits.get(i);


        for (int j = 0; j < tmp2.length; j++) {
            Log.d("Final list", "Array #" + i + "[" + j + "]," + tmp2[j]);
            selection.setText(tmp2[j].toString());
  // set textview in contentview
            setContentView(selection);

        }
    }
}

}