我仍然对获得微调项目文本感到困惑。
所以,在 Activity.class 中,我希望从Fragment.class获取Spinner Item Text ,但是idk如何从片段中获取Spinner Item Text,
任何人都可以提供帮助吗?
Fragment.class
final Spinner spinner = (Spinner) rootView.findViewById(R.id.choices);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
if (SpinerValue3.equals("Item 1")) {
Toast.makeText(getActivity().getBaseContext(),
"Try Choose : " + SpinerValue3,
Toast.LENGTH_SHORT).show();=
} else if (SpinerValue3.equals("Item 2")) {
Toast.makeText(getActivity().getBaseContext(),
"Try Choose : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}else if (SpinerValue3.equals("Item 3")) {
Toast.makeText(getActivity().getBaseContext(),
"Try Choose : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}else if (SpinerValue3.equals("Item 4")) {
Toast.makeText(getActivity().getBaseContext(),
"Try Choose : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}else if (SpinerValue3.equals("Item 5")) {
Toast.makeText(getActivity().getBaseContext(),
"Try Choose : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}else if (SpinerValue3.equals("Item 6")) {
Toast.makeText(getActivity().getBaseContext(),
"Try Choose : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
如果上面我解释为祝酒,但如何获取Activity.class上的文本微调项目
Activity.class我想改变 SPINNERITEM (在下面的代码中)使用来自Fragment的g et Spinner项目文本
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.NetworkImageView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class RekomendasiExercise extends AppCompatActivity implements AdapterView.OnItemClickListener{
// Log tag
private static final String TAG = AbdominalFragment.class.getSimpleName();
private static final String url = "http://........php";
private ProgressDialog pDialog;
private List<Exercise> exerciseList = new ArrayList<Exercise>();
private ListView listView;
private CustomListAdapter adapter;
public RekomendasiExercise() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
final ListView listView = (ListView)findViewById(R.id.list);
final Spinner spinner = (Spinner) findViewById(R.id.choices);
adapter = new CustomListAdapter(this, exerciseList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
if(exerciseList.isEmpty()) {
// Creating volley request obj
JsonArrayRequest exerciseReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
exerciseList.clear();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
String text = spinner.getSelectedItem().toString();
int positionitem = spinner.getSelectedItemPosition();
JSONObject obj = response.getJSONObject(i);
Exercise exercise = new Exercise();
if (obj.getString("type").equals("SPINNERITEM")) {
exercise.setNama(obj.getString("name"));
exercise.setTipe(obj.getString("type"));
exercise.setMainmuscle(obj.getString("mainmuscle"));
exerciseList.add(exercise);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(exerciseReq);
}else{
hidePDialog();
}
listView.setOnItemClickListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
// Create custom dialog object
final Dialog dialog = new Dialog(this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog); // layout of your dialog
// Set dialog title
dialog.setTitle("Detail");
// set values for custom dialog components - text, image and button
TextView nama = (TextView) dialog.findViewById(R.id.nama);
nama.setText("Nama = " + exerciseList.get(position).getNama());
// similar add statements for other details
dialog.show();
}
}
答案 0 :(得分:0)
我的建议是遵循Activity
和Fragment
的标准通信模型,其中Activity
实现了片段中定义的回调接口。
查看此link了解详情。
在微调器的onItemSelected
方法中,您可以使用选定的微调器值调用Activity
的回调方法。
答案 1 :(得分:0)
步骤1)在活动类
中创建微调器项值变量 public class RekomendasiExercise extends AppCompatActivity implements AdapterView.OnItemClickListener{
private String currentSpinnerItem ;
/*
set current spinner item value
*/
public void setCurrentSpinnerItem(String itemValue)
{
this.currentSpinnerItem = itemValue ;
//Do stuffs in activity with new value
}
步骤2)现在在你的片段类中更新currentItemSpinner这样的值
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
//Update spinner selected item value in activity
((RekomendasiExercise) getActivity).setCurrentSpinnerItem(SpinerValue3);
}