gpt4 book ai didi

java - Listview 上的 setOnItemClickListener 扩展 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:26 26 4
gpt4 key购买 nike

更新:我找到了解决方案删除

v.setFocusable(true);v.setClickable(true); 代码中

只添加

v.setEnabled(true);

并在我的 xml (ListView) 中添加

android:drawSelectorOnTop="true"android:focusable="true"


当我点击它时没有任何反应,甚至 ListView 也没有聚焦。

我已经尝试添加所有这些:

           v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);

只有我添加以下代码才能起作用:

但这并不能确定点击的是什么项目

How to handle ListView click in Android

结果还是一样。

这是代码:

public class AppsInspectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//[...]
ListView app_listView = (ListView)findViewById(R.id.listview);

// I try setOnItemClickListene here - 1
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();

}
});
}

适配器

 public class AppAdapter extends BaseAdapter {

Context context;
ArrayList<AppInfo> dataList=new ArrayList<AppInfo>();
public AppAdapter(Context context,ArrayList<AppInfo> inputDataList)
{

this.context = context;
dataList.clear();
for(int i=0;i<inputDataList.size();i++)
{
dataList.add(inputDataList.get(i));
}
}
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

View v=convertView;
final AppInfo appUnit = dataList.get(position);

ListView app_listView = (ListView)findViewById(R.id.listview);

/** Remove this , i just try to add to see setOnItemClickListener will work at here or not **/
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});

if(v==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.app_row, null);
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
}


TextView appName=(TextView)v.findViewById(R.id.appName);
ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);

if(appName!=null)
appName.setText(appUnit.appName);
if(appIcon!=null)
appIcon.setImageDrawable(appUnit.appIcon);
return v;
}
}
}
}

最佳答案

能否请您发布 main.xml 布局。在我看来,您在 ListView 顶部还有另一个 View 正在使用触摸事件。

关于java - Listview 上的 setOnItemClickListener 扩展 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382161/

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