我有一个从服务器获取数据后生成的列表视图。现在我想让每个项目都可以触摸或点击。但它不工作。任何帮助都会得到帮助
这是我的xml文件:
<ListView
android:id="@+id/promo_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
我的活动:
public class Promo extends ListActivity implements OnTouchListener
,OnGestureListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.promotions);
gestureScanner = new GestureDetector(this);
RelativeLayout layMain = (RelativeLayout) findViewById(R.id.main_layout);
layMain.setOnTouchListener((OnTouchListener) this);
listView = (ListView) findViewById(R.id.promo_list);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("---------------------","------------------------------------");
}
});
MyTask obj = new MyTask();
obj.execute("http:www.somelink.com");
}
// ////////////////////////////////////////////////////////////////////
// //////////Background Thread todo Async work////////////////////////
// ////////////////////////////////////////////////////////////////////
private class MyTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPostExecute(String result) {
// JSONObject obj = myList.get(2);
listAdapter = new MyCustomList(getApplicationContext());
listView.setAdapter(listAdapter);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
String myRespone = null;
String url = params[0];
HttpClient client = new DefaultHttpClient();
HttpGet Get = new HttpGet(url);
try {
HttpResponse response = client.execute(Get);
HttpEntity entity = response.getEntity();
myRespone = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("My webservice Response", "ClientProtocolException");
} catch (IOException e) {
Log.e("My webservice Response", "IOException");
e.printStackTrace();
}
JSONObject jsonObj;
if (myRespone != null) {
try {
jsonObj = new JSONObject(myRespone);
JSONArray jsonArray = jsonObj.getJSONArray("Promo");
Log.d("Promo Count", "" + jsonArray.length());
for (int i = 0; i <= jsonArray.length() - 1; i++) {
myList.add(jsonArray.getJSONObject(i));
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
// do nothing
}
return null;
}
}
private static class Promo {
TextView name1, name2, name3, name4;
}
private class MyCustomList extends BaseAdapter implements OnItemClickListener{
private Context context;
LayoutInflater inflater;
public MyCustomList(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Promo promotion;
if (view == null) {
view = inflater.inflate(R.layout.promo_listview, null);
promotion = new Promo();
view.setTag(promotion);
} else {
promo = (Promo) view.getTag();
}
return view;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
}