开发中遇到一个很奇怪的问题,列表中的ITEM如果不满屏点击事件就不起作用。代码如下,求教各位老大。
package com.peihuobao;import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;import com.peihuobao.R;
import com.peihuobao.utils.InfoParser;
import com.peihuobao.utils.MyHttpClient;public class SearchResults extends Activity implements OnItemClickListener{
private static final int UPDATE_LISTVIEW = 1;
private static final int ERROR = 0;
private static final int THREADPOOL_SIZE = 4;
private int last_id = -1;
private boolean shouldRefresh = false;
private ListView myList;
private SimpleAdapter adapter = null;
private View loadingView = null;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Activity mActivity;
private Handler handler;
private ExecutorService executorService = null;
private Button refresh;
private String fromprovince = "";
private String fromcity = "";
private String fromcounty = "";
private String toprovince = "";
private String tocity = "";
private String tocounty = "";
private String infotype = "goods";
private String userid = "";
private JSONArray jsonsaver = new JSONArray();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.requestWindowFeature(Window.FEATURE_NO_TITLE);
mActivity = this;
Intent intent = getIntent();
fromprovince = intent.getStringExtra("fromprovince");
fromcity = intent.getStringExtra("fromcity");
fromcounty = intent.getStringExtra("fromcounty");
toprovince = intent.getStringExtra("toprovince");
tocity = intent.getStringExtra("tocity");
tocounty = intent.getStringExtra("tocounty");
infotype = intent.getStringExtra("infotype");
userid = intent.getStringExtra("userid");
initUI();
} private void initUI() {
setContentView(R.layout.search_results);
String titletemp = "";
if(infotype.equals("goods")){
titletemp = "货源信息";
}else if(infotype.equals("trucks")){
titletemp = "车源信息";
}
TextView maintitle = (TextView) findViewById(R.id.maintitle);
maintitle.setText(titletemp);
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(LAYOUT_INFLATER_SERVICE);
loadingView = inflater.inflate(R.layout.listloading, null);
myList = (ListView) findViewById(R.id.datalist);
myList.setOnScrollListener(new MyScrollListener());
adapter = new SimpleAdapter(this,
mylist,
R.layout.listitem,
new String[] { "desc", "pubdate" },
new int[] { R.id.item_desc, R.id.item_pubdate }
);
myList.setAdapter(adapter);
myList.setOnItemClickListener(this);
handler = new ListHandler();
refresh = (Button)findViewById(R.id.refresh);
refresh.setEnabled(false);
executorService = Executors.newFixedThreadPool(THREADPOOL_SIZE);
executorService.submit(new ListThread());
refresh.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v) {
v.setEnabled(false);
mylist.clear();
last_id = -1;
myList.addFooterView(loadingView);
executorService.submit(new ListThread());
}
}
);
Button backtoprev = (Button)findViewById(R.id.back);
backtoprev.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
}
);
} class MyScrollListener implements OnScrollListener { public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount == totalItemCount){
if (myList.getFooterViewsCount() == 0) {
myList.addFooterView(loadingView);
} else {
shouldRefresh = true;
}
}
} public void onScrollStateChanged(AbsListView view, int scrollState) {
if (shouldRefresh && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
executorService.submit(new ListThread());
}
}
}
class ListThread implements Runnable {
public void run() {
boolean code = true;
String info = "数据加载成功"; try {
String requrl = getResources().getString(R.string.app_baseurl)+"info_pages.php?" +
"&infotype=" + infotype +
"&fromprovince=" + fromprovince +
"&fromcity=" + fromcity +
"&fromcounty=" + fromcounty +
"&toprovince=" + toprovince +
"&tocity=" + tocity +
"&tocounty=" + tocounty +
"&last_id="+last_id +
"&userid="+userid;
Log.e(">>>>>>>ANYUNDEBUG", requrl);
MyHttpClient mhc = new MyHttpClient();
mhc.CheckNetwork(mActivity);
String result = mhc.getData(requrl, mActivity);
result = result.substring(result.indexOf("{"),
result.lastIndexOf("}") + 1);
buildList(new JSONObject(result));
Log.e(">>>>>>>ANYUNDEBUG", result);
} catch (Exception e) {
code = false;
info = "数据加载失败";
Log.e(">>>>>>>ANYUNDEBUG", e.toString());
}
//通知handler处理数据
Message msg = handler.obtainMessage();
if(code){
msg.what = UPDATE_LISTVIEW;
}else{
msg.what = ERROR;
}
msg.obj = info;
handler.sendMessage(msg);
}
}
class ListHandler extends Handler { @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case UPDATE_LISTVIEW:
refresh.setEnabled(true);
adapter.notifyDataSetChanged();
myList.removeFooterView(loadingView);
break;
case ERROR:
Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_SHORT).show();
myList.removeFooterView(loadingView);
break;
}
}
}
//构建列表
private void buildList(JSONObject json){
try{
JSONArray datalist = json.getJSONArray("infos"); for(int i=0;i<datalist.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = datalist.getJSONObject(i);
map.put("id", e.getString("aid"));
if(infotype.equals("goods")){
map.put("desc", InfoParser.parseGood(e));
}else if(infotype.equals("trucks")){
map.put("desc", InfoParser.parseTruck(e));
}
map.put("pubdate", e.getString("pubdate"));
mylist.add(map);
jsonsaver.put(e);
}
last_id = json.getInt("last_id");
}catch(JSONException e){
Log.e(">>>>>>>ANYUNDEBUG", "列表构建失败:"+e.toString());
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent = new Intent(SearchResults.this, InfoDetails.class);
try {
intent.putExtra("infotype", infotype);
intent.putExtra("details", jsonsaver.getJSONObject(position).toString());
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.peihuobao;import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;import com.peihuobao.R;
import com.peihuobao.utils.InfoParser;
import com.peihuobao.utils.MyHttpClient;public class SearchResults extends Activity implements OnItemClickListener{
private static final int UPDATE_LISTVIEW = 1;
private static final int ERROR = 0;
private static final int THREADPOOL_SIZE = 4;
private int last_id = -1;
private boolean shouldRefresh = false;
private ListView myList;
private SimpleAdapter adapter = null;
private View loadingView = null;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Activity mActivity;
private Handler handler;
private ExecutorService executorService = null;
private Button refresh;
private String fromprovince = "";
private String fromcity = "";
private String fromcounty = "";
private String toprovince = "";
private String tocity = "";
private String tocounty = "";
private String infotype = "goods";
private String userid = "";
private JSONArray jsonsaver = new JSONArray();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.requestWindowFeature(Window.FEATURE_NO_TITLE);
mActivity = this;
Intent intent = getIntent();
fromprovince = intent.getStringExtra("fromprovince");
fromcity = intent.getStringExtra("fromcity");
fromcounty = intent.getStringExtra("fromcounty");
toprovince = intent.getStringExtra("toprovince");
tocity = intent.getStringExtra("tocity");
tocounty = intent.getStringExtra("tocounty");
infotype = intent.getStringExtra("infotype");
userid = intent.getStringExtra("userid");
initUI();
} private void initUI() {
setContentView(R.layout.search_results);
String titletemp = "";
if(infotype.equals("goods")){
titletemp = "货源信息";
}else if(infotype.equals("trucks")){
titletemp = "车源信息";
}
TextView maintitle = (TextView) findViewById(R.id.maintitle);
maintitle.setText(titletemp);
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(LAYOUT_INFLATER_SERVICE);
loadingView = inflater.inflate(R.layout.listloading, null);
myList = (ListView) findViewById(R.id.datalist);
myList.setOnScrollListener(new MyScrollListener());
adapter = new SimpleAdapter(this,
mylist,
R.layout.listitem,
new String[] { "desc", "pubdate" },
new int[] { R.id.item_desc, R.id.item_pubdate }
);
myList.setAdapter(adapter);
myList.setOnItemClickListener(this);
handler = new ListHandler();
refresh = (Button)findViewById(R.id.refresh);
refresh.setEnabled(false);
executorService = Executors.newFixedThreadPool(THREADPOOL_SIZE);
executorService.submit(new ListThread());
refresh.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v) {
v.setEnabled(false);
mylist.clear();
last_id = -1;
myList.addFooterView(loadingView);
executorService.submit(new ListThread());
}
}
);
Button backtoprev = (Button)findViewById(R.id.back);
backtoprev.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
}
);
} class MyScrollListener implements OnScrollListener { public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount == totalItemCount){
if (myList.getFooterViewsCount() == 0) {
myList.addFooterView(loadingView);
} else {
shouldRefresh = true;
}
}
} public void onScrollStateChanged(AbsListView view, int scrollState) {
if (shouldRefresh && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
executorService.submit(new ListThread());
}
}
}
class ListThread implements Runnable {
public void run() {
boolean code = true;
String info = "数据加载成功"; try {
String requrl = getResources().getString(R.string.app_baseurl)+"info_pages.php?" +
"&infotype=" + infotype +
"&fromprovince=" + fromprovince +
"&fromcity=" + fromcity +
"&fromcounty=" + fromcounty +
"&toprovince=" + toprovince +
"&tocity=" + tocity +
"&tocounty=" + tocounty +
"&last_id="+last_id +
"&userid="+userid;
Log.e(">>>>>>>ANYUNDEBUG", requrl);
MyHttpClient mhc = new MyHttpClient();
mhc.CheckNetwork(mActivity);
String result = mhc.getData(requrl, mActivity);
result = result.substring(result.indexOf("{"),
result.lastIndexOf("}") + 1);
buildList(new JSONObject(result));
Log.e(">>>>>>>ANYUNDEBUG", result);
} catch (Exception e) {
code = false;
info = "数据加载失败";
Log.e(">>>>>>>ANYUNDEBUG", e.toString());
}
//通知handler处理数据
Message msg = handler.obtainMessage();
if(code){
msg.what = UPDATE_LISTVIEW;
}else{
msg.what = ERROR;
}
msg.obj = info;
handler.sendMessage(msg);
}
}
class ListHandler extends Handler { @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case UPDATE_LISTVIEW:
refresh.setEnabled(true);
adapter.notifyDataSetChanged();
myList.removeFooterView(loadingView);
break;
case ERROR:
Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_SHORT).show();
myList.removeFooterView(loadingView);
break;
}
}
}
//构建列表
private void buildList(JSONObject json){
try{
JSONArray datalist = json.getJSONArray("infos"); for(int i=0;i<datalist.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = datalist.getJSONObject(i);
map.put("id", e.getString("aid"));
if(infotype.equals("goods")){
map.put("desc", InfoParser.parseGood(e));
}else if(infotype.equals("trucks")){
map.put("desc", InfoParser.parseTruck(e));
}
map.put("pubdate", e.getString("pubdate"));
mylist.add(map);
jsonsaver.put(e);
}
last_id = json.getInt("last_id");
}catch(JSONException e){
Log.e(">>>>>>>ANYUNDEBUG", "列表构建失败:"+e.toString());
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent = new Intent(SearchResults.this, InfoDetails.class);
try {
intent.putExtra("infotype", infotype);
intent.putExtra("details", jsonsaver.getJSONObject(position).toString());
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货