2016-10-09 4 views
0

カスタムリストビューを実行していて、リストビューのいくつかの要素をクリックしたときに別のクラスを開きたいとします。カスタムリストビューを使用してアクティビティを開始しようとするとエラーが発生する

アダプタ、クラス、アクティビティ、および.xmlファイルを開始しました。

クラス:

public class DatosMercado { 
protected Drawable foto; 
protected String titulo; 
protected String info; 
protected long id; 

public DatosMercado(Drawable foto, String titulo, String info){ 
    this.foto = foto; 
    this.titulo = titulo; 
    this.info = info; 


} 

public Drawable getFoto(){ 
    return foto; 
} 

public void setFoto(Drawable foto){ 
    this.foto = foto; 
} 
public String getTitulo(){ 
    return titulo; 
} 

public void setTitulo(String titulo){ 
    this.titulo = titulo; 
} 

public String getInfo(){ 
    return info; 
} 

public void setInfo(String info){ 
    this.info = info; 
} 

public long getId(){ 
    return id; 
} 
public void setId(long id){ 
    this.id = id; 
} 

}

アダプタ:

public class AdapterDatosMercado extends BaseAdapter { 

protected Activity activity; 
//Asignamos la clase DatosMercado al arrayadapter 
protected ArrayList<DatosMercado> items; 

//Metodo constructor de la clase 
public AdapterDatosMercado(Activity activity, ArrayList<DatosMercado> items){ 
    this.activity = activity; 
    this.items = items; 
} 
@Override 
public int getCount() { 
    return items.size(); 
} 

@Override 
public Object getItem(int position) { 
    return items.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return items.get(position).getId(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 
    //Asociar el Layout de la lista que hemos creado 
    if(convertView == null){ 
     LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inf.inflate(R.layout.mercado2, null); 
    } 
    //Creamos un objeto de la clase DatosMercado 
    DatosMercado datos = items.get(position); 
    //Relleno la foto 
    ImageView foto = (ImageView) v.findViewById(R.id.Photo); 
    foto.setImageDrawable(datos.getFoto()); 
    //Rellenamos el nombre 
    TextView titulo = (TextView) v.findViewById(R.id.Title); 
    titulo.setText(datos.getTitulo()); 
    //Rellenamos la informacion 
    TextView info = (TextView) v.findViewById(R.id.Description); 
    info.setText(datos.getInfo()); 

    //retornamos la vista 
    return v; 
} 

}

そして、私が活動に問題を抱えている、私はそのコードをexectue場合は、エラーは述べています:

アクティビティを開始できませんComponentInfo {edu.upc.com.game/edu.upc.com.game.Mercado}:java.lang.RuntimeException:コンテンツには、id属性が 'android.R.id.list'のListViewが必要です「ここ

は私のコードです:

public class Mercado extends ListActivity { 

String classnames[] = {"BuscarColtan"}; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mercado); 

    ListView lista = (ListView) findViewById(R.id.listamoviles); 
    ArrayList<DatosMercado> arraydatos = new ArrayList<DatosMercado>(); 
    DatosMercado datos; 

    //Datos introducidos 

    datos = new DatosMercado(getResources().getDrawable(R.drawable.ipear), "IPear", "La mejor marca del mundo"); 
    arraydatos.add(datos); 
    datos = new DatosMercado(getResources().getDrawable(R.drawable.ipear), "Bensung", "Moviles Star de alta gama"); 
    arraydatos.add(datos); 
    datos = new DatosMercado(getResources().getDrawable(R.drawable.ipear), "HiYou", "Venimos desde China a dar el salto al mercado europeo"); 
    arraydatos.add(datos); 
    datos = new DatosMercado(getResources().getDrawable(R.drawable.ipear), "IQ", "Moviles de alta tecnologia a bajo coste"); 
    arraydatos.add(datos); 
    datos = new DatosMercado(getResources().getDrawable(R.drawable.ipear), "Zonya", "No te resistiras a comprar nuestros Hesperia"); 
    arraydatos.add(datos); 
    datos = new DatosMercado(getResources().getDrawable(R.drawable.ipear), "Hullaguey", "Los mobiles mas baratos"); 
    arraydatos.add(datos); 


    //Crear el Adapter personalizado 
    AdapterDatosMercado adapter = new AdapterDatosMercado(this,arraydatos); 

    //Aplicarlo 
    lista.setAdapter(adapter); 




} 

protected void onListItemClick(ListView lv, View v, int position, long id){ 
    super.onListItemClick(lv,v,position,id); 
    String openClass = classnames[position]; 
    try { 
     Class selected = Class.forName("edu.upc.com.game. "+ openClass); 
     Intent selectedIntent = new Intent(this,selected); 
     startActivity(selectedIntent); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

}

リストビューの元の名前は次のとおりです。

android:id="@+id/listamoviles" 

私はそれを変更しました

android:id="@android:id/list" 

しかし、今、私はそのラインに問題がある:

ListView lista = (ListView) findViewById(R.id.listamoviles); 

は、私はそれが動作しないことを実行しない場合は、そのリストビュー原因を定義する必要があります。 onListItemClickを削除してActivityへのListActivityを変更するとアプリケーションが動作しますが、カスタムリストビューをクリックして別のアクティビティ(クラス)を開きたいとします。

ありがとうございます!あなたはListActivity によって拡張する場合

答えて

0

することは、その後

ListView lista = (ListView) findViewById(R.id.listamoviles); 

この行を削除するにも、問題があれば、私は変更するということですので、それは失敗し続け

setListAdapter(adapter); 
+0

によって

lista.setAdapter(adapter); 

を置き換えます'android:id = "@ android:id/list" '行:' ListView lista =(リストビュー)findViewB yId(R.id.listamoviles); 'それはうまく動作せず、listamovilesの名前を変更する必要がありますが、名前のように/ listを置くことはできません。 –

+0

それは動作するかどうか? –

関連する問題