[英]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.