簡體   English   中英

列表視圖中列表項的詳細信息

[英]Details of a list item in a list view

我試圖將列表項的一些詳細信息傳遞給新活動。 不幸的是,我總是得到新活動列表中第一項的詳細信息。 這是由於以相同名稱命名的textview的名稱(我想)。 如何獲得與點擊的物品相關的物品?

public class GetFloor extends ListActivity {

  private Context context;
  private static String url = "http://indoorlocation.altervista.org/JSON/floor.php";

  private static final String FLOORID = "FloorID";
  private static final String BUILDINGID = "BuildingID";
  private static final String FLOORNAME = "FloorName";
  private static final String DESCRIPTION = "Description";

 ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

 ListView lv ;

 TextView txFloorid;
 TextView txBuildingid;
 TextView txFloorname;
 TextView txDescription;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.getfloor);
    new ProgressTask(GetFloor.this).execute();
}

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;
    private ListActivity activity;

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }
    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
    }
        ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.floorlist, new String[] { FLOORID, BUILDINGID, FLOORNAME, DESCRIPTION}, new int[] { R.id.FloorID, R.id.BuildingID, R.id.FloorName, R.id.Description});
        setListAdapter(adapter);

        // select single ListView item
        lv = getListView();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                txFloorid = (TextView) parent.findViewById(R.id.FloorID);
                txBuildingid = (TextView) parent.findViewById(R.id.BuildingID);

                txFloorname = (TextView) parent.findViewById(R.id.FloorName);
                txDescription = (TextView) parent.findViewById(R.id.Description);

                Intent intent = new Intent(GetFloor.this, FloorDetails.class);

                intent.putExtra("floorid", txFloorid.getText().toString());
                intent.putExtra("buildingid", txBuildingid.getText().toString());
                intent.putExtra("floorname", txFloorname.getText().toString());
                intent.putExtra("description", txDescription.getText().toString());

                startActivity(intent);
            }

        });
    }

    protected Boolean doInBackground(final String... args) {
        JSONParser jParser = new JSONParser();

    // get JSON data from URL
    JSONArray json = jParser.getJSONFromUrl(url);

        for (int i = 0; i < json.length(); i++) {
            try {
            JSONObject c = json.getJSONObject(i);
            String floorid = c.getString(FLOORID);
                String buildingid = c.getString(BUILDINGID);
            String floorname = c.getString(FLOORNAME);
            String description = c.getString(DESCRIPTION);

            HashMap<String, String> map = new HashMap<String, String>();
            // Add child node to HashMap key & value
            map.put(FLOORID, floorid);
                map.put(BUILDINGID, buildingid);
            map.put(FLOORNAME, floorname);
            map.put(DESCRIPTION, description);
                jsonlist.add(map);
        }
        catch (JSONException e) {
            e.printStackTrace();
        }

    }
    return null;
}
}
}

在我創建的類下面,該類用於獲取第一個活動傳遞的額外數據。

 public class FloorDetails extends Activity {


private static final String FLOORID = "FloorID";
private static final String BUILDINGID = "BuildingID";
private static final String FLOORNAME = "FloorName";
private static final String DESCRIPTION = "Description";

TextView floorid;
TextView buildingid;
TextView floorname;
TextView description;



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


    floorid = (TextView) findViewById(R.id.FloorID);
    buildingid = (TextView) findViewById(R.id.BuildingID);

    floorname = (TextView) findViewById(R.id.FloorName);
   description = (TextView)findViewById(R.id.Description);


    // get the intent from which this activity is called.
    Intent intent = getIntent();

    // fetch value from key-value pair and make it visible on TextView.

    String FloorID = intent.getStringExtra("floorid");
    String BuildingID = intent.getStringExtra("buildingid");
    String FloorName = intent.getStringExtra("floorname");
    String Description = intent.getStringExtra("description");


    floorid.setText(FLOORID + ":" + FloorID);
    buildingid.setText(BUILDINGID +  ":"  + BuildingID);
     floorname.setText(FLOORNAME + ":" + FloorName);
    description.setText(DESCRIPTION + ":" + Description) ;

}
}

在下面與該樓層列表相關的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" >
<TextView android:id="@+id/FloorID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FloorID" />
<TextView android:id="@+id/BuildingID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BuildingID" />
<TextView android:id="@+id/FloorName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FloorName" />
<TextView android:id="@+id/Description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Description"
    android:visibility="gone"/>

任何幫助將不勝感激,對於我的代碼格式錯誤,我深表歉意。

預先感謝您,Celair

onItemClick()方法中,您應該執行view.findViewById(...)而不是parent.findViewById(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM