从listview行进入另一个屏幕

时间:2012-03-19 23:33:27

标签: android listview

在下面的代码中,我想在我的行的某些“元素”上设置Onclick Listeners。我应该在哪里放置setOnclick监听器代码?在活动代码或适配器中?我想第一个,但在这种情况下,我将如何识别已选择哪一行 (每一行都应该让我得到不同的观点)?

Java代码:

List<String[]> info=new ArrayList<String[]>();
         String[] names_info=new String[5];
         String[] phones_info=new String[5];
         String[] map_info=new String[5];
         String[] web_info=new String[5];
         String[] photos_info=new String[5];
for(int i=0;i<names_info.length;i++)
     {
         info.add(new String[]{names_info[i],phones_info[i],map_info[i],web_info[i],photos_info[i]});
     }
     for (int i = 0; i < info.size(); i++) {
       Log.d("TAG", "item " + i + 
                " name:" +  info.get(i)[0] + 
                " info:" + info.get(i)[1]);
    }
     FacilitiesAdapter adapter = new FacilitiesAdapter(this,info,want_info_display,want_phone_display,want_photos_display);
     //System.out.println(info[0]);
     setListAdapter(adapter);

EDIT

 ListView lv = getListView();



lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
             // When clicked, show a toast with the TextView text
            //Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),
            //Toast.LENGTH_SHORT).show();
Intent i33 = new Intent(HotelsScreen.this,AbousUsScreen.class);
          i33.putExtra("position", arg2);
            System.out.println(arg2);
            System.out.println(info.get(arg2)[0]);

        startActivity(i33);             
        }
    });

我在Log中看到了结果,这意味着我的信息正常得到了结果。

这是我的适配器。

public class FacilitiesAdapter extends ArrayAdapter<String[]> {
    private final Context context;
    List <String[]> dataList;

    public FacilitiesAdapter(Context context, List<String[]> dataList, int need_phone, int need_info, int need_photos)  {
        super(context, R.layout.expand_row);
        this.context = context;
        this.dataList = dataList;
        //this.tracks_condition=tracks_condition;
        //this.count=count;
    }
     public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.expand_row, parent, false);
            System.out.println("I am here");
            String[] data = dataList.get(position);
            String name=data[0];
            TextView textView = (TextView) rowView.findViewById(R.id.name);
            System.out.println("I am in the adapter "+name);
            textView.setText(name);
            return rowView;
     }
}

编辑1: 我现在应该在每个项目中看到我点击它的位置和名称吗?因为我什么也看不见?

2 个答案:

答案 0 :(得分:1)

可以像这些行一样简单:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int position,
      long arg3)
  {
    Intent i33 = new Intent(MyScreen.this,OtherScreen.class);
    i33.putExtra("position", position);
    startActivity(i33);
  }
});

答案 1 :(得分:0)

我认为你最大的问题是弄清楚如何将一行映射到适当的屏幕。如果可以执行此操作,则可以设置包含所有映射的Map对象。

在你的适配器中,你可以在视图上使用setTag()并将它从Map返回,然后从中创建一个新的Intent。

//In your adapter's getView() method...
Class activityClass = rowToActivityMap.get(someRowAttribute);
view.setTag(activityClass);

//In your click listener...
Class activityClass = (Class)view.getTag();
Intent intent = new Intent(this, activityClass);
startActivity(intent);