如何从android listview中的选定行获取值?

时间:2015-11-17 10:27:33

标签: android listview onclick

我有ListView,我用适配器类填充了这个ListView的值。现在我想在另一个活动中显示选定的行值。如何做?
我的活动:

public class SimplePurchaseOrderActivity extends AppCompatActivity {
    protected FrameLayout frameLayout;
    SharedPreferences sharedPrefUser;
    SharedPreferences.Editor editor;    
    private ListView purchaseOrderListView;
    SimplePurchaseOrderAdapter simplePurchaseOrderAdapter;
    ArrayList<PoHeadersBO> poHeadersBOs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_purchase_order)
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        purchaseOrderListView=(ListView) findViewById(R.id.purchaseOrderListView);
        searchPOOrders=(Button) findViewById(R.id.btnPOSearch);
        searchSupplier=(EditText) findViewById(R.id.searchPOEditText);
        edtFromDate=(EditText) findViewById(R.id.poFromDate);
        edtToDate=(EditText) findViewById(R.id.poToDate);
        sharedPrefUser = getSharedPreferences(ISharedPreferencesUtil.SHARED_USER_MANAGEMENT, 0);
        editor = sharedPrefUser.edit();
        new GetPurchaseOrders(IUrlsUtil.URL_PURCHASE_ORDERS).execute();
    ***purchaseOrderListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object obj = purchaseOrderListView.getItemAtPosition(position);
                //here is the problem ... please check the console error below.
                String purchaseOrderNumber = ((PoHeadersBO) obj).getPoNumber().toString();
                String supplierName=((PoHeadersBO) obj).getSupplierId().toString();
                ArrayList<PoDetailsBO> poDetails=((PoHeadersBO) obj).getPoDetails();
                Intent iPODetails = new Intent(SimplePurchaseOrderActivity.this, PurchaseOrderReportActivity.class);
                iPODetails.putExtra("poNumber", purchaseOrderNumber);
                iPODetails.putExtra("supplierName", supplierName);
                iPODetails.putExtra("poDetails", poDetails);
                startActivity(iPODetails);
            }
        });***
    }
    class GetPurchaseOrders extends AsyncTask<String, String, String> {
        ProgressDialog pDialog;
        ResultVO getPurchaseOrderResponse;
        String param;
        PurchaseOrderArrayListBO purchaseOrderArrayListBO;
        GetPurchaseOrders(String param) {
            this.param = param;
        }
        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(SimplePurchaseOrderActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
            try {
                YGTHeaderParms ygtHeaderParms = ICommonMethods.setHeaderParams(SimplePurchaseOrderActivity.this);
                APIServicesImpl services = new APIServicesImpl();
                getPurchaseOrderResponse = services.get(PurchaseOrderArrayListBO.class, param, ygtHeaderParms);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String file_url) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
            if (getPurchaseOrderResponse != null) {
                String authToken = getPurchaseOrderResponse.getAuthToken();
                if (authToken != null) {
                    editor.putString(ISharedPreferencesUtil.AUTH_TOKEN, authToken);
                    editor.commit();
                }
                if (getPurchaseOrderResponse.getAppStatusCode() == Constants.APP_STATUS_CODE_SUCCESS) {
                    purchaseOrderArrayListBO = (PurchaseOrderArrayListBO) getPurchaseOrderResponse.getPayload();
                    poHeadersBOs = purchaseOrderArrayListBO.getPurchaseOrderList();
                    if (poHeadersBOs != null) {
                        simplePurchaseOrderAdapter = new SimplePurchaseOrderAdapter(SimplePurchaseOrderActivity.this, poHeadersBOs);
                        purchaseOrderListView.setAdapter(simplePurchaseOrderAdapter);
                    } else {
                        ExceptionMessages.showAlertDialog(SimplePurchaseOrderActivity.this, "No response from server", "Unable to get records", true);
                    }
                } else {
                    if (poHeadersBOs != null) {
                        poHeadersBOs.clear();
                        simplePurchaseOrderAdapter.notifyDataSetChanged();
                    }
                }
            } else {
                ExceptionMessages.showAlertDialog(SimplePurchaseOrderActivity.this, IExceptionUtil.NULL_RESPONSE_TITLE, IExceptionUtil.NULL_RESPONSE_MESSAGE, true);
            }
        }
    }
}

我的错误输出是这样的:

11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime: FATAL EXCEPTION: main
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime: Process: com.yougotag.pharmainventory, PID: 12980
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime: java.lang.ClassCastException: java.lang.Integer cannot be cast to com.ygt.aroga.common.bo.PoHeadersBO
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at com.yougotag.pharmatenant.SimplePurchaseOrderActivity$5.onItemClick(SimplePurchaseOrderActivity.java:181)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.widget.AbsListView$3.run(AbsListView.java:3865)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:808)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:103)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5333)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
11-17 15:20:58.824 12980-12980/com.yougotag.pharmainventory E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

这是在课堂上发生的主要错误,我在活动中评论过。它正在运行的一些课程。这是不行的

2 个答案:

答案 0 :(得分:0)

尝试更改此内容:

Object obj = purchaseOrderListView.getItemAtPosition(position);

Object obj = parent.getItemAtPosition(position);

答案 1 :(得分:0)

您应该从适配器下面的对象获取数据,而不是从ListView中获取数据。

因此,如果您的Adapater数据源是poHeadersBOs,您应该从那里获取它:

PoHeadersBO poHeadersBOsItem = poHeadersBOs.get(position);