Android 从ListView访问行数据

Android 从ListView访问行数据,android,android-listview,Android,Android Listview,我正在尝试通过单击ListViewItem将数据从一个活动传递到另一个活动 但是我在访问行中的数据以便将其传递给下一个活动时遇到问题 我目前的代码是: final ListView businessListObj = (ListView) findViewById(R.id.bussinessListView); ListAdapter adapter = new SimpleAdapter(HomeActivity.this, bussinessList, R.

我正在尝试通过单击
ListViewItem
将数据从一个活动传递到另一个活动

但是我在访问行中的数据以便将其传递给下一个活动时遇到问题

我目前的代码是:

final ListView businessListObj = (ListView) findViewById(R.id.bussinessListView);
ListAdapter adapter = new SimpleAdapter(HomeActivity.this, bussinessList,
                R.layout.business_list_item,
                new String[] { TAG_NAME, TAG_ADDRESS, TAG_ID }, new int[] {
                        R.id.name, R.id.address, R.id.businessId});
businessListObj.setAdapter(adapter);
businessListObj.setClickable(true);
businessListObj.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
{
     Intent detailsIntent = new Intent(HomeActivity.this, DetailActivity.class);
     startActivity(detailsIntent);
}
});
但是,我似乎不太明白从对象获取名称、地址和id以传递给下一个活动的语法

编辑:XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
       android:id="@+id/businessId"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Small Text"
       android:textAppearance="?android:attr/textAppearanceSmall" />

编辑2:业务列表:

ArrayList<HashMap<String, String>> businessList = new ArrayList<HashMap<String, String>>();
ArrayList businessList=new ArrayList();
您的代码已关闭

Object o = businessListObj.getAdapter().getItem(position);
if(o instanceof MyClass){
    MyClass myObject = (MyClass) o;
    Intent detailsIntent = new Intent(HomeActivity.this, DetailActivity.class);

    detailsIntent.putExtra("extName", myObject.getName());
    detailsIntent.putExtra("extAddress", myObject.getAddress());
    detailsIntent.putExtra("extId", myObject.getId());

    startActivity(detailsIntent);
}
最好是让MyClass实现Parcelable。然后你可以做:

 detailsIntent.putExtra("EXTRA_ITEM", myObject);
然后在活动中,您可以执行以下操作:

MyClass mMyObject = getIntent().getParcelableExtra("EXTRA_ITEM");
如果您需要有关parcelable实现的帮助,请查看我构建的这个web应用程序,它通过json提要为您生成了模型类,包括使它们可打包

我明白了。你实际上没有模特儿。您肯定需要使用json到java生成器并使用真实的对象模型。每次我尝试使用HashMap作为我的模型时,我都不得不在以后重写它

那么,

HashMap<String,String> myMap = (HashMap<String,String>) o;
HashMap myMap=(HashMap)o;
然后,您可以根据需要从映射中提取值。

请尝试以下操作

你有这个

  ArrayList<HashMap<String, String>> businessList = new ArrayList<HashMap<String, String>>();

发布你的
布局.business\u list\u item.xml
@Raghunandan,开始吧。没什么特别的。你能公布什么是商业清单吗?这显然是一个列表,但它是什么类型的对象?@JonFHancock用BusinessList编辑。@FrankJohnson现在查看我的帖子并尝试一下。这不太管用。
map.get()。如果你做对了,上面的方法应该有效
  ArrayList<HashMap<String, String>> businessList = new ArrayList<HashMap<String, String>>();
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            HashMap<String,String> map =(HashMap<String, String>) arg0.getItemAtPosition(arg2);
            Intent detailsIntent = new Intent(HomeActivity.this, DetailActivity.class);              
            detailsIntent.putExtra("addresskey",map.get("TAG_ADDRESS").toString());
            detailsIntent.putExtra("namekey",map.get("TAG_NAME").toString());
            detailsIntent.putExtra("idkey",map.get("TAG_ID").toString()); 
            startActivity(detailsIntent);
            // check in logcat  
            Log.i("....................",map.get("TAG_ADDRESS").toString()); 
            Log.i("....................",map.get("TAG_NAME").toString());
            Log.i("....................",map.get("TAG_ID").toString());     
        }
    });