自定义列表视图启动新活动 [英] custom Listview Start new activity

查看:135
本文介绍了自定义列表视图启动新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,自定义列表视图与listactivity 我希望当我点击从列表视图,然后新的活动将启动项目。

Hi i have problem with custom listview with listactivity i want when i click item from listview then new activity will started.

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;
    }

}

当我单击列表视图,然后新的活动将显示

when i click that listview then new activity will show up

推荐答案

地址:

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(自定义);

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

Of course, replace YourCurrentActivity and YourNextActivity with the corresponding names.

意图用于启动其他活动。 您也可以使用 intent.putExtra(的ExtraValue,theValue); 来从一个活动传递变量值到另一个

Intents are used to start up other activities. You can also use intent.putExtra("extraValue", theValue); to pass variable values from one activity to another.

有关意图 点击此处了解详情

这篇关于自定义列表视图启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