我正在开发一个应用程序,我已使用listview
解析器填充json
。现在我必须在这个listview
单行中添加本地微调器数组。
如何在此listview
行中添加本地微调器数组。而且我想要选择listview
的微调值。以下是我的代码,请建议我。
//activity
public class MyModifyPgmLvAddUserActivity1 extends AppCompatActivity {
private DBManager dbManager;
private ListView listView, listView1;
Toolbar toolbar;
private String TAG = MyModifyPgmLvAddUserAdapter.class.getSimpleName();
private ProgressDialog pDialog;
// private SimpleCursorAdapter adapter;
MyModifyPgmLvAddUserAdapter1 adapter;
static String RANK = "AsanaId";
static String COUNTRY = "Asananame";
static String POPULATION = "repetation";
ArrayList<PojoList> SubjectList = new ArrayList<PojoList>();
final static String[] mSpinnerItems = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
private static String url = "http://www.test.com/ypAndroid/api/getDefaultProgramlist";
String asananame, asanaid,duration, imageurl, imageversion, audiourl, audioversion, videourl, videoversion;
String sid;
boolean selected;
String id;
String[] subject = null;
String[] spinner = null;
String title;
long id1;
Button btnCreate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_modifypgm_lv_add_user);
btnCreate = (Button) findViewById(R.id.button_submit);
dbManager = new DBManager(this);
dbManager.open();
Intent i = getIntent();
id= i.getStringExtra("id");
subject = i.getStringArrayExtra("subject");
spinner = i.getStringArrayExtra("spinner");
title= i.getStringExtra("title");
id1= Long.parseLong(id);
SubjectList = new ArrayList<>();
listView = (ListView) findViewById(R.id.listview);
//Net Connection
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null){
AlertDialog.Builder alert = new AlertDialog.Builder(MyModifyPgmLvAddUserActivity1.this);
// new AlertDialog.Builder(MyModifyProgramEditDeleteActivity.this)
alert.setTitle(getResources().getString(R.string.app_name));
alert.setMessage(getResources().getString(R.string.no_internet));
alert.setPositiveButton("OK", null).show();
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something here where "ok" clicked and then perform intent from activity context
Intent intent = new Intent(MyModifyPgmLvAddUserActivity1.this, MyYogaPosesSubMenuActivity.class);
MyModifyPgmLvAddUserActivity1.this.startActivity(intent);
}
});
alert.show();
}
else {
new MyModifyPgmLvAddUserActivity1.DownloadJSON().execute();
}
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkButtonClick();
Intent modify_intent = new Intent(MyModifyPgmLvAddUserActivity1.this, MyModifySavedProgramActivity1.class);
modify_intent.putExtra("subject", spinner);
modify_intent.putExtra("spinner", subject);
modify_intent.putExtra("id", id);
modify_intent.putExtra("title", title);
startActivity(modify_intent);
}
});
}
private void checkButtonClick() {
ArrayList<String> list1= new ArrayList<>();
ArrayList<String> list2= new ArrayList<>();
// ArrayList<String> list3= new ArrayList<>();
PojoList pojo = null;
ArrayList<PojoList> countryList = adapter.SubjectListTemp;
for (int i = 0; i < countryList.size(); i++) {
pojo = countryList.get(i);
if (pojo.isSelected()) {
// responseText.append("\n" + country.getName());
list1.add(pojo.getAsanName());
// list2.add(convertArrayToString(subject));
list2.add(pojo.getAsanId());
// list2.add(convertArrayToString(spinner));
}
}
String [] aIds = list1.toArray(new String[list1.size()]);
String [] aNames = list2.toArray(new String[list2.size()]);
dbManager.update(id1, aIds, aNames);
// dbManager.insert(title, names, spinner);
// dbManager.set_datetime_next(id, names, spinner);
}
public static String[] convertStringToArray(String str){
String[] arr = str.split(strSeparator);
return arr;
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyModifyPgmLvAddUserActivity1.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
PojoList pojo;
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject o = new JSONObject(jsonStr);
JSONArray restaurants = o.getJSONArray("getdefaultprogramlist");
for (int k = 0; k < restaurants.length(); k++) {
JSONObject rating1 = restaurants.getJSONObject(k);
asanaid = rating1.getString("programid");
Log.d("TestJson asanaid", asanaid);
asananame = rating1.getString("programname");
Log.d("TestJson asananame", asananame);
for (int i = 0; i < restaurants.length(); i++) {
JSONObject restaurant = restaurants.getJSONObject(i);
JSONArray ratings = restaurant.getJSONArray("ProgramItem");
for (int j = 0; j < ratings.length(); j++) {
JSONObject rating = ratings.getJSONObject(j);
String stepnumber = rating.getString("AsanaId");
Log.d("TestJson stepnumber", stepnumber);
String stepdesc = rating.optString("Asananame");
Log.d("TestJson stepdesc", stepdesc);
String stepimg = rating.getString("duration");
Log.d("TestJson stepimg", stepimg);
String stepimgeversion = rating.getString("repetation");
Log.d("TestJson stepimgvrsion ", stepimgeversion);
// int sid = Integer.parseInt(convertArrayToString(mSpinnerItems));
pojo = new PojoList(stepnumber, stepdesc, selected, convertArrayToString(mSpinnerItems));
SubjectList.add(pojo);
System.out.println("Size of Entry list from Map: " + SubjectList.size());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void args) {
if (pDialog.isShowing())
pDialog.dismiss();
adapter = new MyModifyPgmLvAddUserAdapter1(MyModifyPgmLvAddUserActivity1.this, R.layout.my_yoga_poseslv_add_user_single_item, SubjectList);
listView.setAdapter(adapter);
}
}
public static String strSeparator = ",";
public static String convertArrayToString(String[] array){
String str = "";
for (int i = 0;i<array.length; i++) {
str = str+array[i];
// Do not append comma at the end of last element
if(i<array.length-1){
str = str+strSeparator;
}
}
return str;
}
}
//PojoList
public class PojoList {
String AsanName = null;
String AsanId = null;
boolean selected = false;
String sid = null;
String sarray = null;
boolean sSelected = false;
public PojoList(String Sname, String SFullForm, boolean selected, String Sarray) {
super();
this.AsanName = Sname;
this.AsanId = SFullForm;
this.selected = selected;
this.sarray = Sarray;
// this.sid = Sid;
}
public String getAsanName() {
return AsanName;
}
public void setAsanName(String code) {
this.AsanName = code;
}
public String getAsanId() {
return AsanId;
}
public void setAsanId(String id) {
this.AsanId = id;
}
@Override
public String toString() {
return AsanName + " " + AsanId ;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getId() {
return sarray;
}
public void setId(String sarray) {
this.sarray = sarray;
}
}
//适配器
public class MyModifyPgmLvAddUserAdapter1 extends ArrayAdapter<PojoList> {
public ArrayList<PojoList> MainList;
public ArrayList<PojoList> SubjectListTemp;
Context context;
private String[] mSpinnerItems = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
// public ListAdapter.SubjectDataFilter subjectDataFilter ;
public MyModifyPgmLvAddUserAdapter1(Context context, int id, ArrayList<PojoList> subjectArrayList) {
super(context, id, subjectArrayList);
this.SubjectListTemp = new ArrayList<PojoList>();
this.SubjectListTemp.addAll(subjectArrayList);
this.MainList = new ArrayList<PojoList>();
this.MainList.addAll(subjectArrayList);
}
public class ViewHolder {
TextView SubjectName, aId;
TextView SubjectFullForm, aName;
CheckBox cb;
Spinner spinner;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.my_yoga_poseslv_add_user_single_item, null);
holder = new ViewHolder();
holder.aName = (TextView) convertView.findViewById(R.id.listview_tv);
holder.aId = (TextView) convertView.findViewById(R.id.listview_id);
holder.cb = (CheckBox) convertView.findViewById(R.id.listview_cb);
holder.spinner = (Spinner) convertView.findViewById(R.id.listview_spinner);
holder.cb.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
PojoList pojo = (PojoList) cb.getTag();
Toast.makeText(getContext(),
"Clicked on Checkbox: " + cb.getText() +
"Clicked on Checkbox: "+
" is " + cb.isChecked() + "Id is : "+ pojo.getAsanName(),
Toast.LENGTH_LONG).show();
pojo.setSelected(cb.isChecked());
}
});
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_1, mSpinnerItems);
holder.spinner.setAdapter(spinnerAdapter);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PojoList pojo = SubjectListTemp.get(position);
holder.aName.setText(pojo.getAsanName());
holder.cb.setText(pojo.getAsanId());
holder.cb.setChecked(pojo.isSelected());
holder.cb.setTag(pojo);
// holder.spinner.setSelection(Integer.parseInt(pojo.getId()));
holder.spinner.setSelected(pojo.isSelected());
return convertView;
}
}
//列表行
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#cccccc">
<Spinner
android:id="@+id/listview_spinner"
style="@style/AppTheme"
android:layout_width="42dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="21dp"
android:layout_marginRight="21dp" />
<CheckBox
android:id="@+id/listview_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="2dp"
android:layout_marginLeft="4dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/listview_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:text="TextView"
android:textSize="1sp"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/listview_imgs"
android:layout_toEndOf="@+id/listview_imgs" />
<TextView
android:id="@+id/listview_id"
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/listview_spinner"
android:layout_alignRight="@+id/listview_spinner"
android:layout_alignTop="@+id/listview_imgs"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:3)
我创建了一个基本项目,向您展示如何设置一个简单的Spinner。这是列表行的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:padding="20dp">
<TextView
android:id="@+id/row_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/row_item_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="2dp" />
这是listview的适配器:
public class BasicSpinnerAdapter extends BaseAdapter {
private ArrayList<String> mSpinnerItems;
private ArrayList<String> mData;
private Context mContext;
public BasicSpinnerAdapter(ArrayList<String> data, ArrayList<String> spinnerItems, Context context) {
mData = data;
mContext = context;
mSpinnerItems = spinnerItems;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_item_spinner, null);
}
TextView textView = (TextView) view.findViewById(R.id.row_item_textview);
Spinner spinner = (Spinner) view.findViewById(R.id.row_item_spinner);
textView.setText(mData.get(position));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, mSpinnerItems);
spinner.setAdapter(adapter);
return view;
}
这是我的活动,我开始这一切:
public class SpinnerListviewActivity extends AppCompatActivity {
private ListView lvSpinner;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_listview_activity);
lvSpinner = (ListView) findViewById(R.id.listview_spinner);
ArrayList<String> mData = new ArrayList<>();
mData.add("Test1");
mData.add("Test2");
mData.add("Test3");
mData.add("Test4");
ArrayList<String> mSpinnerData = new ArrayList<>();
mSpinnerData.add("1");
mSpinnerData.add("2");
mSpinnerData.add("3");
mSpinnerData.add("4");
BasicSpinnerAdapter adapter = new BasicSpinnerAdapter(mData, mSpinnerData, this);
lvSpinner.setAdapter(adapter);
}
}
我的活动的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
希望这能让您了解如何在List行中设置基本Spinner。随意提出任何问题!