2013-07-20 38 views
1

我有從服務器獲取數據後生成的列表視圖。現在我想讓每件物品都能夠觸摸或點擊。但它不起作用。任何幫助將appricitedOnClickItem不工作Android

這裏是我的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 



     } 

     } 
} 
+0

變化的新OnItemClickListener()到新View.OnItemClickListener() – KOTIOS

+0

我將開始評論此行layMain.setOnTouchListener((OnTouchListener)本); – Blackbelt

+0

這兩個解決方案都不起作用... – User42590

回答

1

您可以在膨脹的觀點適用onClickListner按照後續再沒有必要實施OnItemClickListener 。

private class MyCustomList extends BaseAdapter{ 

    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(final 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(); 
     } 

     view..setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       System.out.println("position=="+position); 
      } 
     }); 

     return view; 
    } 


    }` 
+0

中感謝它的工作.... – User42590

0

試試這個代碼,

public class Promo extends ListActivity{ 
    ArrayList<String> myList=null; 
    @Override 
     protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.promotions); 
    listView = (ListView) findViewById(R.id.promo_list); 
    myList=new ArrayList<String>(); 
    MyTask obj = new MyTask(); 
    obj.execute("http:www.somelink.com"); 
    listView.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
     { 
      System.out.println("Name>>>>>>>"+myList.get(position)); 
          //Enter Code Here 
     } 
    }); 
} 
// //////////////////////////////////////////////////////////////////// 
// //////////Background Thread todo Async work//////////////////////// 
// //////////////////////////////////////////////////////////////////// 
private class MyTask extends AsyncTask<String, Integer, String> { 
    @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; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     // JSONObject obj = myList.get(2); 
     listAdapter = new MyCustomList(getApplicationContext(),myList); 
     listView.setAdapter(listAdapter); 
     super.onPostExecute(result); 
    } 
} 
public class MyCustomList extends BaseAdapter{ 
private ArrayList<String> myList=null; 
private LayoutInflater mInflater=null; 

public MyCustomList(Context context, ArrayList<String> results) { 
     myList= results; 
     mInflater = LayoutInflater.from(context); 
} 
public int getCount() 
{ 
    return myList.size(); 
} 
public Object getItem(int position) 
{ 
    return myList.get(position); 
} 
public long getItemId(int position) 
{ 
    return position; 
} 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder; 
    if (convertView == null) 
    { 
    convertView =mInflater.inflate(R.layout.R.layout.promo_listview, null); 
    holder = new ViewHolder(); 
     holder.Name= (TextView) convertView.findViewById(R.id.txtvName); 
    } 
    else 
    { 
    holder = (ViewHolder) convertView.getTag(); 
    } 
     holder.Name.setText(myList.get(position)); 
} 
static class ViewHolder 
{ 
     TextView Name=null; 
} 
} 
} 
}