选择ListView项目并单击按钮自动单击

时间:2013-12-27 17:04:19

标签: android listview select buttonclick

我想知道如何选择listview第一项选择并点击按钮自动点击。

使用以下代码,我可以选择第一行但无法选择。

lv1.setItemChecked(0, true);

这是我的listview点击事件:

final ListView lv1 = (ListView) findViewById(R.id.list);

// Pass results to ListViewAdapter Class
adapter = new ItemListBaseAdapter(this, somedata);
// Binds the Adapter to the ListView
lv1.setAdapter(adapter);
lv1.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

lv1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        //btnNxt.setEnabled(true);
        Intent i = new Intent(getApplicationContext(), FirstActivity.class);
        // Pass all data 
        i.putExtra("mydata", somedata);
        // Pass a single position
        i.putExtra("position", position);
        // Open SingleItemView.java Activity
        startActivity(i);
    }
});

非常感谢任何帮助! 如果有人想要了解更多相关信息,请告诉我,以便我更新我的问题。

3 个答案:

答案 0 :(得分:0)

选择项目与单击项目不同。 如果您想对某个项目的选择做出反应,请使用setOnItemSelectedListener,但如果您想对用户点击其中做出反应,请使用setOnItemClickListener

答案 1 :(得分:0)

更改为

 lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

使用SparseBooleanArray获取已检查的项目

示例:

检查第一行myList.setItemChecked(0, true)

 public class MainActivity extends Activity {

    ListView myList;
    Button getChoice;
    String[] listContent = {"January","February","March","April","May","June", "July","August","September","October","November","December" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myList = (ListView)findViewById(R.id.listView1);
        getChoice = (Button)findViewById(R.id.button1);
        ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,listContent);
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        myList.setAdapter(adapter);
        getChoice.setOnClickListener(new Button.OnClickListener(){
         @Override
         public void onClick(View v) {
                String selected = "";
                int cntChoice = myList.getCount();
                SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
                for(int i = 0; i < cntChoice; i++){
                    if(sparseBooleanArray.get(i)) {
                        selected += myList.getItemAtPosition(i).toString() + "\n";

                    }
                }

                Toast.makeText(MainActivity.this, selected,  Toast.LENGTH_LONG).show();

            }});
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
         >

    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

对齐

enter image description here

答案 2 :(得分:0)

在初始化适配器之前,通过将setChoiceMode设置为listview来尝试。

lv1.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

adapter = new ItemListBaseAdapter(this, somedata);

lv1.setAdapter(adapter);