如何从列表中选择的两个autocompletetextview中获取autocompletetextview的id?

时间:2014-09-20 08:27:04

标签: android xml

这是mysearchActivity.java

    package com.example.mumbailocal;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class SearchTrainActivity extends ActionBarActivity implements OnItemClickListener {
    private AutoCompleteTextView actv1;
    private AutoCompleteTextView actv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_train);

        actv1=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        actv2=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
        String[] stations=getResources().getStringArray(R.array.stations_list);
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stations);


        actv1.setAdapter(adapter1);
        actv2.setAdapter(adapter1);

        actv1.setOnItemClickListener(this);
        actv2.setOnItemClickListener(this);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_train, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View v, int pos, long rowindex) {
         String selection = (String)parent.getItemAtPosition(pos);

         Log.i("id is "+v.getId(), selection, null);
         Toast.makeText(getApplicationContext(),"parent "+parent.getId()+"view "+v.getId()+" row "+rowindex, Toast.LENGTH_LONG).show();
        // TODO Auto-generated method stub


    }
}

我首先有两个AutocompleteTextviews是From&amp;另一个是To。 现在我必须通过在文本.ie源和目标中输入值来验证这两个字段! 但是我无法获得当前选择textview的id .ie源或目的地!! 怎么实现这个?!!

1 个答案:

答案 0 :(得分:0)

onClick建议,&#34;长行指数&#34;是所选项目的ID。