Android:ListView未使用McClickListener
我使用的是类的片段类型,但我的ListView没有使用onItemclickListner,如何在ListView上使用onclick事件。我已经试过focusable=false,但不起作用。请给我建议一些解决方案Android:ListView未使用McClickListener,android,Android,我使用的是类的片段类型,但我的ListView没有使用onItemclickListner,如何在ListView上使用onclick事件。我已经试过focusable=false,但不起作用。请给我建议一些解决方案 public class HomePage extends SherlockListFragment { String postid, date, title, content, image; String PostID,Date,Title,Image, Cont
public class HomePage extends SherlockListFragment {
String postid, date, title, content, image;
String PostID,Date,Title,Image, Content;
TextView detail;
ListView lv;
List<String> list = new ArrayList<String>();
List<String> listtitle = new ArrayList<String>();
List<String> listDetails = new ArrayList<String>();
List<Drawable> cIcons = new ArrayList<Drawable>();
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);;
ActionBar ab = getSherlockActivity().getSupportActionBar();
ab.getDisplayOptions();
ab.setDisplayShowTitleEnabled(true);
ab.setDisplayUseLogoEnabled(true);
ab.setTitle("मुख्य पृष्ठ");
ab.setIcon(R.drawable.duk);
ProcessIt p = new ProcessIt();
p.execute();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.homepage, container, false);
lv = (ListView) view.findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
list.add(title+image+date+postid);
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(),NewPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
return view;
}
private class ProcessIt extends AsyncTask<String, Void, String> {
private ProgressDialog prog = new ProgressDialog(getActivity());
@Override
protected String doInBackground(String... param) {
try {
String response = CustomHttpClient.executeHttpGet
("http://*********api.aspx?function=GetPost");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(response));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("postid")) {
postid = xpp.nextText();
PostID = postid.trim();
} else if (xpp.getName().equalsIgnoreCase("date")) {
date = xpp.nextText();
Date = date.trim();
} else if (xpp.getName().equalsIgnoreCase("title")) {
title = xpp.nextText();
Title = title.trim();
} else if (xpp.getName().equalsIgnoreCase("content")) {
content = xpp.nextText();
Content = content.trim();
list.add(Title+Image+Date+PostID);
listtitle.add(Title);
cIcons.add(getActivity().getResources().getDrawable(R.drawable.ic_icon));
listDetails.add(Content+"\n"+Date + " << detail");
} else if (xpp.getName().equalsIgnoreCase("image")) {
image = xpp.nextText();
Image = image.trim();
}
}
eventType = xpp.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
if (prog.isShowing()) {
prog.dismiss();
}
if(list.size()<=0){
list.add(Title+Image+Date+PostID);
listtitle.add(Title);
cIcons.add(getActivity().getResources().getDrawable(R.drawable.ic_star));
}
handler.sendEmptyMessage(1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
prog.setMessage("Loading...Please Wait");
prog.show();
prog.setCanceledOnTouchOutside(true);
}
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
handler.sendEmptyMessage(1);
}
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (msg.what == 1) {
CustomListAdaptor adaptor = createAdapter();
if (adaptor != null) {
lv.setAdapter(adaptor);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public CustomListAdaptor createAdapter() {
CustomListAdaptor adapter = null;
if (list != null) {
adapter = new CustomListAdaptor(getActivity(), R.layout.list_item, list, listtitle, listDetails,cIcons);
}
return adapter;
}
}
类文件的布局在这里
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/firstheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/txt1"
android:textColor="#D12F2F"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="25sp"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nextFocusDown="@+id/firstheading"
android:layout_gravity="center_vertical"
android:cacheColorHint="#00000000"
android:divider="#666666"
android:dividerHeight="2.0sp"
android:drawSelectorOnTop="false"/>
首先,请告诉我您是否正确显示了ListView?发布您的主页布局xml文件。您是否为ListView设置了focusable=false?是的,我尝试了focusable=false。最后,在OnActivitycreated方法中使用它,使ListView监听器正常工作…: