9

我试图在这里从不同的类似问题中得到答案,但没有得到任何答案。实际上,我正在尝试从任何列表视图项的多个文本视图中获取值。这是代码
ListView listView = getListView();

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {               
        String contactId = ((TextView) findViewById(R.id.cid)).getText().toString();
        String contactName= ((TextView) findViewById(R.id.contactName)).getText().toString();
        String contactNo= ((TextView) findViewById(R.id.contactNo)).getText().toString();
        Intent i = new Intent(getApplicationContext(), editContactActivity.class);
        i.putExtra(TAG_ID, contactId );
        i.putExtra(TAG_NAME, contactName);
        i.putExtra(TAG_CONTACT_NO, contactNo);
        startActivityForResult(i, 100);     
    }   
}); 

我得到什么结果,我在 listview 中获得第一项(位置)的 textviews 值,或者我正在单击任何项​​目,但我在 listview 中获得第一项(位置)的 textviews 值,这是我如何获取值的代码在其他活动上

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_contact);

    btnSave = (Button) findViewById(R.id.btnSaveContact);
    btnDelete = (Button) findViewById(R.id.btnDeleteContact);

    Intent i = getIntent();
    cId = i.getStringExtra(TAG_CONTACT_ID);
    cNameTxt = i.getStringExtra(TAG_CONTACT_NAME);
    cNumberTxt = i.getStringExtra(TAG_CONTACT_NUMBER);

    //Log.d("Value of contact name", cNameTxt);
    //Log.d("Value of contact name", cNumberTxt);


    TextView contactIdTxtView = (TextView) findViewById(R.id.cid);
    cName = (EditText) findViewById(R.id.editCName);
    cNumber = (EditText) findViewById(R.id.editCNumber);


    cName.setText(cId);
    cNumber.setText(cNumberTxt);

}

如果有人能提供详细帮助,我将不胜感激。感谢 Akmal Rasool

4

3 回答 3

39

我在列表视图中获取第一项(位置)的文本视图值

每次使用时都告诉它只findViewById()查看这一行:

(TextView) view.findViewById(R.id.cid)
//Add this ^^^^^  

全部一起:

@Override
public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {
    String contactId = ((TextView) view.findViewById(R.id.cid)).getText().toString();
    //             Again use view  ^^^^^
    // Repeat this for contactName and contactNo
于 2013-02-12T18:25:39.813 回答
0

要获取 textView 值,试试这个。

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView text = (TextView)view.findViewById(R.id.correoEncuestador);
            String message= text.getText().toString();
            Toast.makeText(getApplication(), message,  Toast.LENGTH_LONG).show();
        }
    }
于 2016-04-07T22:03:06.963 回答
-1
View parentView = (View) view.getParent();
    textview1 = ((TextView) parentView
            .findViewById(R.id.textview1)).getText().toString();

    textview2 = ((TextView) parentView
            .findViewById(R.id.textview2)).getText().toString();

    textview3 = ((TextView) parentView
            .findViewById(R.id.textview3)).getText().toString();
于 2016-07-27T05:23:24.703 回答