Autore Topic: problema listview con adapter custom  (Letto 831 volte)

Offline gbu123

  • Utente junior
  • **
  • Post: 74
  • Respect: +2
    • Mostra profilo
  • Sistema operativo:
    windows 7
problema listview con adapter custom
« il: 27 Agosto 2012, 17:36:36 CEST »
0
ciao
chiedo scusa , non riesco a risolvere l'errore seguente :

ho definito gli xml seguenti :

listimages.xml :
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidrientation="vertical" >

<Button
android:id="@+id/ritorna"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="torna" />

<listView android:id="@+id/listimages"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</listView>

</LinearLayout>

listimages_riga.xml :
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listimages_riga"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="horizontal" >

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/listimages_riga_image"
android:layout_marginLeft="14dp"
android:layout_weight="0.15" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox"
android:layout_alignBottom="@+id/checkBox"
android:layout_marginLeft="19dp"
android:layout_toRightOf="@+id/checkBox"
android:text="TextView" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/foto1" />

</RelativeLayout>


il 2° xml descrive la riga del 1° xml , i 2 xml sono gestiti dall'activity ListImagesAct ;

l'activity seguente listaVociAct visualizza una listview e , selezionando una delle sue righe , dovrebbe chiamare l'activity ListImagesAct che prepara il 1° xml secondo il tracciato del 2° xml :


Codice (Java): [Seleziona]
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class listaVociAct extends Activity {

protected Context mContext;
protected ArrayList<String> mat1;
private ListView listav ;
private ArrayAdapter<String> listAdapt ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listavoci);
mContext = this;

Intent int2 = getIntent();
mat1 = int2.getStringArrayListExtra("matrice1");

// Find the ListView , lista delle voci selezionabili
listav = (ListView) findViewById( R.id.listavoci );


// Collegamento con la sorgente dati da visualizzare.
listAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mat1);

// Set the ArrayAdapter as the ListView's adapter.
listav.setAdapter( listAdapt );

//controlla elemento della lista selezionato
listav.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
selectActivity(position);
}

// Metodo selezione , per semplicita faccio tutto uguale
private void selectActivity(int position) {
switch (position) {
case 0:
Intent myIntent1 = new Intent(listaVociAct.this,ListImagesAct.class);
myIntent1.putStringArrayListExtra("matrice1", mat1);
listaVociAct.this.startActivity(myIntent1);
break;
case 1:
Intent myIntent2 = new Intent(listaVociAct.this,ListImagesAct.class);
myIntent2.putStringArrayListExtra("matrice1", mat1);
listaVociAct.this.startActivity(myIntent2);
break;
case 2:
. . . . . .
}
});

}

l'activity ListImagesAct è così fatta :

Codice (Java): [Seleziona]
package com.example.pippo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class ListImagesAct extends Activity {

protected Context mContext;
protected ArrayList<String> mat1;
private ListView listav ;
private myAdapter mioAdapt ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listimages);
mContext = this;

Intent int2 = getIntent();
mat1 = int2.getStringArrayListExtra("matrice1");

// Find the resource : listView del 1° xml
listav = (ListView) findViewById( R.id.listimages );

// Collegamento con la sorgente dati da visualizzare , uso un adapter mio
mioAdapt = new myAdapter(this, mat1);

// Set the adapter.
listav.setAdapter( mioAdapt );

//controllo elemento della lista selezionato
listav.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
selectActivity(position);
}

// Metodo selezione , per semplicità torno indietro
private void selectActivity(int position) {
switch (position) {
case 0:
Intent myIntent1 = new Intent(ListImagesAct.this,listaVociAct.class);
myIntent1.putStringArrayListExtra("matrice1", mat1);
ListImagesAct.this.startActivity(myIntent1);
break;
case 1:
. . . . .
}
}
});

}

}

mioAdapter è fatto così :

Codice (Java): [Seleziona]
package com.example.pippo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class myAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;

// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.foto1, R.drawable.foto2,
R.drawable.foto3, R.drawable.foto4,
R.drawable.foto5, R.drawable.foto6,
R.drawable.foto7, R.drawable.foto8,
R.drawable.foto9
};

public myAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.listimages_riga, values);
this.context = context;
this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
View rowView = inflater.inflate(R.layout.listimages_riga, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.checkBox);

textView.setText(values.get(position));
imageView.setImageResource(mThumbIds[position]);

return rowView;
}
}

,'esecuzione mi da il seguente errore , ho provato a fare il debug ma non ho ricavato niente ,
forse perchè sono poco esperto :


