简体   繁体   English

如何使用TextView显示从列表视图中选择的项目的值

[英]How to display the value of an item selected from a list view using textview

Using simplesursoradapter I have raised a listview. 使用simplesursoradapter,我提出了一个listview。 I select an item from listview and keep it in a variable 'titlename' and able to show in Toast. 我从列表视图中选择一个项目,并将其保留在变量“ titlename”中,并能够在Toast中显示。 Now, I want this value in 'titlename' to be passed to a text view id 'textView3' so that it could be displayed on top of the screen in the same activity. 现在,我希望将此“ titlename”中的值传递给文本视图id“ textView3”,以便可以在同一活动中将其显示在屏幕顶部。 When I tried with the following code I get a NULL pointer exception at line textView.setText(v_titlename);. 当我尝试使用以下代码时,在textView.setText(v_titlename);行出现了NULL指针异常。

a)TextView ID 'textView' and 'textView2' are in custom_row.xml a)TextView ID'textView'和'textView2'在custom_row.xml中

b)TextView ID 'textView3' is in Activity_Out.xml b)TextView ID'textView3'在Activity_Out.xml中

public void onItemClick(AdapterView<?> parent, View view, int position, long thislist) {
    TextView selectedTitle = (TextView)view.findViewById(R.id.textView);
    TextView titlename= (TextView)view.findViewById(R.id.textView2);
    TextView textView = (TextView) findViewById(R.id.textView3);
    v_titlename=titlename.getText().toString();
    textView.setText(v_titlename);
    Toast.makeText(this, titlename.getText() , Toast.LENGTH_LONG).show();
    inputID=selectedTitle.getText().toString(); // the _id of this title is stored in inputID
}

You need to make "textView" a field on your class and call findViewById() in your activity's onCreate(). 您需要将“ textView”设置为类的一个字段,并在活动的onCreate()中调用findViewById()。 The way you have it setup now the only way for textView to not be null would be for it to be a part of your adapter view and you'd have to call it with view.findViewById(). 现在,将它设置为textView不为null的唯一方法是使其成为适配器视图的一部分,而您必须使用view.findViewById()进行调用。 Given that you said you wanted this to be at the top of your activity you just have it in the wrong place. 假设您说过要让它成为活动的重中之重,那么您把它放在错误的位置。 textView is part of the activity, not the list. textView是活动的一部分,而不是列表。

R.id.textView3 is a unique integer in a generated Class. R.id.textView3是生成的Class中的唯一整数。 So, it is "valid" even though it is not part of that View. 因此,即使它不是该视图的一部分,它也是“有效的”。 Trips lots of people up. 绊倒了很多人。

    public class SomeActivity extends Activity {

        private TextView textView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.Activity_Out);
            textView = (TextView) findViewById(R.id.textView3);
            // ...
        }
    // ...
    }

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

相关问题 如何在TextView上显示数组列表中的选定项目 - How do I display selected item from array list on textview 如何将选定的项目从列表视图转换为自动完成的textview - how to take on selected item from list view to autocomplete textview 在TextView中显示searchablespinner的选定项目 - Display the selected item of the searchablespinner in the TextView 如何从RECYCLERVIEW中的选定项目获取TEXT VIEW值 - How to get a TEXT VIEW value from selected item in RECYCLERVIEW 如何在列表视图中显示使用Filewriter保存的项目 - how do i display an item saved using filewriter in a list view 如何旋转列表使得从列表中选择的项始终位于列表视图中的中间位置 - How to rotate a list such that item selected from the list always gets the middle position in a list view 如何从Android的列表视图中获取选定的微调器值? - How to get selected spinner value from list view in android? 如何使用Java GAE在数据存储区的下拉列表中显示所选值? - How to display the selected value in dropdown from datastore using java GAE? 如何在列表视图中的选定列表项目上设置自定义默认铃声? - How to set custom default ringtone on the selected list item in list View? 从JComboBox中选择一个项目后,如何让JLabel从数组中显示值? - How do you let a JLabel display a value from an array when an item is selected from a JComboBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM