如何让项目在列表视图中点击

如何让项目在列表视图中点击

本文介绍了如何让项目在列表视图中点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图寻找一个解决方案,但很少成功。
我要显示弹出窗口包含的项目列表。
我能够显示窗口,但 onitemclicklistener 没有被要求单击列表视图的项目。这个问题的任何帮助将大大AP preciated。

I have been trying to search for a solution, but with very little success.I have to display a pop up window containing a list of items.I was able to display the window but onitemclicklistener is not being called upon clicking an item in the list view. Any help with this issue would be greatly appreciated.

感谢

EDIT1:

public class PopUpWindowActivity extends Activity {

    /** Called when the activity is first created. */
    String[] countries = new String[] {
        "India", "USA", "Canada"
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ListView lv = new ListView(this);
        lv.setAdapter(new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, countries));
        lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView <? > arg0, View arg1, int arg2,
                    long arg3) {
                    Log.v("clicked", (String)((TextView) arg1).getText());
                }

            });
        ll.addView(lv);
        setContentView(ll);
    }
}

在上面的code,我试图创建布局里面,我增加了一个列表视图。这使得列表视图不再点击。我必须这样做,因为,我想实现一个弹出窗口,其内部应该有多个项目进行列表视图一起。

In the above code, i tried to create a layout inside which i added a list view. This makes the list view no longer clickable. I have to do this because, i am trying to implement a pop up window inside which there should be multiple items along with a list view.

推荐答案

编辑答案

应用此样品请这对你的作品我已经测试这code

Apply This Sample Please This Works For You I have Tested This Code

// ListviewActivity.java

//ListviewActivity.java

 package com.anl.lk;

 public class ListviewActivity extends ListActivity {

     static final String[] COUNTRIES = new String[] {

         "Afghanistan", "Albania", "Algeria", "American Samoa",
             "Andorra", "Angola", "Anguilla", "Antarctica",
             "Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
             "Australia", "Austria", "Azerbaijan", "Bahrain",
             "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
             "Benin", "Bermuda", "Bhutan", "Bolivia",
             "Bosnia and Herzegovina", "Botswana", "Bouvet Island",
             "Brazil", "British Indian Ocean Territory"
     };

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setListAdapter(new ArrayAdapter < String > (this,
                 android.R.layout.simple_list_item_1, COUNTRIES));
         getListView().setTextFilterEnabled(true);
     }

     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
         // TODO Auto-generated method stub
         super.onListItemClick(l, v, position, id);

         new AlertDialog.Builder(this)
             .setTitle("Hello")
             .setMessage("from " + getListView().getItemAtPosition(position))
             .setPositiveButton("OK",
             new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {}
             })
             .show();

         Toast.makeText(ListviewActivity.this,
             "ListView: " + l.toString() + "\n" +
             "View: " + v.toString() + "\n" +
             "position: " + String.valueOf(position) + "\n" +
             "id: " + String.valueOf(id),
             Toast.LENGTH_LONG).show();
     }

 }

//第一页

package com.anl.lk;

public class FirstPage extends Activity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent sam = new Intent(FirstPage.this, ListviewActivity.class);
                    startActivity(sam);

                }
            });
    }
}

//清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anl.lk"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".FirstPage" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ListviewActivity"    android:theme="@android:style/Theme.Dialog"></activity>
</application>

请使用此code这有你的解决方案

Please Use This Code it have your Solution

这篇关于如何让项目在列表视图中点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:41