OnItemClickListener Problem
i'm creating a lazy load of image in ListView. i was followed the tutorial from this source which i found in Stack Overflow It was run successful.
But, when i join the code together with my project then i face a problem. the program was no perform the OnItemClickListener :(
my project have a TabHost and it had 5 tab contents. 2 contents is using the ListActivity and run perfectly.
here is my coding, Main.java:
public class ProductListing extends Activity {
ListView list;
MyListAdapter adapter;
Controller c;
ImageLoader imageLoader;
TextView select;
//========== JSON ===========
ArrayList<String> strName = new ArrayList<String>();
ArrayList<String> strImage = new ArrayList<String>();
ArrayList<String> strDesc = new ArrayList<String>();
ArrayList<String> strSize = new ArrayList<String>();
JSONObject jsonObject;
String[] listItem;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadJSON();
setContentView(R.layout.productlisting_tab);
list=(ListView)findViewById(R.id.ListView01);
c = new Controller(this);
adapter=new MyListAdapter(this,this, strName, strImage,strDesc,strSize);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println("Item Clicked");
}
});
}
public void LoadJSON(){
try {
InputStream is = this.getResources().openRawResource(R.raw.premium);
byte[] buffer;
buffer = new byte[is.available()];
while(is.read(buffer) != -1);
String jsonText = new String(buffer);
jsonObject = new JSONObject(jsonText);
JSONObject premium_tab = jsonObject.getJSONObject("premium_tab");
int totalItem = premium_tab.getInt(".total");
for (int i = 1; i <= totalItem; i++) {
JSONObject premium = premium_tab.getJSONObject("premium_"+i);
String tempName =premium.getString(".name").toString();
String tempImg = premium.getString(".image").toString();
String tempDesc = premium.getString(".desc").toString();
String tempSize = premium.getString(".size").toString();
strName.add(tempName);
strImage.add(tempImg);
strDesc.add(tempDesc);
strSize.add(tempSize);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MyListAdapter.java:
public MyListAdapter(Context b,Activity a, ArrayList<String> strName, ArrayList<String> strImage,
ArrayList<String> strDesc, ArrayList<String> strSize) {
activity = a;
name = strName;
image = strImage;
desc = strDesc;
size = strSize;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return image.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView ProductName,ProductSize, ProductDesc;
public ImageView ProductIcon;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.productlisting, null);
holder=new ViewHolder();
holder.ProductName=(TextView)vi.findViewById(R.id.text);
holder.ProductIcon=(ImageView)vi.findViewById(R.id.image);
holder.ProductDesc=(TextView)vi.findViewById(R.id.textdesc);
holder.ProductSize=(TextView)vi.findViewById(R.id.textsize);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.ProductName.setText(name.get(position));
holder.ProductDesc.setText(desc.get(position));
holder.ProductIcon.setTag(image.get(position));
holder.ProductSize.setText(size.get(position));
imageLoader.DisplayImage(image.get(position), activity, holder.ProductIcon);
return vi;
}
}
Another class which name ImageLoader.java please refer the source link above. May i know where is my mistake? i understand my code will very ugly, i'm a new in android please help me to solve the problem. it was stuck me for few days. your reply is very appreciated !!!
P/S: I'm sorry about my bad english, hope开发者_高级运维 you guys understand what i'm talking about. Thank you.
Regard Wynix
I use a different technique adding event-listeners. In the OnCreate-method i write btnAdd.setOnClickListener(onAdd);
and adding a stand-alone method to hook up to the event like this:
private View.OnClickListener onAdd=new View.OnClickListener() {
public void onClick(View v) {
// your code here
}
};
This makes it easier to search for errors in your code.
From your code, you set the event-listener to the entire list, instead of each individual item. Maybe you should try adding events to individual items instead?
I have solve the problem and solve it. the erroe is on the xml file. in ListView should NOT have
android:focusable="true";
menthod.
anyway Thanks for trying to fix my problem. Thanks again. Cheers!
Regard Wynix
精彩评论