- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个充满 API 数据的数组,我有一个可扩展 ListView 来显示该数组的项目,现在我要做的是当用户点击一个项目时它会保存该项目的 ID到一个数组中,所以如果用户在我的数组中选择我想要的 2 个 ID 的 2 个项目,现在发生的是:无论我是否只选择其中一个项目,它都会获取所有项目并将其保存在我的数组中。
public class MainActivity extends Fragment {
private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;
private Button notificar;
private Context context;
MainActivity mContext;
private Button footer;
private double RaioEscola;
private CircleOptions mCircle;
GPSTracker tracker;
private Location location;
private Integer IdEscola;
public MainActivity() {
}
public MainActivity(Context context) {
this.context = context;
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.expand, container, false);
ExpandList = (ExpandableListView) rootView.findViewById(R.id.exp_list);
notificar = (Button) rootView.findViewById(R.id.btnGetMoreResults);
new asyncTask(MainActivity.this).execute();
return rootView;
}
private class asyncTask extends AsyncTask<String, Void, Void> {
private ProgressDialog pd;
public asyncTask(MainActivity context) {
mContext = context;
pd = new ProgressDialog(getActivity());
pd.setTitle("Por Favor Espere ...");
pd.setMessage("Enviando ...");
if (!pd.isShowing()) {
pd.show();
}
}
@Override
protected Void doInBackground(final String... params) {
try {
String[] resposta = new WebService().get("filhos");
if (resposta[0].equals("200")) {
JSONObject mJSONObject = new JSONObject(resposta[1]);
JSONArray dados = mJSONObject.getJSONArray("data");
/* cria o array que vai receber os dados da api */
final ArrayList<Escolas> mArrayList = new ArrayList<Escolas>();
/* percorre o array, adicionando cada linha encontrada em um ArrayList */
for (int i = 0; i < dados.length(); i++) {
JSONObject item = dados.getJSONObject(i);
Escolas mEscolas = new Escolas();
mEscolas.setId_escola(item.optInt("id_escola"));
mEscolas.setCnpj(item.getString("cnpj"));
mEscolas.setRazao_social(item.getString("razao_social"));
mEscolas.setNome_fantasia(item.getString("nome_fantasia"));
mEscolas.setDistancia(Float.valueOf(item.getString("distancia")));
mEscolas.setLogradouro(item.optString("logradouro"));
mEscolas.setNumero(item.optString("numero"));
mEscolas.setBairro(item.getString("bairro"));
mEscolas.setComplemento(item.getString("complemento"));
mEscolas.setCep(item.getString("cep"));
mEscolas.setCidade(item.getString("cidade"));
mEscolas.setEstado(item.getString("estado"));
mEscolas.setLatitude(Float.parseFloat(item.getString("latitude")));
mEscolas.setLongitude(Float.parseFloat(item.getString("longitude")));
RaioEscola = Double.parseDouble(String.valueOf(mEscolas.getDistancia()));
IdEscola = mEscolas.getId_escola();
JSONObject alunos = item.optJSONObject("alunos");
JSONArray data = alunos.getJSONArray("data");
if (data != null) {
ArrayList<Filhos> arrayalunos = new ArrayList<Filhos>();
for (int a = 0; a < data.length(); a++) {
Filhos mFilhos = new Filhos();
JSONObject clientes = data.getJSONObject(a);
mFilhos.setId_aluno(clientes.optInt("id_aluno"));
mFilhos.setNome(clientes.optString("nome"));
mFilhos.setSobrenome(clientes.optString("sobrenome"));
mFilhos.setFoto(clientes.optString("foto"));
mFilhos.setModalidade_de_ensino(clientes.optString("modalidade_de_ensino"));
mFilhos.setObservacoes(clientes.optString("observacoes"));
arrayalunos.add(mFilhos);
}
mEscolas.setalunos(arrayalunos);
}
/* popula o array de viagens */
mArrayList.add(mEscolas);
ExpAdapter = new ExpandListAdapter(getActivity(), mArrayList);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ExpandList.setAdapter(ExpAdapter);
ExpAdapter.notifyDataSetChanged();
ExpAdapter.setChoiceMode(ExpandListAdapter.CHOICE_MODE_MULTIPLE);
ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(final ExpandableListView parent, View v, final int groupPosition, final int childPosition, final long id) {
ExpAdapter.setClicked(groupPosition, childPosition);
final int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
parent.setSelectedChild(groupPosition, childPosition, true);
parent.getChildAt(index);
notificar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
class update extends TimerTask {
public void run() {
try {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
final float latitude = mArrayList.get(groupPosition).getLatitude();
final float longitude = mArrayList.get(groupPosition).getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
drawMarkerWithCircle(latLng);
GPSTracker gps = new GPSTracker(getActivity());
double latitudegps = gps.getLatitude();
double longitudegps = gps.getLongitude();
float[] distance = new float[2];
Location.distanceBetween(mCircle.getCenter().latitude, mCircle.getCenter().longitude, latitudegps, longitudegps, distance);
if (distance[0] > mCircle.getRadius()) {
Toast.makeText(getActivity(), "Outside", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getActivity(), "Inside", Toast.LENGTH_LONG).show();
AlertRest mAlertRest = new AlertRest();
try {
List<Integer> myIdList = new ArrayList<Integer>();
for (int i = 0; i < mArrayList.get(groupPosition).getalunos().size(); i++) {
Integer Idalunos = mArrayList.get(groupPosition).getalunos().get(i).getId_aluno();
myIdList.add(Idalunos);
}
mAlertRest.getNotificacao(1, mArrayList.get(groupPosition).getId_escola(), String.valueOf(myIdList), latitudegps, longitudegps);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Timer timer = new Timer();
timer.schedule(new update(), 0, 15000);
}
private void drawMarkerWithCircle(LatLng position) {
mCircle = new CircleOptions().center(position).radius(RaioEscola);
}
});
return false;
}
});
}
});
}
/* retorna um array de objetos */
}
else {
throw new Exception("[" + resposta[0] + "] ERRO: " + resposta[1]);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void cursor) {
if (pd.isShowing()) {
pd.dismiss();
}
}
}
}
警报休息:
public class AlertRest extends WebService {
private String recurso = "notificacoes";
private String[] resposta;
private AlertModelo mAlertModelo;
public AlertModelo getNotificacao(Integer id_usuario, String token, Integer id_escola, String ids_aluno, Double latitude, Double longitude) throws Exception {
/* dispara a requisição para a API retornar os dados do "recurso" necessário */
resposta = new WebService().postToken(recurso, token, "{\"id_usuario\":" + id_usuario + "," + "\"id_escola\":" + id_escola + "," + "\"ids_aluno\":\"" + ids_aluno + "\"," + "\"latitude\":" + latitude + "," + "\"longitude\":" + longitude + "}");
JSONObject mJSONObject = new JSONObject(resposta[1]);
if (resposta[1].equals("201")) {
mAlertModelo = new AlertModelo();
mAlertModelo.setId_usuario(mJSONObject.getInt(String.valueOf(id_usuario)));
mAlertModelo.setId_escola(mJSONObject.getInt(String.valueOf(id_escola)));
mAlertModelo.setIds_aluno(mJSONObject.getInt(String.valueOf(ids_aluno)));
mAlertModelo.setLatitude(mJSONObject.getDouble(String.valueOf(latitude)));
mAlertModelo.setLongitude(mJSONObject.getDouble(String.valueOf(longitude)));
}
return mAlertModelo;
}
}
最佳答案
由于嵌套循环和运行任务很少,代码很难理解。无论如何,我怀疑下面的代码导致您的应用程序保存列表中的所有(未选择的)项目:
for (int i = 0; i < mArrayList.get(groupPosition).getalunos().size(); i++) {
Integer Idalunos = mArrayList.get(groupPosition).getalunos().get(i).getId_aluno();
myIdList.add(Idalunos);
}
注意:遍历整个列表是可疑的。它应该选择列表中的一个或多个特定项目。
所以...另一组相关代码可以帮助我们确定代码修复。我不确定这里是否也有错误。
parent.setItemChecked(index, true);
parent.setSelectedChild(groupPosition, childPosition, true);
parent.getChildAt(index);
注意:这些代码可能没问题。我认为关键代码是 index。我不太熟悉 ExpandableListView。
最后,建议的代码修复:
Integer Idalunos = mArrayList.get(groupPosition).getalunos().get(index).getId_aluno();
...
myIdList.add(Idalunos);
注意变量 index 用于获取正确的项目,Idalunos。
关于android - 具有多项选择的 ExpandableListView 将所选项目保存到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239903/
我只是想知道Mvvmmcross框架中是否潜藏着ExpandableListViews或与之相似的功能,还是这种控件不适用于多平台要求。 的功能在 http://deapsquatter.blogsp
我想知道是否可以将一个 ExpandableListView 作为另一个 ExpandableListView 的一个元素的一个子元素。谢谢 最佳答案 是的。可以在可扩展 ListView 中创建可扩
现在我想使用 ExpandableListView 来显示我的数据,我想更改项目左侧的图标或让它消失。有人可以给我一个建议吗。 谢谢。 最佳答案 setGroupIndicator(null) 应该让
我在 expandablelistview 子项目中有一个 edittext。然后,我在这个 edittext 中输入了一个文本,但是每当我点击其他 expandablelistview 标题时,我输
我正在尝试弄清楚如何构建包含(许多)的 View : PARENT1(可勾选、可扩展) CHILD1(单选按钮) CHILD2(单选按钮) ... PARENT2(可勾选、可扩展) CHILD1(可检
所以我刚刚进入了 ExpandableListView 领域,并希望在 DrawerLayout 中制作一个只有一组和 3 个 child 的简单列表 这是我的适配器代码: public cl
我是 Android 开发新手。我关注了this显示 ExpandableListView 及其正常工作的教程。如果我按所有组,那么所有组都会展开。 我一次只想扩展一组。一开始,第一个组将自动展开,如
我是 Android 新手。我想创建一个可扩展的 ListView,其中 1 行具有必须在 manin 线程中指定的自定义信息,而不是在布局中进行硬编码。 我想要一个图像和 2 个 TextView
我的应用程序中有一个 ExpandableListView。我想让子布局比组标题窄一点,并保持居中。由于一些视觉效果,我必须从代码中设置布局参数,但是我似乎找不到最好的解决方案,尝试从 xml 更
我正在将 JSON 解析为 ExpandableListView,在每个子项上,用户可以选择他想要拥有的 +/- 按钮的每个子项的数量。 +/- 按钮连接到 TextView,其中显示每个子项的总金额
我希望我的团队能够在第一时间扩大。然后它应该像往常一样运行(单击指示器后折叠/展开) 我找不到解决此问题的任何有效解决方案。 最佳答案 循环遍历所有组并使用expandGroup()您的 Expand
我正在编写一个 Android 应用程序,我决定使用可扩展 ListView 。我拥有所需的一切,列表工作正常,但是当我填充它时,组的顺序似乎与应有的顺序不同,这是一个示例: 如您所见,子值很好,只有
我在组项目的 ExpandableListView 中添加了 EditText,之后当我单击组项目时,不会出现包含子项目的列表。可能是什么问题?在这种情况下,这个列表是否可以扩展?或者禁止在Expan
当我单击组 View 时,可扩展列表的顺序会发生变化 我发现有人有类似的问题 here 顺序似乎是随机的,我不明白发生了什么。 这是我的代码,我知道它很乱,我已经重写了很多次。 public clas
我是 Android 编程的新手,我一直在思考如何做到这一点……我想创建一个 ExpandableListView,其中包含相同的子项 XML(包含添加、编辑和删除按钮)填充在所有组项目中。 有人可以
我在 Android 抽屉导航中使用 ExpandableListsViews 而不是菜单。而 OnExpandlistner 我正在改变背景颜色。前两个项目表现正常,它们的背景颜色发生了变化。但最后
我对 ExpandableListView 有疑问:在我的应用程序中,我使用了 ExpandableListView 并用我的适配器填充它“类 MyExpandableListAdapter 扩展了
我正在使用 Fedor 的适配器和图像加载器将图像延迟加载到可扩展 ListView 中。 ( Lazy load of images in ListView ) 它运行良好,但有一个问题。仅当 Im
关注this教程我能够制作一个大致这样布局的 View : Group1 child1 child2 group2 child1 child2 但是我想要更多的嵌套: grou
Blockquote 我在 android 中使用 ExpandableListView 这是适配器类中getChildView的代码: @Override public View g
我是一名优秀的程序员,十分优秀!