对ListView中的选定项目执行操作

时间:2015-10-04 08:28:45

标签: java android android-studio

我有一个名为type_of_dairy的布局,代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:layoutDirection="rtl"

    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Type"
        android:id="@+id/iceCreamType"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.05"
        android:layout_marginTop="20dp"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:id="@+id/listView"
        android:layout_weight="0.26"
        android:background="#999900"
        android:choiceMode="singleChoice"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Flavour"
        android:id="@+id/iceCreamFlavor"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.05"
        android:layout_marginTop="10dp"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:id="@+id/listView2"
        android:layout_gravity="center_horizontal"
        android:background="#999900"
        android:choiceMode="singleChoice"
        />

    <EditText
        android:layout_width="89dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:id="@+id/editText"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply"
        android:id="@+id/countOficeCream"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_weight="0.05" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:id="@+id/iceCreamReject"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_weight="0.05" />

</LinearLayout>

如您所见,我有两个ListView,我已经从我的数据库中填写了第一个ListView,我的数据库表的一个字段已选择填写此ListView(@+id/listView)和有两个按钮,我希望当我从第一个ListView(@+id/listView)中选择一个项目时,第二个ListView (@+id/listView2)填写我的数据库的另一个字段,

public class Dairy extends Activity {
    Button returnBack;
    Statement statement;
    Statement statement2;
    ListView dairyType;
    ListView dairyFlavour;
    ArrayList<String> typeArray;
    ArrayList<String> dairyArray;
    ArrayAdapter<String> typeAdapter;
    ArrayAdapter<String> flavorAapter;
    String typeString, flavourString;
    private Object view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.type_of_dairy);



        dairyType = (ListView) findViewById(R.id.listView);
        dairyFlavour = (ListView) findViewById(R.id.listView2);
        typeArray = new ArrayList<String>();
        typeAdapter = new ArrayAdapter<String>(getApplicationContext(),
        android.R.layout.simple_list_item_single_choice, typeArray);

        flavorAapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_single_choice, dairyArray);

        try
        {
            ConnectionHelper connectionHelper = new ConnectionHelper();
            statement = connectionHelper.getConnection().createStatement();
            ResultSet resultSet = statement.executeQuery("select Name from tblProductCategory where TypeId=1");

            if(resultSet.next()) {
                do {
                    typeString=resultSet.getString(resultSet.findColumn("Name"));
                    typeArray.add(typeString);
                    dairyType.setAdapter(typeAdapter);
                }while(resultSet.next());
            }
        }
        catch (SQLException e)
        {

        }

        dairyType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try{
                    ConnectionHelper connectionHelper = new ConnectionHelper();
                    statement2 = connectionHelper.getConnection().createStatement();
                    ResultSet resultSet = statement2.executeQuery("select username from tblUser");
                    if(resultSet.next())

                    {
                        do {
                            flavourString = resultSet.getString(resultSet.findColumn("UserName"));
                            dairyArray.add(flavourString);
                            dairyFlavour.setAdapter(flavorAapter);
                        } while (resultSet.next());
                    }
                }catch (SQLException e)
                {
                    Toast.makeText(getApplicationContext(), "Problem detected", Toast.LENGTH_LONG).show();
                }
            }
        });

我希望当我从第一个ListView(dairyType)中选择一个项目时,我的数据库的另一个表中的一些数据会被选中并显示在第二个ListView(dairyFlavour)中,而这部分我说的是:

    dairyType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try{
                ConnectionHelper connectionHelper = new ConnectionHelper();
                statement2 = connectionHelper.getConnection().createStatement();
                ResultSet resultSet = statement2.executeQuery("select username from tblUser");
                if(resultSet.next())

                {
                    do {
                        flavourString = resultSet.getString(resultSet.findColumn("UserName"));
                        dairyArray.add(flavourString);
                        dairyFlavour.setAdapter(flavorAapter);
                    } while (resultSet.next());
                }
            }catch (SQLException e)
            {
                Toast.makeText(getApplicationContext(), "Problem detected", Toast.LENGTH_LONG).show();
            }
        }
    });

但我在我的Android设备中停止myAppclication,在android studio中停止javaNullPointExecption 但是,通过与DB的这种连接,我已经显示了第一个ListView并将其载入应用程序。

2 个答案:

答案 0 :(得分:0)

从您给定的代码中,您似乎忘记初始化“dairyArray”。 首先初始化它,然后添加字符串。

你可能会收到“dairyArray.add(flavourString);”的错误,因为dairyArray没有初始化。 我希望它能奏效。

答案 1 :(得分:0)

正如我的朋友@harish所说我没有在我的代码中指定dairyArray,所以相关的适配器什么都不返回, 但我通过以下代码对所选项目采取行动:

        public void onClick(View v) {
            String str;
        str=flavorAapter.getItem(dairyFlavour.getCheckedItemPosition()).toString();

然后返回str。