gpt4 book ai didi

android - 在项目列表选择上设置 TextView

转载 作者:行者123 更新时间:2023-11-29 14:50:15 26 4
gpt4 key购买 nike

我正在尝试使用对话框来显示字符串列表....然后在单击任何项​​目时,我需要在 Activity 中设置文本

我尝试了什么?

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void popUp(View v){

//declare the collection to add people into them
ArrayList<String> NamesOfPeople=new ArrayList<String>();
NamesOfPeople.add("USA");//add person
NamesOfPeople.add("CANADA");//add person
NamesOfPeople.add("RUSSIA");//add person

// set the dialog to be displayed on the present activity ... so context is MainActivity.this
final Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.list_of_names);//set the liststructure
dialog.setTitle("List Of People");// Set the title of the dialog

//find the listview using id ...remember listview is on dialog now so
ListView LV=(ListView) dialog.findViewById(R.id.listView);

// Now i need to put the data from the collection to the listview
// We shall use an arrayadapter for this
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, NamesOfPeople);
//we always set the adapter using the listview
LV.setAdapter(adapter);

//Dont forget to show the dialog
dialog.show();

LV.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

//dialog.dismiss();// used to cancel the dialog on click of it
TextView TV=(TextView) dialog.findViewById(R.id.textView);

//TV.setText(arg1);
}
});
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="154dp"
android:text="SelectString"
android:onClick="popUp"/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="72dp"
android:text="NAME"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

list_of_names.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

我卡在哪里::

我能够显示弹出对话框,但是如何在从列表中选择一个字符串时设置文本


{编辑}

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void popUp(View v){

//declare the collection to add people into them
final ArrayList<String> NamesOfPeople=new ArrayList<String>();
NamesOfPeople.add("USA");//add person
NamesOfPeople.add("CANADA");//add person
NamesOfPeople.add("RUSSIA");//add person

// set the dialog to be displayed on the present activity ... so context is MainActivity.this
final Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.list_of_names);//set the liststructure
dialog.setTitle("List Of People");// Set the title of the dialog

//find the listview using id ...remember listview is on dialog now so
ListView LV=(ListView) dialog.findViewById(R.id.listView);

// Now i need to put the data from the collection to the listview
// We shall use an arrayadapter for this
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, NamesOfPeople);
//we always set the adapter using the listview
LV.setAdapter(adapter);

//Dont forget to show the dialog
dialog.show();

LV.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

//dialog.dismiss();// used to cancel the dialog on click of it
TextView TV=(TextView) dialog.findViewById(R.id.textView);

TV.setText(NamesOfPeople.get(arg2).toString());
}
});
}




}

日志

12-18 16:41:53.757: E/AndroidRuntime(902): Uncaught handler: thread main exiting due to uncaught exception
12-18 16:41:53.816: E/AndroidRuntime(902): java.lang.NullPointerException
12-18 16:41:53.816: E/AndroidRuntime(902): at com.example.popupofstrings.MainActivity$1.onItemClick(MainActivity.java:59)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.widget.ListView.performItemClick(ListView.java:3285)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.os.Handler.handleCallback(Handler.java:587)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.os.Looper.loop(Looper.java:123)
12-18 16:41:53.816: E/AndroidRuntime(902): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-18 16:41:53.816: E/AndroidRuntime(902): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 16:41:53.816: E/AndroidRuntime(902): at java.lang.reflect.Method.invoke(Method.java:521)
12-18 16:41:53.816: E/AndroidRuntime(902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-18 16:41:53.816: E/AndroidRuntime(902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-18 16:41:53.816: E/AndroidRuntime(902): at dalvik.system.NativeStart.main(Native Method)
12-18 16:41:53.836: I/dalvikvm(902): threadid=7: reacting to signal 3
12-18 16:41:53.836: E/dalvikvm(902): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

最佳答案

简单你可以使用这个:

LV.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

//dialog.dismiss();// used to cancel the dialog on click of it
TextView TV=(TextView) dialog.findViewById(R.id.textView);

TV.setText(NamesOfPeople.get(arg2).toString());
}
});

删除这个

 TextView TV=(TextView) dialog.findViewById(R.id.textView);

来自 OnItemClickListener 方法并放在 onCreate() 方法上

 TextView TV=(TextView) findViewById(R.id.textView);

关于android - 在项目列表选择上设置 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20656360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com