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>