2013-04-26 62 views
0

我試圖設置ListView簡單onItemClickListener通過ArrayAdapter監聽器似乎並不ListView中

package it.polito.mad.task2; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 

public class listFragment extends ListFragment { 

public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle b){ 

     View v = li.inflate(R.layout.fraglist , vg, false); //inflate populates a view with desired content 

     //use an adapter to load the imageviews in the fragment 
     ListView listView = (ListView)v.findViewById(R.id.listViewListFrag); //get the resource id for the view to fill with data from adapter 

     ArrayList<opera> pics = JsonLoad("list.json"); 
     operaAdapter adapter = new operaAdapter(this.getActivity(), R.layout.opera, R.id.element, pics);//create the adapter passing the parameter needed to load content to elements in the opera layout 
     listView.setAdapter(adapter); //set the adapter for the view in the fragment 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
        long arg3) { 
       AppObj app = (AppObj) getActivity().getApplication(); 
      Intent goToDet = new Intent(getActivity(),ActivityDetails.class); 

      app.setItem(position); 

      startActivity(goToDet); 

      } 
     }); 


     return v; 
    } 
//more methods 
} 

填充工作,這就是適配器類

package it.polito.mad.task2; 

import java.io.IOException; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

public class operaAdapter extends ArrayAdapter<opera> { 
    private Activity act; 
    ArrayList<opera> list; 
    private static LayoutInflater inflater=null; 

    public operaAdapter(Activity a, int layout, int element, ArrayList<opera> res){//save in local variables content of parameters 

     super(a, layout, res); 
     act = a; 
     list = res; 

    } 
    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public opera getItem(int position) { 
     return list.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView holder; 

     if(convertView==null){ 
      inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.opera, null); 

      holder = (TextView) convertView.findViewById(R.id.element); 
      convertView.setTag(holder); 
     } 
     else{ 
      holder = (TextView) convertView.getTag(); 
     } 

     holder.setText(list.get(position).getTitle()); 

     Drawable d; 
     try { 
      d = Drawable.createFromStream(act.getAssets().open(list.get(position).getImage()), null); 
      d.setBounds(0, 0, 200, 200); //set size of the drawable on the left of the text 
      holder.setCompoundDrawables(d, null, null, null); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     return convertView; 
    } 

} 

我已經嘗試了幾件事情,但聽衆似乎沒有得到任何事件,我錯過了什麼?

+0

如果要檢查是否得到一個斷點或你的東西監聽器裏,然後我會檢查,看看你的適配器是不回來的由於某種原因的空值。 – 2013-04-26 17:06:38

回答

1

您正在設置您的OnItemClickListener,但您沒有對onItemClick回調進行任何操作。嘗試這樣的事情表明,點擊被看出:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), "Position " + position 
        + " clicked", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
+0

對不起,我忘了提及我在那裏開始一個活動,我將它添加到代碼 – hate 2013-04-26 17:52:51

+0

@ user1007179你嘗試添加'Log'或'Toast'(我的例子)來驗證'onItemClick'是被稱爲? – 2013-04-26 18:04:33

+0

是的,它不工作。它看起來像是阻止事件被攔截 – hate 2013-04-26 18:15:42

相關問題