简体   繁体   English

单击时onItemClick不起作用

[英]onItemClick not working when clicked

I am missing something very crucial but I can't quite see what. 我错过了一些非常关键的东西,但我不太明白。 Can someone please assist. 有人可以帮忙吗? It's probably something really silly that I have missed but I cannot initiate my onItemClick. 我可能错过了很愚蠢的东西,但是我无法启动onItemClick。

onCreate.... onCreate...。

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    setContentView(R.layout.act_ipcontrol);

    mainListView = (ListView) findViewById( R.id.mainListView );
    String[] options = new String[] { "All in to 1", "Spare"};  

    ArrayList<String> optionsList = new ArrayList<String>();
    optionsList.addAll( Arrays.asList(options) );
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, optionsList);
    mainListView.setAdapter( listAdapter ); 

    try {
        Toast.makeText(IPControl.this, "Please wait...Connecting...", Toast.LENGTH_SHORT).show();
        new AsyncAction().execute();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private class AsyncAction extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... args) { 
        try {
            InetAddress serverAddr = InetAddress.getByName(actu_ip);
            socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osw);
            out = new PrintWriter(bw, true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            readBuffer();
            out.println("[verbose,off\r\n");
            while (! in .ready());
            String msg = "";
            while ( in .ready()) {
                msg = msg + (char) in .read();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {

        Toast.makeText(IPControl.this, "Connected", Toast.LENGTH_SHORT).show();

        //results the data returned from doInbackground

        IPControl.this.data = result;

    }
}

private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
    else return msg;
}
}

What I want to initiate... 我要发起的...

        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
            long arg3) {

        try {
            new AsyncAction1().execute();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private class AsyncAction1 extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... args) { 
            try {
                out.println("[c,l#,i1,o*\r\n");
                //System.out.print("root\r\n");
                while(! in .ready());


            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;//returns what you want to pass to the onPostExecute()
        }

        protected void onPostExecute(String result) {

            //results the data returned from doInbackground
            Toast.makeText(IPControl.this, "Command Sent", Toast.LENGTH_SHORT).show();

            IPControl.this.data = result;

        }
    }

I haven't seen setOnItemClickListener method for listview in your code. 我还没有在您的代码中看到setOnItemClickListener方法用于listview。 Have you implemented it? 你实现了吗? Try following 尝试跟随

mainListView.setAdapter( listAdapter ); 
mainListView.setOnItemClickListener(new OnItemClickListener(){
   public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

      try {
        new AsyncAction1().execute();
      }catch(Exception e) {
        e.printStackTrace();
   }
});

Thanks for all your help, I fixed my problem. 感谢您的所有帮助,我已解决问题。

I just wasn't doing things in the correct order. 我只是没有按正确的顺序做事。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    setContentView(R.layout.act_ipcontrol);

    mainListView = (ListView) findViewById( R.id.mainListView );
    final String[] options = new String[] { "All in to 1", "Spare"};  

    ArrayList<String> optionsList = new ArrayList<String>();
    optionsList.addAll( Arrays.asList(options) );
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, optionsList);
    mainListView.setAdapter( listAdapter ); 
    mainListView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            try {
                    if(pos == 0) {
                        AsyncAction1 a = new AsyncAction1();
                        a.setCmd("[c,l#,i1,o*\r\n");
                        a.execute();
                    }
                } catch(Exception e) {
                    e.printStackTrace();
                }
        }
    });

Then.... 然后....

private class AsyncAction1 extends AsyncTask<String, Void, String> {
    String cmd;

    public void setCmd(String c) {
        cmd = c;
    }

    protected String doInBackground(String... args) {
        try {
            out.println(cmd);
            //System.out.print("root\r\n");
            while(! in .ready());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {

        //results the data returned from doInbackground
        Toast.makeText(IPControl.this, "Command Sent", Toast.LENGTH_SHORT).show();

        IPControl.this.data = result;
    }
}
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM