要查看阵列 [英] Want to view Array

查看:145
本文介绍了要查看阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在点击链接,我想与我相同的活动阵列,以填补的TextView
我有我已经采取了两个数组一个用数字和一个带名字现在我想这些编号和名称数组中的同一个活动来查看CSV文件的路径。

 公共无效uploadfile(查看视图){
    的EditText =(EditText上)findViewById(R.id.txtFile);
    Toast.makeText(NewMessage.this,完整路径,2000年).show();
    的String []名= NULL;
    的String []号= NULL;
    如果(FULLPATH!= NULL){
        / * CSV UPLOADER * /
        尝试{
            文件csvfile =新的文件(完整路径);
            的FileInputStream csvStream =新的FileInputStream(csvfile);
            在的BufferedReader =新的BufferedReader(新的InputStreamReader(csvStream));
            串线;            INT ICOUNT = 0;
            而((行= in.readLine())!= NULL){
                        串[] RowData = line.split(,);
                        命名[ICOUNT] = RowData [0];
                        号码[ICOUNT] = RowData [1];
                        ICOUNT ++;
            }
            附寄();
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
        / * CSV上传完成* /
        / *查看CSV到一个数组* /
        INT ARRAYSIZE = name.length;
        / *没有得到思想从这里如何填写文本视图与我的序列号和名称* /    }
}


解决方案

下面是如何连接的阵列,并创建一个字符串,从它在你的TextView用一个例子。您需要修改它以满足您的需求。

 最终诠释[]号= INT新[] {1,2,3,4,5,6};
最终的String [] =名新的String [] {测试1,测试2,实验3,测试4,测试5,试验6};StringBuilder的StringBuilder的=新的StringBuilder();stringBuilder.append(显示号码和姓名数据:);
stringBuilder.append(\\ n \\ n);对于(INT计数器= 0;反< numbers.length;反++){
    stringBuilder.append(编号)。追加(将String.valueOf(编号[计数器]));
    stringBuilder.append(\\ n);
    。stringBuilder.append(名称:)追加(将String.valueOf(名称[计数器]));
    stringBuilder.append(\\ n \\ n);
}串stringForTextView = stringBuilder.toString();Log.d(TAG,TextView的数据:+ stringForTextView);

编辑:我已经决定要包括一个可以工作的例子,你可以使用

MainActivity.java

 公共类MainActivity延伸活动{    公共静态最后的字符串标记= MainActivity.class.getSimpleName();    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        最终的TextView arrayData =(的TextView)findViewById(R.id.arrayData);        最终诠释[]号码=新INT [] {1,2,3,4,5,6};
        最终的String [] =名新的String [] {测试1,测试2,实验3,测试4,测试5,试验6};        StringBuilder的StringBuilder的=新的StringBuilder();        stringBuilder.append(显示号码和姓名数据:);
        stringBuilder.append(\\ n \\ n);        对于(INT计数器= 0;反< numbers.length;反++){
            stringBuilder.append(编号)。追加(将String.valueOf(编号[计数器]));
            stringBuilder.append(\\ n);
            。stringBuilder.append(名称:)追加(将String.valueOf(名称[计数器]));
            stringBuilder.append(\\ n \\ n);
        }        串stringForTextView = stringBuilder.toString();        arrayData.setText(stringForTextView);
    }
}

activity_main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:重力=CENTER_HORIZONTAL
    机器人:方向=垂直
    工具:上下文=$ {的packageName} $ {} activityClass。>    <查看
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1/>    <的TextView
        机器人:ID =@ + ID / arrayData
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =20SP/>    <查看
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1/>< / LinearLayout中>

希望这有助于

On button click i want to fill the textview with my array in same activity I have CSV file path which i have taken in to two arrays one with number and one with name now i want those number and name array to view in the same activity

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 */

    }
}

解决方案

Here is an example of how to concatenate your arrays and create a String from it to use in your TextView. You need to modify it to cater for your needs.

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

Edit: I've decided to include a working example you can use

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>

Hope this helps

这篇关于要查看阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