2015-12-22 63 views
0

我有一些僞問題,我需要從片段微調項目的位置到這個類:如何讓微調項目從片段位置到某個類?

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.FragmentManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Spinner; 
import com.example.nortti.politrange.R; 
import com.example.nortti.politrange.intefaces.ICatalog; 
import com.example.nortti.politrange.objects.Person; 
import com.example.nortti.politrange.objects.Site; 
import com.example.nortti.politrange.utils.WebApiAdapter; 
import com.example.nortti.politrange.views.GeneralFragment; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 

public class PersonCatalog implements ICatalog{ 
    private final String COMMAND_PREFIX = "/api/stats/1"; 
    private final WebApiAdapter apiAdapter = new WebApiAdapter(COMMAND_PREFIX); 
    private ArrayList<Person> catalogList = new ArrayList<Person>(); 
    private Site site; 

public PersonCatalog(Site site) { 
    this.site = site; 
} 


@Override 
public ArrayList<Person> getCatalogList() { 
    return catalogList; 
} 

public void populateData() { 
    JSONArray jsonObject = null; 

    try { 
     jsonObject = (JSONArray)(new JSONParser()).parse(apiAdapter.select(null)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    catalogList.clear(); 
    Iterator<JSONObject> iterator = jsonObject.iterator(); 

    while(iterator.hasNext()) { 
     JSONObject o = iterator.next(); 
     catalogList.add(new Person((String)o.get("personName"),(int)(long)o.get("rank"))); 
    } 

} 
} 

我打破了我的頭,我不知道該怎麼做。請幫忙!我應該使用一些意圖還是創建一些獲得者?

UPD:片段代碼

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.StrictMode; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Spinner; 

import com.example.nortti.politrange.R; 
import com.example.nortti.politrange.adapters.GenAdapter; 
import com.example.nortti.politrange.adapters.SiteAdapter; 
import com.example.nortti.politrange.intefaces.ICatalog; 
import com.example.nortti.politrange.intefaces.impls.PersonCatalog; 
import com.example.nortti.politrange.intefaces.impls.SitesCatalog; 
import com.example.nortti.politrange.objects.Site; 

public class GeneralFragment extends Fragment implements OnClickListener, OnItemSelectedListener { 

private Button genApply; 
private Spinner spinner; 
private ListView genList; 
private View header; 
private ICatalog siteCatalogImpl; 
private ICatalog personCatalogImpl; 
public int Num; 

public void setSpinnerSource(ICatalog siteCatalogImpl) { 
    this.siteCatalogImpl = siteCatalogImpl; 
    spinData(); 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) 
{ 
    View v = inflater.inflate(R.layout.general_fragment, container,false); 

    header = inflater.inflate(R.layout.gen_head, null); 

    spinner = (Spinner) v.findViewById(R.id.wSpin); 
    spinner.setOnItemSelectedListener(this); 
    genApply = (Button) v.findViewById(R.id.genApply); 
    genApply.setOnClickListener(this); 

    genList = (ListView) v.findViewById(R.id.genList); 
    genList.addHeaderView(header); 
    this.setSpinnerSource(new SitesCatalog()); 
    Intent i = new Intent(); 
    i.putExtra("spin", spinner.getSelectedItemPosition()+1); 
    return v; 
} 

private void spinData() { 
    siteCatalogImpl.populateData(); 
    spinner.setAdapter(new SiteAdapter(getActivity(), siteCatalogImpl.getCatalogList())); 
} 

private void listData(Site site) { 
    personCatalogImpl = new PersonCatalog(site); 
    personCatalogImpl.populateData(); 
    genList.setAdapter(new GenAdapter(getActivity(), personCatalogImpl.getCatalogList())); 
} 

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

} 

@Override 
public void onNothingSelected(AdapterView<?> parent) { 

} 


@Override 
public void onClick(View v) { 
    int siteIndex = spinner.getSelectedItemPosition(); 
    switch (v.getId()) { 
     case R.id.genApply: 
      listData((Site)siteCatalogImpl.getCatalogList().get(siteIndex)); 
      break; 
    } 
} 
} 

我在的listData方法調用PersonCatalog。

+0

你是如何從片段中調用這個類的? –

+0

請發佈片段代碼 –

回答

0

試試這個。

final ArrayList<String> providerlist= new ArrayList<String>(); 
Spinner spinner1 = (Spinner) findViewById(R.id.prospin); 
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist); 

adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinner1.setAdapter(adapter1); 
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

         // On selecting a spinner item 
     String item = providerlist.get(position); 

     // Showing selected spinner item 
     Toast.makeText(this, 
       "Selected Country : " + item, Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 

    } 
}); 
+0

我知道如何把一個變量的微調項目的位置,我現在不會如何讓它到另一個類(不是片段)。謝謝 –

相關問題