我試圖設置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;
}
}
我已經嘗試了幾件事情,但聽衆似乎沒有得到任何事件,我錯過了什麼?
如果要檢查是否得到一個斷點或你的東西監聽器裏,然後我會檢查,看看你的適配器是不回來的由於某種原因的空值。 – 2013-04-26 17:06:38