0

ああ、ここで何が間違っているのですか。リストビューに onItemClickListener を設定し、onClick を別のアクティビティに設定しましたが、メソッド OnItemClicklistener を使用する必要があることがわかりました...

package org.iimed.www;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import org.iimed.www.R;
import org.iimed.www.CustomListViewAdapter;
import org.iimed.www.RowItem;

public class ImageTextListViewActivity extends Activity implements
        OnItemClickListener {

    public static final String[] titles = new String[] { "Penicillins",
            "Cephalosporins", "Monobactams", "Carbapenems","Macrolides","Tetracyclines",};

    public static final String[] descriptions = new String[] {
            "It is an aggregate accessory fruit",
            "It is the largest herbaceous flowering plant", "Citrus Fruit",
            "Mixed Fruits","none","none" };

    public static final Integer[] images = { R.drawable.antibiotic_buton,
            R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };

    ListView listView;
    List<RowItem> rowItems;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screj);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
            rowItems.add(item);


        listView = (ListView) findViewById(R.id.list);
        CustomListViewAdapter adapter = new CustomListViewAdapter(this,
                R.layout.list_item, rowItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

               @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                   {
             Intent intent = null ;
                switch (arg2) {
            case 0:
                intent  = new Intent(ImageTextListViewActivity.this, MainActivity.class);
                startActivity(intent);          
                break;
            case 1:
            intent = new Intent(ImageTextListViewActivity.this, Miipet.class);
            startActivity(intent);
                break;
            default:
                break;



                }}}}

アダプター:

package org.iimed.www;


import java.util.List;
import org.iimed.www.R;
import org.iimed.www.RowItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

   Context context;

   public CustomListViewAdapter(Context context, int resourceId,
           List<RowItem> items) {
       super(context, resourceId, items);
       this.context = context;
   }


   private class ViewHolder {
       ImageView imageView;
       TextView txtTitle;
       TextView txtDesc;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder = null;
       RowItem rowItem = getItem(position);

       LayoutInflater mInflater = (LayoutInflater) context
               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
       if (convertView == null) {
           convertView = mInflater.inflate(R.layout.list_item, null);
           holder = new ViewHolder();
           holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
           holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
           holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
           convertView.setTag(holder);
       } else
           holder = (ViewHolder) convertView.getTag();

       holder.txtDesc.setText(rowItem.getDesc());
       holder.txtTitle.setText(rowItem.getTitle());
       holder.imageView.setImageResource(rowItem.getImageId());

       return convertView;
   }
}
4

2 に答える 2

2

次のようにクリックして登録します。

 lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
            // TODO Auto-generated method stub

             Intent intent = new Intent(getApplicationContext(), ImageGridActivity.class);

              startActivity(intent);
        }
    });
于 2013-10-24T06:11:30.857 に答える
1

コードを確認しましたが、いくつかの問題があります

1)要素を循環させ、各要素のアダプターを作成します

2) onCreate 内に onItemClick を実装しました

コードのリビジョンを貼り付けますが、RowItem を Object に変更し、CustomListViewAdapter を ListAdapter に変更したので、そこでオブジェクトを使用しますが、これはコードの構造である必要があります:)

package org.iimed.www;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class ImageTextListViewActivity extends Activity implements OnItemClickListener {

public static final String[] titles = new String[] { "Penicillins",
    "Cephalosporins", "Monobactams",      "Carbapenems","Macrolides","Tetracyclines",};

public static final String[] descriptions = new String[] {
    "It is an aggregate accessory fruit",
    "It is the largest herbaceous flowering plant", "Citrus Fruit",
    "Mixed Fruits","none","none" };

public static final Integer[] images = { R.drawable.antibiotic_buton,
    R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,   R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };

ListView listView;
List<Object> rowItems;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screj);

    rowItems = new ArrayList<Object>();
    for (int i = 0; i < titles.length; i++) {
        Object item = new Object();
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    ListAdapter adapter = new ListAdapter(this,R.layout.list_item, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    Intent intent = null ;
    switch (arg2) {
    case 0:
        intent  = new Intent(ImageTextListViewActivity.this, MainActivity.class);
        startActivity(intent);          
        break;
    case 1:
        intent = new Intent(ImageTextListViewActivity.this, Miipet.class);
        startActivity(intent);
        break;
    default:
        break;
    }
}
}
于 2013-10-24T06:20:08.310 に答える