想要查看数组

时间:2014-07-29 11:51:12

标签: android arrays textview

在按钮上单击我想在同一活动中用我的数组填充textview 我有CSV文件路径,我已经接收到两个数组,一个有数字,一个有名称,现在我希望那些数字和名称数组在同一个活动中查看

public void uploadfile(View view) {
    edittext = (EditText)findViewById(R.id.txtFile);
    Toast.makeText(NewMessage.this, FullPath, 2000).show();
    String[] name = null;
    String[] number = null;
    if(FullPath != null) {
        /* CSV UPLOADER */
        try {
            File csvfile = new File(FullPath);
            FileInputStream csvStream = new FileInputStream(csvfile);
            BufferedReader in = new BufferedReader(new InputStreamReader(csvStream));
            String line;

            int iCount=0;
            while ((line = in.readLine()) != null) {
                        String[] RowData = line.split(",");
                        name[iCount] = RowData[0];
                        number[iCount] = RowData[1];
                        iCount++;
            }
            in.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        /*CSV Uploader Finish*/
        /*View CSV into an array*/
        int arraySize = name.length;
        /* Not getting Idea From Here How to fill text view with My array number and name */

    }
}

1 个答案:

答案 0 :(得分:0)

以下是如何连接数组并从中创建String以在TextView中使用的示例。您需要对其进行修改以满足您的需求。

final int[] numbers = new int[] { 1, 2, 3, 4, 5, 6 };
final String[] names = new String[] { "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6" };

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("Show number and name data:");
stringBuilder.append("\n\n");

for (int counter = 0; counter < numbers.length; counter++) {
    stringBuilder.append("number:").append(String.valueOf(numbers[counter]));
    stringBuilder.append("\n");
    stringBuilder.append("name:").append(String.valueOf(names[counter]));
    stringBuilder.append("\n\n");
}

String stringForTextView = stringBuilder.toString();

Log.d(TAG, "TextView data: " + stringForTextView);

编辑:我决定加入一个可以使用的工作示例

MainActivity.java

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

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

        final TextView arrayData = (TextView) findViewById(R.id.arrayData);

        final int[] numbers = new int[] { 1, 2, 3, 4, 5, 6 };
        final String[] names = new String[] { "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6" };

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("Show number and name data:");
        stringBuilder.append("\n\n");

        for (int counter = 0; counter < numbers.length; counter++) {
            stringBuilder.append("number:").append(String.valueOf(numbers[counter]));
            stringBuilder.append("\n");
            stringBuilder.append("name:").append(String.valueOf(names[counter]));
            stringBuilder.append("\n\n");
        }

        String stringForTextView = stringBuilder.toString();

        arrayData.setText(stringForTextView);
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/arrayData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

希望这有帮助