Android的ListView控件,OnListItemClick,找到行ID? [英] Android ListView, OnListItemClick, find row id?

查看:82
本文介绍了Android的ListView控件,OnListItemClick,找到行ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找出如何让我的ListView(OnListItemClick),打开不同activity's。我知道,我莫名其妙地需要得到一些行ID为我的ListView,但我不知道怎么样。现在在我的ListView中每一行只是打开了相同的活动。 对不起,我的英语不好,希望大家理解。

 进口的java.util.List;
进口java.util.Vector中;

进口android.app.Activity;
进口android.app.ListActivity;
进口android.content.Context;
进口android.content.Intent;
进口android.net.ParseException;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TextView;


公共类MainActivity扩展ListActivity {
私人LayoutInflater mInflater;
私人矢量< RowData>数据;
RowData次;

静态最终的String []标题=新的String [] {
文本,
文本,
文本,
;

静态最终的String []详细=新的String [] {
Image1的,
IMAGE2
的Image3
};

私人整数[] imgid = {
R.drawable.spektr,
R.drawable.spektr,
R.drawable.sleep,



};

@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
mInflater =(LayoutInflater)getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
数据=新的矢量< RowData>();
的for(int i = 0; I< title.length;我++){

尝试 {
次=新RowData(ⅰ,标题[I],详细[I]);
}赶上(ParseException的E){
    e.printStackTrace();
}
data.add(RD);
}
CustomAdapter适配器=新CustomAdapter(这一点,R.layout.list,
                              R.id.title,数据);
setListAdapter(适配器);
getListView()setTextFilterEnabled(真)。
}
公共无效onListItemClick(ListView的父,视图V,INT位置,
                                                         长ID){

意向意图=新的意图(这一点,NewActivity.class);
intent.putExtra(ID,item.mId);
  startActivity(意向);

}
私有类RowData {
保护INT MID;
保护字符串mTitle;
保护字符串mDetail;
RowData(INT ID,字符串名称,字符串细节){
MID = ID;
mTitle =称号;
mDetail =细节;
}
@覆盖
公共字符串的toString(){
        返回MID ++ mTitle ++ mDetail;
}
}
私有类CustomAdapter扩展ArrayAdapter< RowData> {

公共CustomAdapter(上下文的背景下,INT资源,
                 INT textViewResourceId,名单,其中,RowData>对象){

超(背景下,资源,textViewResourceId,对象);
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){

ViewHolder支架=无效;
TextView的标题= NULL;
TextView的细节= NULL;
ImageView的I11 = NULL;
RowData rowData =的getItem(位置);
如果(空== convertView){
     convertView = mInflater.inflate(R.layout.list,NULL);
     持有人=新ViewHolder(convertView);
     convertView.setTag(保持器);
}
      支架=(ViewHolder)convertView.getTag();
      标题= holder.gettitle();
      title.setText(rowData.mTitle);
      细节= holder.getdetail();
      detail.setText(rowData.mDetail);

      I11 = holder.getImage();
      i11.setImageResource(imgid [rowData.mId]);
      返回convertView;
}
     私有类ViewHolder {
     私人查看MROW;
     私人TextView的标题= NULL;
     私人TextView的细节= NULL;
     私人ImageView的I11 = NULL;

     公共ViewHolder(查看行){
     MROW =行;
}
  公众的TextView的getTitle(){
      如果(空==标题){
          标题=(TextView中)mRow.findViewById(R.id.title);
         }
     返回称号;
  }

  公众的TextView getdetail(){
      如果(空==细节){
           细节=(TextView中)mRow.findViewById(R.id.detail);
             }
    返回细节;
  }
 公共ImageView的的getImage(){
      如果(空== I11){
           I11 =(ImageView的)mRow.findViewById(R.id.img);
                               }
         返回I11;
 }
 }

 }
}
 

和我NewActivity是这样的:

 进口android.app.Activity;
进口android.os.Bundle;

公共类NewActivity延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.poetry);

    。getIntent()getExtras()调用getInt(ID)。
}}
 

解决方案

您并不需要得到行号的的ListView ,你需要得到在行号的项目的被点击了列表视图

幸运的是,Android的给你行ID在那里作为最后一个参数 onListItemClick(),您正在使用中的位置:

 公共无效onListItemClick(ListView的父,视图V,INT位置,长的id){
  意向意图=新的意图(这一点,NewActivity.class);
  // intent.putExtra(位置,位置); //不是你想要的
  intent.putExtra(身份证,身份证); //更多的可能是你想要什么
  startActivity(意向);
}
 

如果您还没有一个ID,你也可以得到实际的项目,即 RowData 你的情况的对象,从 ListAdapter

 公共无效onListItemClick(ListView的父,视图V,INT位置,长的id){
  RowData项目=(RowData)getListAdapter()的getItem(位置)。
  意向意图=新的意图(这一点,NewActivity.class);
  intent.putExtra(ID,item.mId);
  startActivity(意向);
}
 

在这两种情况下,你必须改变你的code。在 NewActity 让你获取该ID,没有位置,从额外的包。

另一个更新:

  

