0

別のアクティビティに接続する際に、リストにClickListener (OnClickListenerまたは)を使用することに本当に問題があります。OnItemClickListenerリストにクリック イベントのコードを挿入しようとしましたが、うまくいきません。それはいつも私にエラーを与えます。以下は私のアルファベット活動のコードです

public class Alphabet_Activity extends Activity

{

private List<Alpha> myAlpha=new ArrayList<Alpha>();

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

    populateAlphaList();
    populateListView();
    registerClickableList();
}
//------------------------------------------------------------>
//----->for contents of my list in ListView<------------------
//------------------------------------------------------------>
    private void populateAlphaList() 
{
    myAlpha.add(new Alpha("A", R.drawable.a));
    myAlpha.add(new Alpha("B", R.drawable.b));
    myAlpha.add(new Alpha("C", R.drawable.c));
    myAlpha.add(new Alpha("D", R.drawable.d));
    myAlpha.add(new Alpha("E", R.drawable.e));
    myAlpha.add(new Alpha("F", R.drawable.f));
    myAlpha.add(new Alpha("G", R.drawable.g));
    myAlpha.add(new Alpha("H", R.drawable.h));
    myAlpha.add(new Alpha("I", R.drawable.i));
    myAlpha.add(new Alpha("J", R.drawable.j));
    myAlpha.add(new Alpha("K", R.drawable.k));
    myAlpha.add(new Alpha("L", R.drawable.l));
    myAlpha.add(new Alpha("M", R.drawable.m));
    myAlpha.add(new Alpha("N", R.drawable.n));
    myAlpha.add(new Alpha("O", R.drawable.o));
    myAlpha.add(new Alpha("P", R.drawable.p));
    myAlpha.add(new Alpha("Q", R.drawable.q));
    myAlpha.add(new Alpha("R", R.drawable.r));
    myAlpha.add(new Alpha("S", R.drawable.s));
    myAlpha.add(new Alpha("T", R.drawable.t));
    myAlpha.add(new Alpha("U", R.drawable.u));
    myAlpha.add(new Alpha("V", R.drawable.v));
    myAlpha.add(new Alpha("W", R.drawable.w));
    myAlpha.add(new Alpha("X", R.drawable.x));
    myAlpha.add(new Alpha("Y", R.drawable.y));
    myAlpha.add(new Alpha("Z", R.drawable.z));
}
//-------------------------------------------------------------------------------------->
//----->for calling the values in Alpha Activity to Alphabet Activity<------------------
//-------------------------------------------------------------------------------------->
private void populateListView()
{
    ArrayAdapter<Alpha> adapter=new MyListAdapter();
    ListView list=(ListView) findViewById(R.id.alphaListView);
    list.setAdapter(adapter);           
}   
//--------------------------------------------------------------------------->
//----->for getting message after clicking one of the list<------------------
//--------------------------------------------------------------------------->
private void registerClickableList() {
    ListView list = (ListView) findViewById(R.id.alphaListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) {

            Alpha clickedAlpha = myAlpha.get(position);
            String message = "You clicked " + position + clickedAlpha.getName();
            Toast.makeText(Alphabet_Activity.this, message, Toast.LENGTH_LONG).show();
        }
    });
}
//------------------------------------------------------------------------------>
//------>calling of values in Alpha Activity and contents in myAlpha<-----------
//------------------------------------------------------------------------------>
private class MyListAdapter extends ArrayAdapter<Alpha>
{
    public MyListAdapter()
    {
        super(Alphabet_Activity.this, R.layout.alpha, myAlpha);

    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent)
    {
        View itemView=convertView;
        if(itemView==null)
        {
            itemView=getLayoutInflater().inflate(R.layout.alpha, parent,false);
        }

        //---------------------
        Alpha currentAlpha=myAlpha.get(position);

        ImageView imageView=(ImageView)itemView. findViewById(R.id.icon_Alpha);
        imageView.setImageResource(currentAlpha.geticonAlpha());


        TextView name=(TextView) itemView.findViewById(R.id.alpha_Name);
        name.setText(currentAlpha.getName());

        return itemView;
        //return super.getView(position, convertView, parent);


    }

}
 }

これが私のアルファアクティビティのコードです

public class Alpha {

private String Name;
private int iconAlpha;

public Alpha(String make, int iconAlpha) {
    super();
    this.Name = Name;
    this.iconAlpha = iconAlpha;
}
public String getName() {
    return Name;
}
public int geticonAlpha() {
    return iconAlpha;
}
}
4

2 に答える 2

1

あなたのコードは正常に動作しています。ちょっとしたミスがあるだけです。

Alpha クラスでは、文字列の名前を Name に変更します。

さまざまなアクティビティを呼び出すには、onItemClick に以下のコードを追加します

switch (position) {
            case 0: 
                Intent a = new Intent(getApplicationContext(), AActivity.class);
                startActivity(a);
                break;

            case 1: 
                Intent b = new Intent(getApplicationContext(), BActivity.class);
                startActivity(b);
                break;
}

同様に、要件に従ってケースを追加します。

また

すべての新しいアクティビティの文字列の配列を順番に作成できます。

その後、arr [position]でアクティビティ名を取得して呼び出すことができます

Intent b = new Intent(getApplicationContext(), arr[pos]); startActivity(b);

この場合、スイッチケースは必要ありません

于 2013-08-22T11:17:51.830 に答える
0

削除する

ListView list=(ListView) findViewById(R.id.alphaListView); 

populateListView() および registerClickableList() から.................................

onCreate メソッドに追加してから、これらの両方の関数で同じオブジェクトを使用します。

ここでは、2 つの異なるオブジェクトを作成しています。単一のオブジェクトが必要です。

于 2013-08-22T05:46:28.350 に答える