我真的是新手android。 现在我尝试使用Header部分创建一个简单的自定义列表视图。 但是在我运行程序后,listview项目无法点击。 这是我的代码:
MainActivity.java
String procName= "{call PSTATISTIC}";
CallableStatement cs = conn.prepareCall(procName);
cs.executeQuery();
CustomAdapter.java
package io.hidayat.headerlistview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private CustomAdapter mAdapter;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.list_header_detail);
mAdapter = new CustomAdapter(this);
mAdapter.addSectionHeaderItem("Section #1");
for (int i = 1; i < 30; i++) {
mAdapter.addItem("Row Item #" + i);
if (i % 4 == 0) {
mAdapter.addSectionHeaderItem("Section #" + i);
}
}
listView.setAdapter(mAdapter);
}
}
标题和行XML是类似的。
package io.hidayat.headerlistview;
import java.util.ArrayList;
import java.util.TreeSet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
class CustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.row, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.header, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
public static class ViewHolder {
public TextView textView;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:gravity="center_vertical"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF000000" />
</LinearLayout>
我是sory,但这是我的应用程序的屏幕截图:Renaud Bompuis's blog
答案 0 :(得分:4)
您尚未应用OnItemClickListener
。那就是问题所在。别担心。您可以看到以下示例,根据需要更改代码。
首先使用Class
OnItemClickListener
public class MainActivity extends Activity implements OnItemClickListener
然后将OnItemClickListener
附加到您的list
listView.setOnItemClickListener(YourClassName.this);
然后覆盖onItemClick
方法,以便在点击position
时实现您的功能。
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String item = adapter.getItemAtPosition(position).toString();
Toast.makeText(Test.this, "CLICK: " + item, Toast.LENGTH_SHORT).show();
}
如果您遇到任何麻烦,请告诉我。感谢。
答案 1 :(得分:1)
在listview上调用以下方法
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:1)
行项目必须有一个像android:descendantFocusability="blocksDescendants".
这样的参数用于您的行项目父级..它适用于具有CustomAdapter的列表视图..