在项目列表选择上设置textview

时间:2013-12-18 11:03:15

标签: android

我正在尝试使用对话框显示字符串列表....然后点击任意项目,我需要在活动中设置文本

What have i tried ?

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

5 个答案:

答案 0 :(得分:3)

简单你可以使用它:

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);

答案 1 :(得分:1)

试试这个...

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);
           // add following line
            TV.setText(NamesOfPeople.get(arg2));
            //TV.setText(arg1);
        }
    });
}

}

答案 2 :(得分:1)

试试这个..

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

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

并点击您的商品。

LV.setOnItemClickListener(new OnItemClickListener() {

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

            TV.setText(NamesOfPeople.get(arg2).toString());
            //or 
            TV.setText(((TextView) arg1).getText().toString().trim());
        }
    });

答案 3 :(得分:1)

在对话框中显示一组字符串然后对单个选项选项执行某些操作的最简单方法是:

  1. 首先在strings.xml文件

    中声明一个数组
    <string-array name="strings_options">
        <item>String one</item>
        <item>String two</item>
        <item>String three</item>
        <item>String four</item>
    </string-array>
    

    我们可以在Activity自己声明一个字符串数组,但是在strings.xml文件中定义和声明它会在将来需要提供本地化支持时为我们提供更多控制。

  2. 现在,我们将在下一步中创建一个AlertDialog,其中包含单个选项,即我们的字符串。

    AlertDialog.Builder = 
        new AlertDialog.Builder(CONTEXT)
           .setTitle("Options")
           .setSingleChoiceItems(R.string.string_options, null,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int position) {
                            switch(position){
                              case 1: break;
                              case 2: break;
                              .
                              .
                              .
                              default:
                              break;
                            }
            });
           .setNegativeButton("Cancel", null)
           .create().show();
    

答案 4 :(得分:0)

试试这个

final CharSequence[] items = {commentshareType, "Facebook", "Twitter", "Email" };

   AlertDialog.Builder builder = new AlertDialog.Builder(activity);                                   
      builder.setTitle("Share");                                    
      builder.setItems(items, new DialogInterface.OnClickListener() {                                       
       public void onClick(DialogInterface dialog,  int item) {

    if (items[item].equals("Delete")) {

   } else if (items[item].equals("Report Abuse")) {

  } else if (items[item].equals("Facebook")) {  

 } else if (items[item].equals("Twitter")) {

    } else if (items[item].equals("Email")) {   

        }                                   
       });

     AlertDialog alert = builder.create();                                  
     alert.show();