hcg_21 2013-05-07 09:52 采纳率: 0%
浏览 5125
已采纳

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
    }
}
  • 写回答

2条回答

  • franzhong 2013-05-07 12:04
    关注
            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条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