2

我在线性布局中有一个列表视图和一个按钮。我无法扩展 ListActivity(使用 Onlistitemclick),但我必须在列表视图中选择一个项目,这会打开另一个视图,我必须在其中填写 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
    }

}
4

4 回答 4

7

您可以将OnItemClickListener设置为ListView如下所示

listview.setOnItemClickListener(new OnItemClickListener() {

  public void onItemClick(AdapterView adapterView, View view, int position, long id) {

        //Do some more stuff here and launch new activity

  }
});
于 2012-09-26T05:52:41.257 回答
2

您可以onItemClickListener明确地设置它,如下所示:

listview.setOnItemClickListener(listener);

哪里listenerAdapterView.OnItemClickListener

于 2012-09-26T05:43:37.140 回答
0

用于OnItemClickListener()列表视图无需扩展此活动ListActivity

listview.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {          

   }
});
于 2012-09-26T05:48:33.783 回答
0
private final ListView.OnItemClickListener ListClick = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        // your action
    }
};

并以这种方式设置

listview.setOnItemClickListener(ListClick);
于 2012-09-26T05:50:43.970 回答