Skip to content Skip to sidebar Skip to footer

Want To View Array

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 tho

Solution 1:

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 = newint[] { 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

publicclassMainActivityextendsActivity{

    publicstaticfinalString TAG = MainActivity.class.getSimpleName();

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

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

        finalint[] numbers = newint[] { 1, 2, 3, 4, 5, 6 };
        finalString[] names = newString[] { "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

<LinearLayoutxmlns: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}" ><Viewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><TextViewandroid:id="@+id/arrayData"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp" /><Viewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /></LinearLayout>

Hope this helps

Post a Comment for "Want To View Array"