0

みなさんこんにちは。私は初心者です。私の目標は、ListView から特定の項目をクリックしたいときです。プログラムした新しいアクティビティを開く必要があります。たとえば、「ジョン」をクリックすると、X、メアリー、クラス Y、チャーリー、クラス Z などのクラスが開きます。コードの最後に「onListItemClick」コードを使用しようとしましたが、機能しません。ListView アプリから John の名前をクリックしても何も起こりません。私のコードが間違っている場所と、コードをどこに置くべきか、正しいコードは何かを教えてください。

PS: マニフェスト ファイルですべてのアクティビティを宣言しました

これは私が使用している私のコードです:

public class Searchsort extends Activity {

    private ListView lv1;
    private EditText ed;
    private String lv_arr[]={"John","Mary","Carl","Rose","Charlie","Allan", "João"};
    private ArrayList<String> arr_sort= new ArrayList<String>();
    int textlength=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        lv1 = (ListView)findViewById(R.id.ListView01);
        ed = (EditText)findViewById(R.id.EditText01);

        // By using setAdpater method in listview we an add string array in list.

        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,   lv_arr));
        ed.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textlength=ed.getText().length();
                arr_sort.clear();
                for(int i=0;i<lv_arr.length;i++) {
                    if(textlength<=lv_arr[i].length()) {
                        if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0,  textlength))) {
                            arr_sort.add(lv_arr[i]);
                        }
                    }
                }

                lv1.setAdapter(new ArrayAdapter<String>  (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));

            }
        });
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        if ("John".equals(lv_arr[position])) { 
            Intent myIntent = new Intent(Searchsort.this, X.class);
            startActivity(myIntent);

        if ("Mary".equals(lv_arr[position])) { 
            Intent myIntent = new Intent(Searchsort.this, Y.class);
            startActivity(myIntent);

        if ("Charlie".equals(lv_arr[position])) { 
            Intent myIntent = new Intent(Searchsort.this, W.class);
            startActivity(myIntent);


        }
    }

}
4

3 に答える 3

1

onListItemClickメソッドをオーバーライドする唯一の方法は、アクティビティがListActivityを拡張する場合です。そうではないため、onListItemClickメソッドのコードを、アイテムクリックリスナーの設定内の適切なメソッドに移動する必要があります。

lv1.setOnItemClickListener(new onItemClickListener(){

    @Override
    protected void onListItemClick(AdapterView<?> arg0, View view, int position, long id){
        // Your code
    }
});

参考までに、これはStackで何度か尋ねられる同様の質問です。

于 2012-07-25T17:31:19.820 に答える
0
listView1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
        Toast.makeText(cxt, "You select Item " + (position + 1), Toast.LENGTH_SHORT).show();    
    }
});
于 2012-07-25T17:32:15.017 に答える