0

i try to make a list with Clickable strings from my string array. By clicking on any String i want to get to a classes named after this string. I tried with onListItemClick... but it doesnt work :S .. any suggestions for a solution?

Thank you for your time :)

I've got some Strings for example called "Title 1", "Title 2" and a little description under every title called "Description 1", "Description 2"... in a ListView. By clicking on "title" i want to get to a class named Title . How to do that with my construct?

Listview

Title 1 Description 1

Title 2 Description 2

Title 3 Description 3

ListviewEnd

package com.example.benice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {

    String[] titles; // String Array
    String[] beschreibung; // String Array
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Automatisch generierter Methodenstub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        Resources res = getResources();
        titles = res.getStringArray(R.array.titles);
        beschreibung = res.getStringArray(R.array.beschreibung);

        list = (ListView) findViewById(R.id.listView1);
        VivzAdapter adapter = new VivzAdapter(this, titles, beschreibung);
        list.setAdapter(adapter);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Automatisch generierter Methodenstub
        onListItemClick(l, v, position, id);
        String cheese = titles[position];
        try {
            Class ourClass = Class.forName("com.example.benice." + cheese);
            Intent ourIntent = new Intent(Main.this, ourClass);
            startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Automatisch generierter Methodenstub

    }

}

class VivzAdapter extends ArrayAdapter<String> {
    Context context;
    String[] titlesArray;
    String[] beschreibungArray;

    VivzAdapter(Context c, String[] titles, String[] beschreibung) {

        super(c, R.layout.single_row, R.id.titleTextView, titles);

        this.context = c;
        this.titlesArray = titles;
        this.beschreibungArray = beschreibung;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflator.inflate(R.layout.single_row, parent, false);

        TextView titles = (TextView) row.findViewById(R.id.titleTextView);
        TextView beschreibung = (TextView) row.findViewById(R.id.beschTextView);

        titles.setText(titlesArray[position]);
        beschreibung.setText(beschreibungArray[position]);
        return row;
    }

}

this is how the .xml looks like

<?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="90dp"
    android:background="@drawable/backgg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="5dp"
        android:text="Here is the Title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fefefe" />

    <TextView
        android:id="@+id/beschTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="Here is the Descrition"
        android:textColor="#fefefe" />

</LinearLayout>

3 Answers3

0

First of all you need to implement an AdapterView.OnItemClickListener not View.OnClickListener as you Activity is doing.

Then you need to also set this listener on your listview, like this for example:

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // your code here
    }
});

There might be other things wrong with your code, for example, Class.forName() is most likely going to throw exceptions if you're feeding it random strings from a list view, but without more detail about what exactly you're trying to do that's hard to say.

ci_
  • 8,594
  • 10
  • 39
  • 63
0

There's a few issues with your code, and i got a few tips for you as well.

The first issue, as to why ListClick event doesn't work. You never actually bind it to the listview.
To enable click events on your list, use this code:

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Put your code here
        }
    });

Also, you have implemented onClick in your activity, because you have 'implements OnClickListener'. Why? You dont use it, so just remove it.
I'm fairly certain this could also play a role in how click events are handled by your list click event handler. As the click event for the activity might get triggered first, and then stop the click event of the list from firing.

A few tips:
1) Write all your code in english, even variable names and comments. Makes is much easier for people to read (especially since you're asking for help on an english speaking forum)
2) You can use 'LayoutInflater.from(Context)' to get a LayoutInflater, instead of that long line of code where you use 'getSystemService'.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
0

It's possible to construct your ListView by extending ListActivity rather than Activity - onListItemClick will only work as it is used in you code if you extend ListActivity. You can also unimplement OnClickListener regardless of what you do. For instance:

public class MainActivity extends ListActivity {

String[] titles = { "One", "two", "three", "four", "five"};
String[] beschreibung = { "1", "2", "3", "4", "5"};

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

// VivzAdapter? Good to see another SlideNerd fan :)
    VivzAdapter adapter = new VivzAdapter(this, titles, beschreibung);
    setListAdapter(adapter);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String cheese = titles[position];
    try {
        Class ourClass = Class.forName("com.example.benice." + cheese);
        Intent ourIntent = new Intent(this, ourClass);
        startActivity(ourIntent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    }

This will work for as long as you have other Classes called "One", "two", "three" etc. You also need to alter the construction of your getView() method in the custom ArrayAdapter.

public View getView(int position, View convertView, ViewGroup parent) {
    View row;

    if (convertView == null) {
        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.row, parent, false);
    } else {
        row = convertView;
    }

    TextView titles = (TextView) row.findViewById(R.id.titleTextView);
    TextView beschreibung = (TextView) row.findViewById(R.id.beschTextView);

    titles.setText(titlesArray[position]);
    beschreibung.setText(beschreibungArray[position]);

    return row;
}

This post goes into more depth on onListItemClick.

Community
  • 1
  • 1
PPartisan
  • 8,173
  • 4
  • 29
  • 48