listView项单击事件未触发

时间:2014-12-03 05:34:15

标签: android listview android-listview onclick onclicklistener

我在使用listview时遇到问题,当我点击它时没有发生任何事情。当我点击listview项目来祝酒时我想要它,所以我知道它被点击了。我一直在尝试/研究一段时间而没有。有人会介意看看我是否遗漏了一些我忽略的东西?非常感谢提前!

这是我的班级:

public class MyCar extends Activity {

/**
 * Called when the activity is first created.
 */
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mycar);
    mylistView = (ListView) findViewById(R.id.listView);
    arrayListCar = new ArrayList<String>();
    //Just had to remove setting this adapter 2 times.  Took out line below to fix.
    mylistView.setAdapter(mylistAdapter);

    mylistView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            String item = ((TextView) view).getText().toString();

            Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

        }
    });
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    carInfo = trySomethin();
    fillList();
}

public void fillList() {
    String make = "";
    String model = "";

    String[] pieces = carInfo.split("\"");
    make = pieces[3];
    model = pieces[7];

    ArrayList<String> carList = new ArrayList<String>();
    carList.add(make + " " + model);
    // Create ArrayAdapter using the car list.
    mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);

    mylistView.setAdapter(mylistAdapter);
    mylistAdapter.notifyDataSetChanged();
}

}

2 个答案:

答案 0 :(得分:2)

如果listview项目中有任何元素,则为其更改

android:focusable="false"                     

如果要更改运行时的任何元素可见性,则每次更改其可见性时都必须以编程方式处理焦点。

答案 1 :(得分:1)

 Try this 


        listView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

           // ListView Clicked item index
           int itemPosition     = position;

           // ListView Clicked item value
           String  itemValue    = (String) listView.getItemAtPosition(position);              
            // Show Alert 
            Toast.makeText(getApplicationContext(),
              "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
             .show();

          }
        });