2017-04-10 32 views
0

在我的列表視圖中單擊一個項目後,應顯示我的單一項目視圖。不幸的是,每次點擊兩項中的一項時,只會出現相同的內容。我如何解決這個問題,並顯示正確的內容? 首先我得到的解析數據,我的主要活動:我如何在單個項目視圖中獲得正確的內容

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 
ArrayList<productforloc> arrayList; 
ListView lv; 
private String TAG = MainActivity.class.getSimpleName(); 
private TextView addressField; //Add a new TextView to your activity_main to display the address 
private LocationManager locationManager; 
private String provider; 
int i = 1; 
private ProgressDialog pDialog; 
String name; 
String image; 
String street; 
String postalcode; 
String musicstyle; 
String musicsecond; 
String entry; 
String opening; 
String agegroup; 
String urlbtn; 
String Fsk; 
String city; 


// URL to get contacts JSON 
private static String url = "http://partypeople.bplaced.net/maptest.json"; 

ArrayList<HashMap<String, String>> contactList; 

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


    pDialog = new ProgressDialog(MainActivity.this); 
    pDialog.setMessage("Please wait..."); 
    pDialog.setCancelable(false); 
    pDialog.show(); 
    arrayList = new ArrayList<>(); 
    lv = (ListView) findViewById(R.id.lv); 
    lv.setOnItemClickListener((AdapterView.OnItemClickListener) this); 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      new ReadJSON().execute(url); 
     } 
    }); 

    final Button popbutton = (Button) findViewById(R.id.popbutton); 
    popbutton.setOnClickListener(new View.OnClickListener() { 
     @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) 
     @Override 
     public void onClick(View v) { 
      if (i == 1) { 
       if (popbutton.isPressed()) { 
        popbutton.setBackground(getResources().getDrawable(R.drawable.secondbg)); 
        arrayList.clear(); 
        url = "http://partypeople.bplaced.net/justpop.json"; 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          new ReadJSON().execute(url); 
         } 

        }); 
        i = i + 1; 
       } 
      } else { 
       if (popbutton.isPressed()) { 
        popbutton.setBackground(getResources().getDrawable(R.drawable.bg_popbutton)); 

        arrayList.clear(); 
        url = "http://partypeople.bplaced.net/maptest.json"; 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          new ReadJSON().execute(url); 
         } 

        }); 
        i = i - 1; 
       } 
      } 


     } 
    }); 



} 



class ReadJSON extends AsyncTask<String,Integer,String>{ 






    @Override 
    protected String doInBackground(String... params) { 
     return readURL(params[0]); 
    } 

    @Override 
    protected void onPostExecute(String content) { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     try{ 
      JSONObject jo = new JSONObject(content); 
      JSONArray ja = jo.getJSONArray("contacts"); 

      for(int i=0;i<ja.length();i++){ 
       JSONObject po = ja.getJSONObject(i); 
       arrayList.add(new productforloc(
          image= po.getString("imageurl"), 
          name = po.getString("name"), 
          street = po.getString("street"), 
          postalcode = po.getString("postalcode"), 
          musicstyle = po.getString("musicstyle"), 
          musicsecond = po.getString("musicsecond"), 
          entry = po.getString("entry"), 
          opening = po.getString("opening"), 
          agegroup = po.getString("agegroup"), 
          urlbtn = po.getString("urlbtn"), 
          Fsk = po.getString("Fsk"), 
          city = po.getString("city") 

       )); 

      } 



     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     CustomListAdapterforloc adapter = new CustomListAdapterforloc(getApplicationContext(),R.layout.model,arrayList); 
     lv.setAdapter(adapter); 

    } 
} 



private String readURL(String url){ 
     StringBuilder content = new StringBuilder(); 
     try{ 
      URL uri = new URL(url); 
      URLConnection urlConnection = uri.openConnection(); 
      BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
      String line; 
      while((line = bufferedReader.readLine()) !=null){ 
       content.append(line+"\n"); 


      } 
      bufferedReader.close(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return content.toString(); 
    } 

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

    Intent intent = new Intent(); 
    intent.setClass(this,DetailActivity.class); 

    intent.putExtra("name",name); 
    intent.putExtra("imageurl",image); 
    intent.putExtra("street",street); 
    intent.putExtra("postalcode",postalcode); 
    intent.putExtra("musicstyle",musicstyle); 
    intent.putExtra("musicsecond",musicsecond); 
    intent.putExtra("entry",entry); 
    intent.putExtra("opening",opening); 
    intent.putExtra("agegroup",agegroup); 
    intent.putExtra("urlbtn",urlbtn); 
    intent.putExtra("Fsk",Fsk); 
    intent.putExtra("city",city); 
    startActivity(intent); 
    Toast.makeText(getApplicationContext(),street,Toast.LENGTH_LONG).show(); 
} 

/** 
* Async task class to get json by making HTTP call 

    } 



*/ 

}

然後你可以在底部的內容將被髮送到detailactivity看,但我總是得到的第二項內容在我的JSON中,即使我點擊第一項。

回答

0

更改您的onItemClick方法從列表中獲取正確的對象。

Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    productforloc pForloc = arrayList.get(positon); 

    Intent intent = new Intent(); 
    intent.setClass(this,DetailActivity.class); 

    intent.putExtra("name",pForloc.getName()); 
    intent.putExtra("imageurl",pForloc.getImage()); 
    intent.putExtra("street",pForloc.getStreet()); 
    intent.putExtra("postalcode",pForloc.getPostalcode()); 
    intent.putExtra("urlbtn",pForloc.getUrlbtn()); 
    intent.putExtra("Fsk",pForloc.getFsk()); 
    intent.putExtra("city",pForloc.getCity()); 
    startActivity(intent); 
    Toast.makeText(getApplicationContext(),street,Toast.LENGTH_LONG).show(); 
} 
+0

其工作,感謝兄弟:) –

+0

你能接受答案嗎? –

+0

我有一個新的問題,這是接近這個,也許你可以檢查它:) –

相關問題