BABLU KUMAR BABLU KUMAR - 4 months ago 15
Android Question

Closed - Toast message does not show up

In this code I want to show up the toast message but it does not show up on clicking. Probably, I have missed something. Could you figure out the problem.

MainActivity.java

package com.example.hacback17.listviewwithinflater;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);

//Arrayadapter is used to convert the data source into a view.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list, R.id.textView, days);
listView.setAdapter(adapter);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp = (TextView) view;
Toast.makeText(MainActivity.this, temp.getText() + " " + position, Toast.LENGTH_SHORT).show();
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hacback17.listviewwithinflater.MainActivity">


<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>


simple_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/textView" />
</LinearLayout>


Could you please also write a code snippet for LayoutInflater because I could not understand it properly as yet.
Thanks in advance!

After adding the missed part. The app crashes now. Please have a look at the logcat.

Process: com.example.hacback17.listviewwithinflater, PID: 19479
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at com.example.hacback17.listviewwithinflater.MainActivity.onItemClick(MainActivity.java:32)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1115)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2928)
at android.widget.AbsListView$3.run(AbsListView.java:3691)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)

Answer

try this way

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
         listView.setOnItemClickListener(this);

        //Arrayadapter is used to convert the data source into a view.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list, R.id.textView, days);
        listView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView temp = (TextView) view;
        Toast.makeText(this, temp.getText() + " " + position, Toast.LENGTH_SHORT).show();
    }
}
Comments