0

我有一个ListView,我想先选择(突出显示)一个项目,然后在它进入另一个活动之前单击一个按钮。

这是我想做的一个例子:

这就是我制作 ListView 的方式(我从数据库中获取它的条目)

    ListView lv = (ListView) findViewById(R.id.lvListOfCustomers);

    String strDBName = "db_customers.s3db";
    File fileDB = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
            strDBName);
    SQLiteDatabase dbLibrary = SQLiteDatabase.openOrCreateDatabase(fileDB,
            null);

    // Start - Cursor and Queries of List of Customers(ListView)
    String sqlQuery = "select _id, customer_name as cName, customer_address as cAddress, customer_status as cStatus from tbl_customers";
    Cursor cur = (Cursor) dbLibrary.rawQuery(sqlQuery, null);

    @SuppressWarnings("deprecation")
    SimpleCursorAdapter sca = new SimpleCursorAdapter(
            this.getApplicationContext(),
            R.layout.lv_list_of_customers_txtview, cur, new String[] {
                    "cName", "cAddress", "cStatus" }, new int[] {
                    R.id.tvCustomersName, R.id.tvCustomersAddress,
                    R.id.tvCustomersId }, CursorAdapter.FLAG_AUTO_REQUERY);
    lv.setAdapter(sca);

    // End - Cursor and Queries of List of Customers(ListView)

    // Start - Make the items highlighted
    int selectedListItem = getIntent().getIntExtra("PositionInList", -1);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setSelection(selectedListItem); 
    // End - Make the items highlighted

这就是我为我的 Button 所做的

    case R.id.bCallRemarks:
        // TODO Auto-generated method stub
        Button bCRemarks = (Button) findViewById(R.id.bCallRemarks);
        bCRemarks.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ListView.OnItemClickListener oicl = new ListView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> av, View view,
                            int pos, long id) {
                        // TODO Auto-generated method stub
                        Cursor c = (Cursor) av.getItemAtPosition(pos);
                        String cName = c.getString(c
                                .getColumnIndexOrThrow("cName"));
                        Intent intent = new Intent(getApplication(),
                                CallRemarks.class);
                        intent.putExtra("CustomerName", cName);

                        Toast.makeText(av.getContext(), cName,
                                Toast.LENGTH_SHORT).show();

                         Intent CallRemarksScreen = new Intent(
                         getApplicationContext(), CallRemarks.class);
                         startActivity(CallRemarksScreen);
                    }

                };

            }

        });

我问这个是因为ListView底部有一个和多个按钮。所以我必须在选择功能(按钮)之前选择第一项。

4

1 回答 1

0

我把ListView的onItemClick放在Button的onClick外面

        @Override
        public void onItemClick(AdapterView<?> av, View view, int pos,
                long id) {
            // TODO Auto-generated method stub
            selectedItem = true;
            Cursor c = (Cursor) av.getItemAtPosition(pos);
            String cName = c.getString(c.getColumnIndexOrThrow("cName"));
            intent = new Intent(getApplication(), CallRemarks.class);
            intent.putExtra("CustomerId", id);

            Toast.makeText(av.getContext(), "id: " + id, Toast.LENGTH_SHORT)
                    .show();

        }
    });

and then here is my button

    case R.id.bCallRemarks:
        // TODO Auto-generated method stub
        Button bCRemarks = (Button) findViewById(R.id.bCallRemarks);
        bCRemarks.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedItem == false) {
                    Toast.makeText(getApplicationContext(),
                            "No Customer Selected", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    // Intent intent = new Intent(
                    // getApplicationContext(), CallRemarks.class);
                    startActivity(intent);
                }
            }

        });
        break;

我初始化了一个布尔标志来检查是否选择了一个项目,以防我单击了一个按钮而没有在 ListView 中选择一个项目。

private boolean selectedItem = false;
于 2013-02-27T07:53:20.430 回答