I’ve created a listview with its own adapter, but now I have to implement the setOnClickListener method?
Reading my manual and searching on the net I’ve seen that it doesnt work normally in this case, can someone explain me how to use it properly, maybe with a example?
This is my MainActivity.java
package com.gabriele.progetti;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView textview;
ListView menu;
final static String[] Scelte = new String[] {"Una citazione a caso", "Cerca per autore", "Cerca per categoria", "Crediti"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Comincio tutti i magheggi per prendere il font e settarlo */
AssetManager assetManager = getResources().getAssets();
Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/stilo.otf");
textview = (TextView) findViewById(R.id.textView1);
textview.setTypeface(typeface);
/* Magheggi finiti */
menu = (ListView) findViewById(R.id.listView1);
//SetAdapter e non setlistAdapter perchè non sto usando ListActivity.
// Gli passo la classe MenuAdapter con un context proprio e la stringa Scelte.
menu.setAdapter(new MenuAdapter(this, Scelte));
}
}
And this is my MenuAdapter.java
package com.gabriele.progetti;
import java.util.List;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MenuAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] valori;
public MenuAdapter(Context context, String[] valori)
{
// TODO Auto-generated constructor stub
super(context, R.layout.activity_main, valori);
this.context = context;
this.valori = valori;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// Dico al service di Android di prepararsi ad una animazione (inflate = animare).
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Prendo tutte le View all'interno del file XML row_modificata e le metto nella ViewAnimata.
View ViewAnimata = inflater.inflate(R.layout.row_modificata, parent, false);
// Dichiaro un context perchè in un arrayadapter se voglio prendere un asset devo utilizzarlo per forza.
Context context1 = context;
AssetManager assetManager = context1.getResources().getAssets();
TextView textView = (TextView) ViewAnimata.findViewById(R.id.textView2);
Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/menu.ttf");
textView.setTypeface(typeface);
textView.setText(valori[position]); // Assegno un testo per ogni valore presente nella stringa che passo.
return ViewAnimata;
}
Thank you!
Add this code in MainActivity: