0

我是这个领域的新手。我的应用程序有 6 个不同类别的按钮。当我单击其中一个按钮时,它将转到另一个页面以显示我数据库中特定类别的 listView。

我的问题是,我不知道如何为这种方法切换案例。

Log.d("Reading", "Reading all Kategori ..");
     List<UkmLocation> kategori = db.getCategoryFaculty();

     for(UkmLocation k : kategori) {
       results.add(k.getName());
       results_id.add(k.getID());
        }

          adapter = new ArrayAdapter<String>(this, R.layout.list_item,     R.id.textView1,results);     
            setListAdapter(adapter);

我想在这个 QSFacultyLocation.java 中包含 getCategoryHall、getCategoryFacilty 和 getCategoryAdmin。因此,我不需要创建另一个活动来获取 getCategoryHall、getCategoryFacilty 和 getCategoryAdmin (listView)。因为它只是使用相同的编码。

以下是我的完整代码:

QSFacultyLocation.java

package com.example.ukmlocationsearching;

import java.util.ArrayList;
import java.util.List;

import com.example.ukmlocationsearching.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class QSFacultyLocation extends ListActivity {

    //------------------------------------------------------------------
        // Declaration
        public static UkmLocation selectedPOI = null;
        final DatabaseHandler db = new DatabaseHandler(this);
        private EditText filterText = null;
        ArrayAdapter<String> adapter = null;
        final ArrayList<String> results = new ArrayList<String>();
        final ArrayList<String> results_id = new ArrayList<String>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.qs_faculty_location);
            final Intent c = new Intent(QSFacultyLocation.this, QSLocationDetail.class);

            //------------------------------------------------------------------
            // Link editText to layout item
            filterText = (EditText) findViewById(R.id.search_box);
            filterText.addTextChangedListener(filterTextWatcher);

            //------------------------------------------------------------------
            // Reading Poi      
            /*SearchKategori searchKategori = new SearchKategori();
            UkmLocation selectedKategori = searchKategori.getSelectedKategori();
            List<UkmLocation> locationList = null;*/

              Log.d("Reading", "Reading all Kategori ..");
                List<UkmLocation> kategori = db.getCategoryFaculty();

            //------------------------------------------------------------------
            // Determine list POI with category

                for(UkmLocation k : kategori) {
                    results.add(k.getName());
                    results_id.add(k.getID());
                }

            //------------------------------------------------------------------
            // Set list arrayAdapter to adapter
            adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView1,results);       
            setListAdapter(adapter);

            //------------------------------------------------------------------
            // Set ListView from ListActivity
            ListView lv = getListView();
            lv.setTextFilterEnabled(true);

            //------------------------------------------------------------------
            // Set click event from listView
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, 
                        int position, long id) {
                  // When clicked, show a toast with the TextView text
                  Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                      Toast.LENGTH_SHORT).show();

                  Log.d("test", "position:" + position);

                  Log.d("test", "actualname:" + db.getUkmLocationByName(adapter.getItem(position)).getName());              

                  // String poiID = results_id.get(position);
                  String poiID = db.getUkmLocationByName(adapter.getItem(position)).getID();
                  setSelectedPoi(poiID);
                  startActivity(c);
                }
            }); 


            //------------------------------------------------------------------
            // Closing db (if any)
        //    db.close();
        }

        private TextWatcher filterTextWatcher = new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                adapter.getFilter().filter(s);
            }       
        };

        @Override
        protected void onDestroy() {
            super.onDestroy();
            filterText.removeTextChangedListener(filterTextWatcher);
        }

        public UkmLocation getSelectedPoi() {
            return selectedPOI;
        }

        public void setSelectedPoi(String poiID) {
            selectedPOI = db.getUkmLocation(poiID);
            Log.d("test2", "_id:" + db.getUkmLocation(poiID).getID());
            Log.d("test2", "Name:" + db.getUkmLocation(poiID).getName());
            // Closing db
            db.close();
        }
}

