获取所选列表视图的值

时间:2014-09-05 02:02:35

标签: java android listview android-listview arraylist

我有一个屏幕,显示3 listview列中的用户信息,我从3个arraylist获取每个listview的数据,每个arraylist包含我使用DOM解析器解析的xml数据。 对于第一个列表视图,我希望用户能够单击它,获取listview项的值(它是一个字符串),并根据所选单个项的值导航到另一个屏幕。 例如。 arraylist将包含(A,B,C,D,...),因此如果用户点击B>列表视图将具有(A,B,C,D)。用户应该导航到屏幕B(我将有一个使用该列表项的值的查询)

ViewMyTickets.java

    public class ViewMyTickets extends ListActivity  {


        public static String buttonText;


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


        string theirID = xmlresult// this is the xml result i get from web services


try {
    DocumentBuilderFactory dbf =
        DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(theirID));

    Document doc = db.parse(is);

    //All the UDSObjects
    NodeList nodesUDSObjectList = doc.getElementsByTagName("UDSObject");
    // iterate the UDSObjects
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    List<String> valSetOne = new ArrayList<String>();
    List<String> valSetTwo = new ArrayList<String>();
    List<String> valSetThree = new ArrayList<String>();
    for (int i = 0; i < nodesUDSObjectList.getLength(); i++) {
       Element elementUDSObject = (Element) nodesUDSObjectList.item(i);
       //You have a UDSObjects now
       NodeList nodesAttributeList = elementUDSObject.getElementsByTagName("Attribute");
       //You have a list of the attributes for UDSObject(i)
       // iterate the Attributes of the elementUDSObject(i)
        for (int iA = 0; iA < nodesAttributeList.getLength(); iA++) {
            Element elementAttribute = (Element) nodesAttributeList.item(iA);
            //You have attribute(iA)
            NodeList AttrNameElementList = (NodeList) elementAttribute.getElementsByTagName("AttrName");
            String nameValue = getCharacterDataFromElement((Element)(AttrNameElementList.item(0)));

            System.out.println("name"+nameValue);
            NodeList AttrValueElementList = (NodeList) elementAttribute.getElementsByTagName("AttrValue");
            String valueValue = getCharacterDataFromElement((Element)(AttrValueElementList.item(0)));
            System.out.println("value"+valueValue);
            if(nameValue.equals("ref_num")){
                valSetOne.add(valueValue);
            }
            if(nameValue.equals("summary")){
                valSetTwo.add(valueValue);
            }
            if(nameValue.equals("customer.combo_name2")){
                valSetThree.add(valueValue);
            }


        }
    }
    map.put("ref_num", valSetOne);
    map.put("summary",valSetTwo);
    map.put("customer",valSetThree);




 ListView myListView = (ListView) findViewById(R.id.listviewrequest);
 myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, valSetOne));

 myListView.setOnItemClickListener(new OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {
         // getting values from selected ListItem
         String name = ((Button) view.findViewById(R.id.requestnumberbutton)).getText().toString();

         // Starting new intent
         Intent in = new Intent(getApplicationContext(), ViewRequest.class);
         in.putExtra("name",name);
         startActivity(in);

     }

 });

 ListView myListView2 = (ListView) findViewById(R.id.listviewrequest2);
 myListView2.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_request2, valSetTwo));


ListView myListView3 = (ListView) findViewById(R.id.listviewrequest3);
myListView3.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_request3, valSetThree));









}

所以当我运行应用程序时,单击

E/AndroidRuntime(24691): FATAL EXCEPTION: main
E/AndroidRuntime(24691): Process: com.datacom.studentproject.caservicedesk, PID: 24691
E/AndroidRuntime(24691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.datacom.studentproject.caservicedesk/com.datacom.studentproject.caservicedesk.ViewMyTickets}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
E/AndroidRuntime(24691):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
E/AndroidRuntime(24691):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
E/AndroidRuntime(24691):    at android.app.ActivityThread.access$800(ActivityThread.java:164)
E/AndroidRuntime(24691):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)

UPDATE 我已经将ListActivity更改为Activity,我现在可以运行该页面但是当我点击列表项时它会崩溃并且我在我的logcat中收到此错误

E/AndroidRuntime(2714): FATAL EXCEPTION: main
E/AndroidRuntime(2714): Process: com.datacom.studentproject.caservicedesk, PID: 2714
E/AndroidRuntime(2714): java.lang.NullPointerException
E/AndroidRuntime(2714):     at com.studentproject.caservicedesk.ViewMyTickets$1.onItemClick(ViewMyTickets.java:179)
E/AndroidRuntime(2714):     at android.widget.AdapterView.performItemClick(AdapterView.java:308)
E/AndroidRuntime(2714):     at android.widget.AbsListView.performItemClick(AbsListView.java:1478)

第179行是

 String name = ((Button) view.findViewById(R.id.requestnumberbutton)).getText().toString();

这是我的viewMyTickets.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >




      <ListView

        android:id="@+id/listviewrequest"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

    </ListView>



      </RelativeLayout>





        <RelativeLayout

            android:layout_below="@+id/lower_linear_layout"

            android:layout_width="700dp"

            android:layout_height="fill_parent"

            android:layout_alignParentLeft="true">



                <ListView

        android:id="@+id/listviewrequest2"

        android:layout_width="700dp"

        android:layout_height="wrap_content" >            

    </ListView>           





           </RelativeLayout>  



          <RelativeLayout

            android:layout_below="@+id/lower_linear_layout"

            android:layout_width="350dp"

            android:layout_height="fill_parent"

            android:layout_alignParentLeft="true">





                <ListView

        android:id="@+id/listviewrequest3"

        android:layout_width="350dp"

        android:layout_height="wrap_content" >            

    </ListView>           

</LinearLayout>        

这是第一个listview中的内容 listviewrequest.xml

<Button

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/requestnumberbutton"

    android:layout_width="180dp"

    android:layout_height="50dp"

    android:text="@+id/requestnumberbutton"

    android:textStyle="bold" 

    android:textColor="#0077FF"

    android:textSize="25sp"
    android:onClick="onClick"/>

请帮助我,我真的需要帮助

3 个答案:

答案 0 :(得分:0)

从logcat中可以看出,因为你已经扩展了listActivity,你的listview的id应该是:

android:id="@android:id/list"//correct

我猜你的身份证是这样的:

android:id="@+id/list"//wrong

答案 1 :(得分:0)

当您的活动扩展ListActivity时,以这种方式使用listview

在Xml listview id

android:id="@android:id/list"

在Java中获取listview引用

ListView myListView = (ListView) findViewById(android.R.id.list);

OR

ListView myListView = getListView();

答案 2 :(得分:0)

见这一行:

 ListView myListView = (ListView) findViewById(R.id.listviewrequest);

如果您的ListView ID与android:id="@android:id/list"类似,那么您不必使用ListView个对象。您可以使用setListAdapter(adapter)

直接设置adatper

OR

您必须将extends ListActivity更改为extends Activity

如果有任何问题,请随时提出。