Codice: [Seleziona]
08-27 14:37:34.999: D/dalvikvm(607): GC_CONCURRENT freed 89K, 3% free 9385K/9607K, paused 7ms+9ms
08-27 14:37:56.070: D/AndroidRuntime(607): Shutting down VM
08-27 14:37:56.070: W/dalvikvm(607): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-27 14:37:56.138: E/AndroidRuntime(607): FATAL EXCEPTION: main
08-27 14:37:56.138: E/AndroidRuntime(607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pippo/com.example.pippo.ListImagesAct}: android.view.InflateException: Binary XML file line #15: Error inflating class listView
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1956)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:1981)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.ActivityThread.access$600(ActivityThre ad.java:123)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1147)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.os.Handler.dispatchMessage(Handler.java:99 )
08-27 14:37:56.138: E/AndroidRuntime(607): at android.os.Looper.loop(Looper.java:137)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.ActivityThread.main(ActivityThread.jav a:4424)
08-27 14:37:56.138: E/AndroidRuntime(607): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:37:56.138: E/AndroidRuntime(607): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 14:37:56.138: E/AndroidRuntime(607): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:784)
08-27 14:37:56.138: E/AndroidRuntime(607): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:551)
08-27 14:37:56.138: E/AndroidRuntime(607): at dalvik.system.NativeStart.main(Native Method)
08-27 14:37:56.138: E/AndroidRuntime(607): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class listView
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.createViewFromTag(Layo utInflater.java:691)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.rInflate(LayoutInflate r.java:739)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.inflate(LayoutInflater .java:489)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.inflate(LayoutInflater .java:396)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.inflate(LayoutInflater .java:352)
08-27 14:37:56.138: E/AndroidRuntime(607): at com.android.internal.policy.impl.PhoneWindow.setCo ntentView(PhoneWindow.java:251)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.Activity.setContentView(Activity.java: 1835)
08-27 14:37:56.138: E/AndroidRuntime(607): at com.example.pippo.ListImagesAct.onCreate(ListImage sAct.java:31)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.Activity.performCreate(Activity.java:4 465)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1049)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1920)
08-27 14:37:56.138: E/AndroidRuntime(607): ... 11 more
08-27 14:37:56.138: E/AndroidRuntime(607): Caused by: java.lang.ClassNotFoundException: android.view.listView
08-27 14:37:56.138: E/AndroidRuntime(607): at dalvik.system.BaseDexClassLoader.findClass(BaseDex ClassLoader.java:61)
08-27 14:37:56.138: E/AndroidRuntime(607): at java.lang.ClassLoader.loadClass(ClassLoader.java:5 01)
08-27 14:37:56.138: E/AndroidRuntime(607): at java.lang.ClassLoader.loadClass(ClassLoader.java:4 61)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.createView(LayoutInfla ter.java:552)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.onCreateView(LayoutInf later.java:636)
08-27 14:37:56.138: E/AndroidRuntime(607): at com.android.internal.policy.impl.PhoneLayoutInflat er.onCreateView(PhoneLayoutInflater.java:66)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.onCreateView(LayoutInf later.java:653)
08-27 14:37:56.138: E/AndroidRuntime(607): at android.view.LayoutInflater.createViewFromTag(Layo utInflater.java:678)
08-27 14:37:56.138: E/AndroidRuntime(607): ... 21 more
08-27 14:37:56.488: I/dalvikvm(607): threadid=3: reacting to signal 3
08-27 14:37:56.678: I/dalvikvm(607): Wrote stack traces to '/data/anr/traces.txt'
08-27 14:37:56.878: I/dalvikvm(607): threadid=3: reacting to signal 3
08-27 14:37:56.899: I/dalvikvm(607): Wrote stack traces to '/data/anr/traces.txt'
« Ultima modifica: 27 Agosto 2012, 17:57:43 CEST da Ricky`, Reason: codice formattato »

Offline Ricky`

  • Amministratore
  • Utente storico
  • *****
  • Post: 3489
  • Respect: +507
    • Github
    • Google+
    • rciovati
    • Mostra profilo
Re:problema listview con adapter custom
« Risposta #1 il: 27 Agosto 2012, 17:58:44 CEST »
0
Quando posti il codice prima devi formattarlo altrimenti non si capisce niente. In Eclipse basta fare ctrl + alt + f.

Comunque, nel layout hai messo listview al posto di Listview.

Zate

  • Visitatore
Re:problema listview con adapter custom
« Risposta #2 il: 27 Agosto 2012, 21:01:51 CEST »
0
La L di ListView deve essere maiuscola
EDIT: Non mi ero accorto che aveva risposto anche Ricky .-. Scusate

Offline gbu123

  • Utente junior
  • **
  • Post: 74
  • Respect: +2
    • Mostra profilo
  • Sistema operativo:
    windows 7
Re:problema listview con adapter custom
« Risposta #3 il: 28 Agosto 2012, 11:18:55 CEST »
0
grazie mille, sono proprio un tordo
ciao

P.S. scusa , ho notato che l'ultimo elemento della lista ha solo l'immagine , mancano il checkbox e la textview ,
c'è qualche motivo ?
« Ultima modifica: 28 Agosto 2012, 16:21:45 CEST da gbu123 »