0

ListViewで遅延ロードの画像を作成しています。StackOverflowで見つけたこのソースのチュートリアルに従い ました。正常に実行されました。

しかし、プロジェクトと一緒にコードを結合すると、問題が発生します。プログラムはOnItemClickListenerを実行しませんでした:(

私のプロジェクトにはTabHostがあり、5つのタブコンテンツがありました。2つのコンテンツはListActivityを使用しており、完全に実行されます。

これが私のコーディング、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;
  }    
}

ImageLoader.javaという名前の別のクラスは、上記のソースリンクを参照してください。私の間違いはどこにあるのか分かりますか?私は私のコードが非常に醜いことを理解しています、私はアンドロイドの初心者です問題を解決するのを手伝ってください。それは私を数日間立ち往生させました。あなたの返事は大歓迎です!!!

P / S:私の英語が下手でごめんなさい、皆さんが私が話していることを理解してくれることを願っています。ありがとうございました。

よろしくWynix

4

2 に答える 2

0

私は問題を解決し、それを解決しました。エラーはxmlファイルにあります。ListView にあるべきではありません

android:focusable="true";方法。

とにかく私の問題を解決しようとしてくれてありがとう。再度、感謝します。乾杯!

ウィニックスについて

于 2010-08-05T04:58:42.570 に答える
0

イベントリスナーを追加する別の手法を使用します。OnCreate メソッドではbtnAdd.setOnClickListener(onAdd);、次のようにイベントに接続するスタンドアロン メソッドを記述して追加します。

private View.OnClickListener onAdd=new View.OnClickListener() {
    public void onClick(View v) {
        // your code here
    }
};

これにより、コード内のエラーを簡単に検索できます。

コードから、個々の項目ではなくリスト全体にイベント リスナーを設定します。代わりに、個々のアイテムにイベントを追加してみてはいかがでしょうか?

于 2010-08-03T08:59:23.070 に答える