1

I'm at my first steps into Android... I already read some of the answered question, and I tool a lot of tips, but I don't find how to solve my problem.

In the activity Categories.java I have a listview built starting from a database. The listview displys correctly, then I would like to start another activity from the OnItemClick, but I realized that I can't display even a toast.

I really hope some of you can help me I really don't find where is my error!! Thank you in advance!

Here is my code:

package com.example.myshopping;

import java.util.ArrayList;

import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.app.Activity;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class Categories extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categories);
    final ListView lista= (ListView) findViewById(R.id.id_categorie);

    MioDatabaseHelper myHelper;

    myHelper= new MioDatabaseHelper(this);

    final SQLiteDatabase db= myHelper.getReadableDatabase();

    String [] columns_cat ={"nome_categoria"};

    Cursor cursor= db.query("categorie", columns_cat, null, null, null, null, null);

    int len= cursor.getCount();

    cursor.moveToFirst();

    final ArrayList <String> array_cat= new ArrayList<String>();

    for (int i=0; i<(len); ++i){
        array_cat.add(cursor.getString(0));
        cursor.moveToNext();
    }

    ArrayAdapter<String> myadapter= new ArrayAdapter<String>(this, R.layout.listitem, R.id.listitemTextView, array_cat);


    lista.setAdapter(myadapter);
    lista.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){


            Toast.makeText(getApplicationContext(), "hello toast!!!!", Toast.LENGTH_LONG).show();

            }

    });



    }

and here the xml file of the list items:

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

<TextView
    android:id="@+id/listitemTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:includeFontPadding="false"
    android:padding="5pt"
    android:scrollHorizontally="false"
    android:textSize="10pt" />

</LinearLayout>

and here the xml file of the categories.java file:

<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="wrap_content"
    android:gravity="center_horizontal"
    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=".Categories" >

    <ListView
        android:id="@+id/id_categorie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" 
        android:textIsSelectable="true">


    </ListView>

</RelativeLayout>
jessyjemy
  • 35
  • 5

3 Answers3

0

I see basic inconsistency in your code:

R.layout.categories//in set content view

and in array adapter:

 ArrayAdapter<String> myadapter= new ArrayAdapter<String>(this, R.layout.listitem, R.id.listitemTextView, array_cat);

Change R.layout.listitem to R.layout.categories or use a different constructor.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0

Is android:textIsSelectable="true" and android:descendantFocusability="blocksDescendants" attributes needed in your case? Using them wrong can cause focusing problems and the click listener doesn't work. Try removing both.

Also the list item contains only one child for the LinearLayout so it could just be TextView having width set to match_parent and height wrap_contentcombined with padding.

Niko
  • 8,093
  • 5
  • 49
  • 85
0

Also if you want to specyfy textView appearance Create XML Just with that textView without parent Noode, coz lists can be ricky sometimes :P try create another XML just to test that one thing
Change this:
<relativeLayout> attributes.. <textView> attributes.. </textView> </relativeLayout>
To
<TextView> attributes </TextView>
Basicaly you want stand alone TextView

user3455363
  • 408
  • 1
  • 5
  • 13