0

大家好。我是新手。我的目标是当我想单击 ListView 中的特定项目时,它应该打开一个我已编程的新活动。例如:当我点击“John”时,我希望它打开一个叫X的班级,Mary,应该打开一个班级Y,Charlie,Z班,等等。我尝试在代码末尾使用“onListItemClick”代码,但它不起作用。当我从 ListView 应用程序中单击 John 的名字时,什么也没有发生。请在我的代码错误的地方帮助我,我应该把代码放在哪里以及正确的代码是什么。

PS:我在 Manifest 文件中声明了所有活动

这是我正在使用的代码:

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 方法的唯一方法是您的 Activity 扩展 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

试试这个代码..

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt,
            long paramLong) {
        switch( position )
{
   case 0:  Intent newActivity0 = new Intent(YourActivity.this, second.class);     
            startActivity(newActivity0);
            break;
   case 1:  Intent newActivity1 = new Intent(YourActivity.this, third.class);     
            startActivity(newActivity1);
            break;
   case 2:  Intent newActivity2 = new Intent(YourActivity.this, fourth.class);     
            startActivity(newActivity2);
            break;
   case 3:  Intent newActivity3 = new Intent(YourActivity.this, fifth.class);     
            startActivity(newActivity3);
            break;
   case 4:  Intent newActivity4 = new Intent(YourActivity.this, sixth.class);     
            startActivity(newActivity4);
            break;
}              
    }
});

试着让我知道......希望这对你有帮助:-)

于 2013-10-17T06:59:11.500 回答
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 回答