2015-09-19 4 views
0

Я пытаюсь передать некоторые детали элемента списка в новое действие. К сожалению, я всегда получаю информацию о первом элементе списка в новом действии. Это связано с именем текстового представления (я полагаю), которое называется с тем же именем. Как я могу получить те, которые связаны с кликом?Детали элемента списка в виде списка

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

ответ

0

в методе onItemClick(), вы должны сделать view.findViewById(...) вместо parent.findViewById(...)

+0

Большое спасибо. Это сработало! – Celair

Смежные вопросы