快速搜索菜单.java

package com.example.ukmlocationsearching;

import java.io.IOException;
import com.example.ukmlocationsearching.R;
import com.example.ukmlocationsearching.R.id;
import com.example.ukmlocationsearching.R.layout;
import com.example.ukmlocationsearching.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class QuickSearchMenu extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //---------------------------------------------------------------------------
        // Initiate database data    
            initiateDb();

        //---------------------------------------------------------------------------
            super.onCreate(savedInstanceState);
        setContentView(R.layout.quick_search_menu);

        View faculty = findViewById(R.id.button2);
        faculty.setOnClickListener(this);

        View researchInstitute = findViewById(R.id.button1);
        researchInstitute.setOnClickListener(this);

        View college = findViewById(R.id.button3);
        college.setOnClickListener(this);

        View admin = findViewById(R.id.button4);
        admin.setOnClickListener(this);

        View facility = findViewById(R.id.button5);
        facility.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quick_search_menu, menu);
        return true;
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {

        case R.id.button1:
            Intent b1 = new Intent(this, QSResearch.class);
            startActivity(b1);
            break;
        case R.id.button2:
            Intent b2 = new Intent(this,QSFacultyLocation.class);
            startActivity(b2);
            break;
        /*case R.id.button3:
            Intent b3 = new Intent(this,QuickSearchCollege.class);
            startActivity(b3);
            break;
        case R.id.button4:
            Intent b4 = new Intent(this,QuickSearchFaculty.class);
            startActivity(b4);
            break;
        case R.id.button5:
            Intent b5 = new Intent(this,QuickSearchCollege.class);
            startActivity(b5);
            break;*/
    }
}
    //---------------------------------------------------------------------------
    // Initiate database data
    public void initiateDb() {
        DatabaseHandler myDbHandler =  new DatabaseHandler(this);

        try {
            myDbHandler.createDataBase();
        } 
        catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            myDbHandler.openDataBase();
        }
        catch(SQLException sqle) {
            throw sqle;
        }
        Log.d("Initiate", "UKM Location Count: " + myDbHandler.getUkmLocationCount());
        /*Log.d("Initiate", "Kategori Count: " + myDbHandler.getKategoriCount());
        Log.d("Initiate", "KategoriPoi Count: " + myDbHandler.getKategoriPoiCount());*/
        myDbHandler.close();
    }

    //------------------------------------------------------------------------------

}

请帮我解决这个问题。谢谢你

4

1 回答 1

0

首先,通过使用每个按钮单击传递查询或简单的任何键值(例如部门 ID 或部门名称)作为意图

intentname.putExtra(keyname_used_to_retrieve_value,value_to_be_passed_to_next_activity);

        case R.id.button1:
        String query="select * from table_name where something='xyz'";
        Intent b1 = new Intent(this, QSResearch.class);
        b1.putExtra("myextrakey",query);
        startActivity(b1);

然后,在list活动的onCreate方法中接收intent如下,初始化数据库并调用用户定义的mylist()和setuplist()函数

    Intent i=getIntent();
    String x=i.getStringExtra("myextrakey");//this assigns the query to string x
    //open database here
    mylist();//call myList function which i have defined in the following lines
    setUpList();//call setUpList function which i have defined in the following lines

这是 myList 函数:

private void myList() {
        myownlist= new ArrayList<String>();
        Cursor myCursor = database.rawQuery(x,null);
        myCursor.moveToFirst();
        if(!myCursor.isAfterLast()) {
            do {
                                //selects data in column 2 to be displayed on list
                String listitem= myCursor.getString(2);

                myownlist.add(listitem);
            } while (myCursor.moveToNext());
        }
        myCursor.close();
    }

这是 setUpList() 函数:

    private void setUpList() {

            setListAdapter(new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, myownlist));
            ListView lv = getListView();
}
于 2013-06-22T10:38:59.487 回答