gpt4 book ai didi

android - listView 项目点击事件未触发

转载 作者:行者123 更新时间:2023-11-29 17:45:02 25 4
gpt4 key购买 nike

我在使用 ListView 时遇到问题,当我单击它时没有任何反应。当我单击 listview 项目来 toast 时,我想要它,所以我知道它被单击了。我已经尝试/研究了一段时间但一无所获。有人介意看看我是否遗漏了我刚刚忽略的东西吗?非常感谢!

这是我的类(class):

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();
}

最佳答案

如果 ListView 项上有任何元素,请为它们更改此元素

android:focusable="false"                     

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

关于android - listView 项目点击事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27264368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com