比方说我的名单上有10个项目,第1项,第2项,第3项等。当第1项被点击我想,打开一个新的活动,让我们称之为NewActivityOne,并NewActivityOne将参考布局一些文字被称为layoutone。第2项也将开启一个新的活动,NewActivityTwo,这将涉及到一个新的布局被称为layouttwo。这就是我想这是对我的所有项目。

  • 如果您的详细活动不同的仅在布局的(即不以任何行为/ code),只需使用 putExtra()来传递 R.layout。* ID(我们称之为layoutId)将 NewActivity 和使用,在的setContentView(getIntent()getExtras()调用getInt(layoutId));

  • 如果您的详细活动不同的更广泛地的,创建一个单独的活动类,每一个列表项,并在 onListItemClick()创建一个意图与你想要显示的respestive活动类。

在这两种情况下,你可以(;活动&GT INT layoutId 类&LT)存储的细节,以显示你的 RowData 类,所以你可以很容易地访问 onListItemClick()

I cant seem to find out how to get my ListView (OnListItemClick), to open different activity´s. I know i somehow need to get some row-id for my ListView, but i dont know how to. Right now every row in my ListView just opens up the same activity. Sorry for my bad English, hope You understand.

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;

static final String[] title = new String[] {
"Text", 
"Text", 
"Text",
;

static final String[] detail = new String[] {
"Image1",
"Image2",
"Image3",
};

private Integer[] imgid = {
R.drawable.spektr,
R.drawable.spektr,
R.drawable.sleep,



};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){

try {
rd = new RowData(i,title[i],detail[i]);
} catch (ParseException e) {
    e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
                              R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position,
                                                         long id) {         

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("id", item.mId);
  startActivity(intent);

}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDetail;
RowData(int id,String title,String detail){
mId=id;
mTitle = title;
mDetail=detail;
}
@Override
public String toString() {
        return mId+" "+mTitle+" "+mDetail;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {

public CustomAdapter(Context context, int resource,
                 int textViewResourceId, List<RowData> objects) {               

super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {   

ViewHolder holder = null;
TextView title = null;
TextView detail = null;
ImageView i11=null;
RowData rowData= getItem(position);
if(null == convertView){
     convertView = mInflater.inflate(R.layout.list, null);
     holder = new ViewHolder(convertView);
     convertView.setTag(holder);
}
      holder = (ViewHolder) convertView.getTag();
      title = holder.gettitle();
      title.setText(rowData.mTitle);
      detail = holder.getdetail();
      detail.setText(rowData.mDetail);                                                     

      i11=holder.getImage();
      i11.setImageResource(imgid[rowData.mId]);
      return convertView;
}
     private class ViewHolder {
     private View mRow;
     private TextView title = null;
     private TextView detail = null;
     private ImageView i11=null; 

     public ViewHolder(View row) {
     mRow = row;
}
  public TextView gettitle() {
      if(null == title){
          title = (TextView) mRow.findViewById(R.id.title);
         }
     return title;
  }     

  public TextView getdetail() {
      if(null == detail){
           detail = (TextView) mRow.findViewById(R.id.detail);
             }
    return detail;
  }
 public ImageView getImage() {
      if(null == i11){
           i11 = (ImageView) mRow.findViewById(R.id.img);
                               }
         return i11;
 }
 }

 }
}

And my NewActivity looks like this:

import android.app.Activity;
import android.os.Bundle;

public class NewActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.poetry);

    getIntent().getExtras().getInt("id");
}}

解决方案

You don't need to get the "row ID of the ListView", you need to get the "row ID of the item that was clicked in the Listview"

Luckily, Android gives you the row ID right there as the last parameter of onListItemClick(), where you're currently using the position:

public void onListItemClick(ListView parent, View v, int position, long id) {
  Intent intent = new Intent(this, NewActivity.class);
  // intent.putExtra("position", position); // not what you want
  intent.putExtra("id", id); // more likely what you want
  startActivity(intent);
}

If you do not have an ID, you can also get the actual item, i.e. a RowData object in your case, from the ListAdapter:

public void onListItemClick(ListView parent, View v, int position, long id) {
  RowData item = (RowData) getListAdapter().getItem(position);
  Intent intent = new Intent(this, NewActivity.class);
  intent.putExtra("id", item.mId);
  startActivity(intent);
}

In both cases, you have to change your code in NewActity so you extract the ID, not position, from the extra bundle.

Another update:

Lets say my list have 10 items, item 1, item 2, item 3 and so on. When item 1 is clicked i want that to open a new activity, let's call that NewActivityOne, and NewActivityOne will refer to a layout with some text called layoutone. Item 2 will also open a new activity, NewActivityTwo, which will refer to a new layout called layouttwo. And that's how I want it to be for all my items.

  • If your detail activities differ only in the layout (i.e. not in any behavior/code), just use putExtra() to pass an R.layout.* ID (let's call it "layoutId") to your NewActivity and use that in setContentView(getIntent().getExtras().getInt("layoutId"));.

  • If your detail activities differ more widely, create a separate Activity class for every list item, and in onListItemClick() create an Intent with the respestive activity class you want to display.

In either case, you can store the details to display (int layoutId or Class<Activity>) in your RowData class so you can easily access it in onListItemClick().

这篇关于Android的ListView控件,OnListItemClick,找到行ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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