Android 自定义Listview启动新活动

Android 自定义Listview启动新活动,android,listview,android-activity,listactivity,Android,Listview,Android Activity,Listactivity,您好,我对listactivity的自定义listview有问题 我希望在单击listview中的项目时,新活动将启动 整体 package com.custom.listview; public class EntitasRestoran { String namaresto = ""; String alamatresto = ""; int pic; public String getNamaResto() { return namare

您好,我对listactivity的自定义listview有问题 我希望在单击listview中的项目时,新活动将启动

整体

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;
导入java.util.ArrayList;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类CustomBaseAdapter扩展了BaseAdapter{
私有静态数组列表restoran;
私人停车场;
公共CustomBaseAdapter(上下文,ArrayList res){
restoran=res;
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回restoran.size();
}
公共对象getItem(int位置){
返回restoran.get(位置);
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater
.充气(R.layout.custom_item_listview,空);
holder=新的ViewHolder();
holder.nama=(TextView)convertView
.findviewbyd(R.id.namarestoran);
holder.alamat=(TextView)convertView
.findviewbyd(R.id.alamatrestoran);
holder.im=(ImageView)convertView.findViewById(R.id.img);
convertView.setTag(支架);
}否则{
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());
返回视图;
}
静态类视窗夹{
文本视图nama;
文本视图阿拉马特;
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;
    }

}
package com.custom.listview;
导入java.util.ArrayList;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
ListView lv;
ArrayList restoran=新的ArrayList();
EntitasRestoran EntitasRestoran;
字符串[]arrayRestoran={“Kare Ayam”、“Spesial Sambal”、“Waroeng牛排”,
“Hoka便当”};
字符串[]arrayAlamat={“Jalan Kartini”、“Jalan Bimo”、“Jalan Arjuno”,
“Jalan Merdeka”};
int[]ArrayPicRestoran={R.drawable.kareayam,R.drawable.ss,
R.drawable.ws,R.drawable.hokben};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
for(int i=0;i

当我单击该列表视图时,新活动将显示在这一行之后:
lv.setAdapter(自定义)

这样做:

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);
});
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
//在此处编写代码,以启动新的意图,例如:
意向意向=新意向(此,Activity2.class);
星触觉(意向);
});
添加:

lv.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id)
{
//在下一行中,“v”指的是`getView()`方法返回的视图;表示单击的视图。
TextView txtName=(TextView)v.findViewById(R.id.yourTextViewID);
字符串名称=txtName.getText().toString();
交换机(名称)
{
案例“nameOne”:
意向意向=新意向(YourCurrentActivity.this,NameOneActivity.class);
星触觉(意向);
打破
案例“名称二”:
意向意向=新意向(YourCurrentActivity.this,NameTwoActivity.class);
星触觉(意向);
打破
//诸如此类。。。。
}
}
});
之后
lv.setAdapter(自定义);


当然,用相应的名称替换
YourCurrentActivity
YourNextActivity

意图
用于启动其他活动。 您还可以使用
intent.putExtra(“extraValue”,theValue);
将变量值从一个活动传递到另一个活动



关于
意图的更多信息

第二项和其他项目如何?String[]arrayRestoran={“Kare Ayam”、“Spesial Sambal”、“Waroeng牛排”、“Hoka便当”};我希望每个数组都有一个新的活动,比如如果我单击“KAre ayam”,然后单击“KAre ayam”活动,然后单击“Sepial Sambalt”,然后显示特殊的Sambal活动?名称是什么?文本视图?CLaire我有同样的问题,如何在“消耗品”列表视图中添加?
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....
         }

    }
});