0

I build an android app with the help of an tutorial.

This is the MainActivity.java:-

package com.example.listdisplay;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    // Array of strings...
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
            "WebOS","Ubuntu","Windows7","Max OS X", "PHP", "Java", "HTML", "CSS", "JavaScript","MySQL"};

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

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}

This is the 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:orientation="vertical">

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

This is the activity_listview.xml:-

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:orientation="horizontal">
</TextView>

When I change the textview to any other view like cardview and more things. The app is going to crashed. I want to do more styling with cardview and layout. Like i also tried with the linearlayout.I inserted this textview with an button in linear layout. But, it is still going to crash.

Please help me.

Shivam Jha
  • 13
  • 1
  • For use CardView use RecycleView instead ListView – Style-7 Oct 14 '20 at 13:22
  • please share text crash – Javad Dehban Oct 14 '20 at 15:38
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Oct 15 '20 at 02:20

3 Answers3

1

You can't change the TextView from activity_listview.xml because ArrayAdapterconstructor expects a Layout with an only EditText.

If you want to modify that, you have to implement a custom ArrayAdaptermaking a class that extends from it.

You can follow next Medium tutorial to achieve this. https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd

If you want to add a Button or something else, you need the custom ArrayAdapter.

Bogdan Android
  • 1,345
  • 2
  • 10
  • 21
0

If you want to do more styling with card view so you don't need change text view to card view just simply write your code like below code...

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardCornerRadius="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp">
            <TextView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/label"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:padding="5dip"
             android:textSize="16dip"
             android:textStyle="bold"
             android:orientation="horizontal">
             </TextView>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
Faizan
  • 233
  • 3
  • 9
0

Create your custom xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="This is just text" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

Important part is android:id="@+id/label" in TextView.

And add the R.id.label to third parameter of ArrayAdapter, like that:

ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, R.id.label, mobileArray);
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19