我是新手Android开发者。我想有人帮助我。 现在,我可以将JSON数据调用到我的ListView。我做了导入复选框到ListView。但它没有用。当我在某个复选框上被选中时,将选择其他复选框,例如下面的示例事件。
复选框名称
X A
_ B
_ C
_ D
_ E
_ F
__________________<<底部屏幕
X G
_ H
_我
当我选中复选框" A"。只会选择另一个复选框(复选框G)。
抱歉我的英语能力差。 先生,请帮帮我。
SearchActivity.java
package com.example.rattapongt.rjp_b;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.rattapongt.rjp_b.Date.myCalendarView;
import com.example.rattapongt.rjp_b.Util.Config;
import com.example.rattapongt.rjp_b.Util.RequestHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressDialog loading;
private String JSON_STRING;
ListView listView;
TextView tvDate, tvDateHide;
EditText editTextId, editTextId2;
ImageView ivSearch;
Button btnDate, btnReject;
private int mYear;
private int mMonth;
private int mDay;
private String mYearCheck;
static final int CALENDAR_VIEW_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listView = (ListView) findViewById(R.id.listView);
editTextId = (EditText) findViewById(R.id.editTextId);
editTextId2 = (EditText) findViewById(R.id.editTextId2);
ivSearch = (ImageView) findViewById(R.id.ivSearch);
tvDate = (TextView) findViewById(R.id.tvDate);
tvDateHide = (TextView) findViewById(R.id.tvDateHide);
btnDate = (Button) findViewById(R.id.btnDate);
btnReject = (Button) findViewById(R.id.btnReject);
ivSearch.setOnClickListener(this);
btnReject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent = new Intent(SearchActivity.this, RejectActivity.class);
//startActivity(intent);
}
});
editTextId.setText("1992");
editTextId2.setText("PCB116A731 ");
// add a click listener to the button
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SearchActivity.this, myCalendarView.class);
startActivityForResult(intent, CALENDAR_VIEW_ID);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CALENDAR_VIEW_ID:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
tvDate.setText(bundle.getString("dateSelected1"));
tvDateHide.setText(bundle.getString("dateSelected"));
break;
}
}
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(SearchActivity.this, "Fetching Data", "Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showEmployee();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(Config.URL_GET_ALL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
private void showEmployee() {
JSONObject jsonObject = null;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
String Ta = jo.getString(Config.TAG_A);
String Tb = jo.getString(Config.TAG_B);
String Tc = jo.getString(Config.TAG_C);
String Td = jo.getString(Config.TAG_D);
String Te = jo.getString(Config.TAG_E);
String Tf = jo.getString(Config.TAG_F);
String Tg = jo.getString(Config.TAG_G);
HashMap<String, String> employees = new HashMap<>();
employees.put(Config.TAG_A, Ta);
employees.put(Config.TAG_B, Tb);
employees.put(Config.TAG_C, Tc);
employees.put(Config.TAG_D, Td);
employees.put(Config.TAG_E, Te);
employees.put(Config.TAG_F, Tf);
employees.put(Config.TAG_G, Tg);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
SearchActivity.this, list, R.layout.item_list,
new String[]{Config.TAG_A, Config.TAG_B, Config.TAG_C, Config.TAG_D, Config.TAG_E, Config.TAG_F, Config.TAG_G},
new int[]{R.id.invoiceNo, R.id.code, R.id.name, R.id.itemNo, R.id.poNo, R.id.sq, R.id.qty});
listView.setAdapter(adapter);
}
private void getData() {
loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);
//String url = Config.DATA_URL + editTextId.getText().toString().trim();
String url = Config.DATA_URL + "&strKeyword=" + editTextId.getText().toString().trim()
+ "&strKeyword3=" + tvDateHide.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(SearchActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
loading.dismiss();
lostConnection();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
JSONObject jsonObject = null;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONObject jsonObjectSearch = new JSONObject(response);
JSONArray result = jsonObjectSearch.getJSONArray(Config.JSON_ARRAY);
//For map values for JSON to android
/*
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
vc = collegeData.getString(Config.KEY_VC);
*/
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
String Ka = jo.getString(Config.TAG_A);
String Kb = jo.getString(Config.TAG_B);
String Kc = jo.getString(Config.TAG_C);
String Kd = jo.getString(Config.TAG_D);
String Ke = jo.getString(Config.TAG_E);
String Kf = jo.getString(Config.TAG_F);
String Kg = jo.getString(Config.TAG_G);
HashMap<String, String> employees = new HashMap<>();
employees.put(Config.TAG_A, Ka);
employees.put(Config.TAG_B, Kb);
employees.put(Config.TAG_C, Kc);
employees.put(Config.TAG_D, Kd);
employees.put(Config.TAG_E, Ke);
employees.put(Config.TAG_F, Kf);
employees.put(Config.TAG_G, Kg);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
//That will be show information to textView.
/*
if (!name.toString().equals("null")) {
//textViewResult.setText("Name:\t" + name + "\nDesignation:\t" + address + "\nSalary:\t" + vc);
Toast.makeText(this, response, Toast.LENGTH_LONG).show();
} else {
textViewResult.setText("Data not found!");
}
//textViewResult.setText("Name:\t" + name + "\nDesignation:\t" + address + "\nSalary:\t" + vc);
*/
ListAdapter adapter = new SimpleAdapter(
SearchActivity.this, list, R.layout.item_list,
new String[]{Config.KEY_A, Config.KEY_B, Config.KEY_C, Config.KEY_D, Config.KEY_E, Config.KEY_F, Config.KEY_G},
new int[]{R.id.invoiceNo, R.id.code, R.id.name, R.id.itemNo, R.id.poNo, R.id.sq, R.id.qty});
//Toast.makeText(this, response, Toast.LENGTH_LONG).show();
listView.setAdapter(adapter);
}
}
&#13;
CustomAdapter.java
package com.example.rattapongt.rjp_b.Util;
/**
* Created by Rattapongt on 2/23/2017.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.rattapongt.rjp_b.R;
public class CustomAdapter extends BaseAdapter {
Context mContext;
String[] strName;
String[] strName1;
String[] strName2;
String[] strName3;
String[] strName4;
String[] strName5;
String[] strName6;
int[] resId;
public CustomAdapter(Context context, String[] strName, String[] strName1, String[] strName2, String[] strName3, String[] strName4, String[] strName5, String[] strName6) {
this.mContext = context;
this.strName = strName;
this.strName1 = strName1;
this.strName2 = strName2;
this.strName3 = strName3;
this.strName4 = strName4;
this.strName5 = strName5;
this.strName6 = strName6;
this.resId = resId;
}
public int getCount() {
return strName.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater mInflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view = mInflater.inflate(R.layout.item_list, parent, false);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(strName[position]);
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText(strName1[position]);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
textView2.setText(strName2[position]);
TextView textView3 = (TextView) view.findViewById(R.id.textView3);
textView3.setText(strName3[position]);
TextView textView4 = (TextView) view.findViewById(R.id.textView4);
textView4.setText(strName4[position]);
TextView textView5 = (TextView) view.findViewById(R.id.textView5);
textView5.setText(strName5[position]);
TextView textView6 = (TextView) view.findViewById(R.id.textView6);
textView6.setText(strName6[position]);
return view;
}
}
&#13;
答案 0 :(得分:1)
获取所选复选框的位置并将其存储在arraylist中,然后在getView方法中将位置与所选位置列表进行比较。如果列表具有该位置,则选中复选框,否则取消选中。
答案 1 :(得分:0)
我认为删除if(view == null)将完成这项工作。试试它,就像在下面的代码中一样:
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.grid_parent_cat_individual_items, null);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(strName[position]);
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText(strName1[position]);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
textView2.setText(strName2[position]);
TextView textView3 = (TextView) view.findViewById(R.id.textView3);
textView3.setText(strName3[position]);
TextView textView4 = (TextView) view.findViewById(R.id.textView4);
textView4.setText(strName4[position]);
TextView textView5 = (TextView) view.findViewById(R.id.textView5);
textView5.setText(strName5[position]);
TextView textView6 = (TextView) view.findViewById(R.id.textView6);
textView6.setText(strName6[position]);
return view;
}