Reputation: 47
this program makes a listview in relativelayout, Listview is coming fine, but when i click on a item it crashes (where a toast is expected) In debug mode, the code crashes on the bold line (i.e. RelativeLayout relativeLayoutChild = (RelativeLayout ) relativeLayoutParent.getChildAt(1);)
public class MainActivity extends Activity {
ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// URL to the JSON data
String strUrl = "http://10.0.2.2/android_conect/conn.php";
// Creating a new non-ui thread task to download json data
DownloadTask downloadTask = new DownloadTask();
// Starting the download process
downloadTask.execute(strUrl);
// Getting a reference to ListView of activity_main
mListView = (ListView) findViewById(R.id.lv_coordinates);
// Item Click Listener for the listview
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
// Getting the Container Layout of the ListView
RelativeLayout relativeLayoutParent = (RelativeLayout) container;
// Getting the inner Linear Layout
**RelativeLayout relativeLayoutChild = (RelativeLayout ) relativeLayoutParent.getChildAt(1);**
// Getting the Country TextView
TextView tvId = (TextView) relativeLayoutChild.getChildAt(0);
Toast.makeText(getBaseContext(), tvId.getText().toString(), Toast.LENGTH_SHORT).show();
}
};
// Setting the item click listener for the listview
mListView.setOnItemClickListener(itemClickListener);
}
the layout files are: activity_main.xml
<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" >
<ListView
android:id="@+id/lv_coordinates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" />
</RelativeLayout>
lv_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_coordinate_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_id" />
</RelativeLayout>
the error message is:
FATAL EXCEPTION: main
java.lang.ClassCastException:
android.widget.TextView cannot be cast to android.widget.RelativeLayout
at com.example.mylistview.MainActivity$1.onItemClick(MainActivity.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
at android.widget.AbsListView$1.run(AbsListView.java:3423)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Sending signal. PID: 2591 SIG: 9
Upvotes: 0
Views: 1041
Reputation: 3661
The View container
is your lv_layout.xml. relativeLayoutChild
is unnecessary, the children of relativeLayoutParent
would be your TextViews. You are trying to cast tv_coordinate_details
to a RelativeLayout.
Upvotes: 0
Reputation: 3195
Change the following line
**RelativeLayout relativeLayoutChild = (RelativeLayout ) relativeLayoutParent.getChildAt(1);**
to
**TextView tv_coordinate_details= (TextView ) relativeLayoutParent.getChildAt(1);**
TextView tvId = (TextView) relativeLayoutParent.getChildAt(0);
Upvotes: 1
Reputation: 33495
I think you should get your child widget from layout (row you selected) in this way:
TextView tvId = (TextView) container.findViewById(R.id.tv_id);
Second parameter in onItemClick()
method represents rowView (in your case lv_layout.xml). Each row has own lv_layout.xml layout.
So you have direct access to each child widget of each row via this method (it automatic returns whole rowView at clicked position).
Upvotes: 3