Android ListView,OnListItemClick,查找行ID?

时间:2012-03-26 10:41:49

标签: android

我似乎无法找到如何获取我的ListView(OnListItemClick),以打开不同的活动。我知道我需要为我的ListView获取一些row-id,但我不知道如何。现在,ListView中的每一行都会打开相同的活动。 抱歉我的英语不好,希望你明白。

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

 }
}

我的NewActivity看起来像这样:

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

1 个答案:

答案 0 :(得分:10)

您无需获取“ListView”的“行ID”,您需要获取{{1}中单击的的行ID }“

幸运的是,Android会在那里为您提供行ID作为Listview的最后一个参数,您当前正在使用该位置:

onListItemClick()

如果您没有ID,您还可以从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); } 获取实际项目,即您的案例中的RowData对象:

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); } 中更改代码,以便从额外的数据包中提取ID而不是位置。

另一次更新:

  

让我们说我的清单有10个项目,第1项,第2项,第3项等等。当单击项目1时,我想要打开一个新活动,让我们调用NewActivityOne,NewActivityOne将引用一个带有一些名为layoutone的文本的布局。第2项还将打开一个新活动NewActivityTwo,它将引用一个名为layouttwo的新布局。这就是我想要的所有物品。

  • 如果您的详细活动仅在布局中(即不在任何行为/代码中),请使用NewActity传递putExtra() ID(让我们将其命名为“layoutId”)到R.layout.*并在NewActivity中使用。

  • 如果您的详细活动更广泛,请为每个列表项创建单独的setContentView(getIntent().getExtras().getInt("layoutId"));类,并在Activity中创建onListItemClick()您要显示的具有限制性的活动类。

在任何一种情况下,您都可以在Intent课程中存储要显示的详细信息(int layoutIdClass<Activity>),以便您可以在RowData轻松访问它。