程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-List View 上的 Item Click 事件

listview-List View 上的 Item Click 事件

編輯:編程綜合問答
List View 上的 Item Click 事件

我設置了List view,在Linear 布局中有一個按鈕。使用 Onlistitemclick 不能繼承ListActivity,但我需要在list view中選擇一個item 來打開另外一個視圖,然後再添加SQLite DB 的內容。如何實現?

public class Contacts extends Activity implements OnClickListener {
    int NewContact_Request_Code = 1;
    Button newcontact;
    ListView listview;
    public static final String LOG_TAG = "Contacts";
    int mInt = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactview);
        // Set the content to contactview.xml

        // newcontact = NEW CONTACT button
        // listview = MyList List View
        newcontact = (Button) findViewById(R.id.baddcontact);
        listview = (ListView) findViewById(R.id.mylist);

        // Make a New Database
        DBContact info = new DBContact(this);
        // Open , get Information from database and close it
        info.open();
        String[] data = info.queryAll();
        info.close();
        // listview = getListView();
        listview.setTextFilterEnabled(true);
        // Display the names
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(Contacts.this, android.R.layout.simple_list_item_1, data);
         listview.setAdapter(adapter);
        newcontact.setOnClickListener(this);
    }
    // @Override
    // protected void onListItemClick(ListView l, View v, int position, long id)
    // {
    // // TODO Auto-generated method stub
    // super.onListItemClick(l, v, position, id);
    // Intent viewintent = new Intent(Contacts.this, ViewContact.class);
    // String item = (String) getListAdapter().getItem(position);
    // viewintent.putExtra("name_clicked", item);
    // startActivity(viewintent);
    //
    // }
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent newintent = new Intent(Contacts.this, AddNewContact.class);
        // start actiivity for result - to get the name of the new contact
        startActivityForResult(newintent, 0);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        // pass the value of the string via cursor and update the list
    }
}

最佳回答:


        listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            Log.d("TEST","You choosed the - "+arg2);//arg2 is the ListView's clicked index
            //setContentView(R.layout.another_view);//view changing
            //updateDatabase(); //DB operation
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }

    });
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved