下面你可以看到代码,我为listview实现了一个简单的适配器。但我无法进入onListItemClick。任何人都有建议吗?
实际上它正常显示列表,但我无法获得onitemclick事件。提前谢谢。
public class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Frame> results = WebOperations
.loadList_Frame();
myAdapter = new MyListAdapter(MyListActivity.this);
myAdapter.internalList = results;
setListAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show();
}
public static class MyListAdapter extends BaseAdapter {
public ArrayList<Frame> internalList;
public LayoutInflater mInflater;
public int pageCount = 0;
public MyListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
if (internalList == null)
return 0;
return internalList.size();
}
public Object getItem(int position) {
if (internalList == null || internalList.size() < position)
return null;
return internalList.get(position);
}
public long getItemId(int position) {
if (internalList == null || internalList.size() < position)
return 0;
return internalList.get(position).getId();
}
public View getView(int position, View arg1, ViewGroup parent) {
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.entryrow, null);
try {
String gunlukText = String.format(" %s ",
internalList.get(position).getKeyText().toString());
TextView entry = (TextView) v
.findViewById(R.id.textViewEntryText);
entry.setText((CharSequence) gunlukText);
} catch (Exception e) {
Log.d("aaaaaaaaaaaaaaaaaaa", "errorrrrrrrrrrr");
}
}
return v;
}
}
}
编辑1:我在下面添加了entry_row布局xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewEntryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:padding="5dp"
android:paddingLeft="10dp"
android:text="@string/Ana_Sayfa"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
答案 0 :(得分:2)
您应该考虑将您的监听器添加到列表视图中:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:1)
您是否尝试在布局/输入行中添加addStatesFromChildren属性并将其设置为true?
答案 2 :(得分:1)
在返回视图之前将onclicklistener添加到getView方法中。
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.w("position", position + "");
}
});
检查它是否有帮助..
答案 3 :(得分:0)
请参考http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List13.html自定义BaseAdapter的API演示中给出的一个好示例,只需覆盖onListItemCLicked
并检查即可。它工作得很好。尝试相应地修改您的代码。