2013-09-23 54 views
0

嗨我有問題與自定義列表視圖與列表活動 我想要當我從列表查看項目然後新的活動將開始。自定義列表視圖開始新的活動

Entitiy

package com.custom.listview; 

public class EntitasRestoran { 
    String namaresto = ""; 
    String alamatresto = ""; 
    int pic; 

    public String getNamaResto() { 
     return namaresto; 
    } 

    public void setNamaResto(String n) { 
     this.namaresto = n; 
    } 

    public String getAlamatResto() { 
     return alamatresto; 
    } 

    public void setAlamatResto(String a) { 
     this.alamatresto = a; 
    } 

    public int getPicResto() { 
     return pic; 
    } 

    public void setPicResto(int p) { 
     this.pic = p; 
    } 
} 

定義適配器

package com.custom.listview; 

import java.util.ArrayList; 

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

public class CustomBaseAdapter extends BaseAdapter { 
    private static ArrayList<EntitasRestoran> restoran; 

    private LayoutInflater mInflater; 

    public CustomBaseAdapter(Context context, ArrayList<EntitasRestoran> res) { 
     restoran = res; 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() { 
     return restoran.size(); 
    } 

    public Object getItem(int position) { 
     return restoran.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater 
        .inflate(R.layout.custom_item_listview, null); 
      holder = new ViewHolder(); 
      holder.nama = (TextView) convertView 
        .findViewById(R.id.namarestoran); 
      holder.alamat = (TextView) convertView 
        .findViewById(R.id.alamatrestoran); 
      holder.im = (ImageView) convertView.findViewById(R.id.img); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.nama.setText(restoran.get(position).getNamaResto()); 
     holder.alamat.setText(restoran.get(position).getAlamatResto()); 
     holder.im.setBackgroundResource(restoran.get(position).getPicResto()); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView nama; 
     TextView alamat; 
     ImageView im; 
    } 
} 

主要活動

package com.custom.listview; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.ListView; 

public class MainActivity extends Activity { 
    ListView lv; 
    ArrayList<EntitasRestoran> restoran = new ArrayList<EntitasRestoran>(); 
    EntitasRestoran entitasrestoran; 

    String[] arrayRestoran = { "Kare Ayam", "Spesial Sambal", "Waroeng Steak", 
      "Hoka Bento" }; 
    String[] arrayAlamat = { "Jalan Kartini", "Jalan Bimo", "Jalan Arjuno", 
      "Jalan Merdeka" }; 
    int[] ArrayPicRestoran = { R.drawable.kareayam, R.drawable.ss, 
      R.drawable.ws, R.drawable.hokben }; 

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

     lv = (ListView) findViewById(R.id.listView1); 

     for (int i = 0; i < arrayRestoran.length; i++) { 
      entitasrestoran = new EntitasRestoran(); 
      entitasrestoran.setNamaResto(arrayRestoran[i]); 
      entitasrestoran.setAlamatResto(arrayAlamat[i]); 
      entitasrestoran.setPicResto(ArrayPicRestoran[i]); 
      restoran.add(entitasrestoran); 

     } 

     CustomBaseAdapter custom = new CustomBaseAdapter(this, restoran); 
     lv.setAdapter(custom); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 

當我單擊列表視圖,然後新的活動將顯示

回答

1

lv.setOnItemClickListener(new OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) 
    { 
     // In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View. 
     TextView txtName = (TextView)v.findViewById(R.id.yourTextViewID); 
     String name = txtName.getText().toString(); 
     switch(name) 
     { 
       case "nameOne": 
        Intent intent = new Intent(YourCurrentActivity.this, NameOneActivity.class); 
        startActivity(intent); 
        break; 

       case "nameTwo": 
        Intent intent = new Intent(YourCurrentActivity.this, NameTwoActivity.class); 
        startActivity(intent); 
        break; 

       //And so on and so forth.... 
     } 

    } 
}); 

lv.setAdapter(custom);後。


當然,更換YourCurrentActivityYourNextActivity與相應的名稱。

Intents用於啓動其他活動。 您也可以使用intent.putExtra("extraValue", theValue);將變量值從一個活動傳遞到另一個活動。


Intentshere的更多信息。

+0

第二項和其他項目如何? String [] arrayRestoran = {「Kare Ayam」,「Spesial Sambal」,「Waroeng Steak」,「Hoka Bento」}; 我想每個陣列都有一個新的活動 – kucluk

+0

就像如果我clicke KAre ayam然後Kare Ayam活動顯示 然後我點擊Sepial Sambalthen特別Sambal活動顯示? – kucluk

+0

什麼是名字? TextViews? – ClaireG

1

THI後s的線路:lv.setAdapter(custom);

做到這一點:

添加
lv.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
     //write your code here that will start a new intent, something like: 
     Intent intent = new Intent(this, Activity2.class); 
     startActivity(intent); 
});