java - 从 Post Method Json 数据到 Android Studio 中的 Listview 的 CheckBox TextView?
问题描述
我写了一个代码 CheckBox TextView 从 Post Json api 数据到 Android Studio 中的 Listview 。我在运行程序时看到了列表视图项目。而不是我想选择项目并单击所选项目的按钮= 1并将方法数据发布到api。
package com.ceta.listviewchecked;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
MyCustomAdapter dataAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Generate list View from ArrayList
displayListView();
checkButtonClick();
}
private void displayListView() {
//Array list of operasyon
ArrayList<Operasyon> operasyonList = new ArrayList<Operasyon>();
Operasyon operasyon = new Operasyon("1","Operasyon",false);
operasyonList.add(operasyon);
operasyon = new Operasyon("2","Operasyon",true);
operasyonList.add(operasyon);
operasyon = new Operasyon("3","Operasyon",false);
operasyonList.add(operasyon);
operasyon = new Operasyon("4","Operasyon",true);
operasyonList.add(operasyon);
operasyon = new Operasyon("5","Operasyon",true);
operasyonList.add(operasyon);
operasyon = new Operasyon("6","Operasyon",false);
operasyonList.add(operasyon);
operasyon = new Operasyon("7","Operasyon",false);
operasyonList.add(operasyon);
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.operasyon_info, operasyonList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Operasyon operasyon = (Operasyon) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Clicked on Row: " + operasyon.getName(),
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Operasyon> {
private ArrayList<Operasyon> operasyonList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Operasyon> operasyonList) {
super(context, textViewResourceId, operasyonList);
this.operasyonList= new ArrayList<Operasyon>();
this.operasyonList.addAll(operasyonList);
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.operasyon_info, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.textView);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Operasyon operasyon = (Operasyon) cb.getTag();
Toast.makeText(getApplicationContext(),
"Onay Kutusuna tıklandı: " + cb.getText() +
" ... " + cb.isChecked(),
Toast.LENGTH_LONG).show();
operasyon.setSelected(cb.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Operasyon operasyon = operasyonList.get(position);
holder.code.setText(" (" + operasyon.getCode() + ")");
holder.name.setText(operasyon.getName());
holder.name.setChecked(operasyon.isSelected());
holder.name.setTag(operasyon);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("Secilen Operasyonlar...\n");
ArrayList<Operasyon> operasyonList = dataAdapter.operasyonList ;
for(int i=0;i<operasyonList.size();i++){
Operasyon operasyon = operasyonList.get(i);
if(operasyon.isSelected()){
responseText.append("\n" + operasyon.getName());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
}
在数组列表操作中,我想发布 api 并获取 json 数据,然后在 listView 中查看。当我选择项目时,单击复选框,当单击按钮时,将 api 发布到服务器并作为“选定项目 = 1”发送。
如果你能帮助我将不胜感激
此致。
解决方案
推荐阅读
- react-native - 如何在 React Native 0.63 中解析 RSS 提要?
- python - 以新日期作为预测扩展多索引数据框
- javascript - 挂钩 setState 不会立即更新
- mongodb - MongoDb 到 AWS Quicksight
- html - 在选择 angularjs 中为选定选项设置颜色
- javascript - 以 2D 格式更改 JSON 反应原生
- c# - JsonConvert.DeserializeObject 返回 null asp.net 核心
- javascript - 如何用另一个数组值过滤数组
- lua - 我正在制作一个 roblox 游戏,当您单击底板时需要更新排行榜统计信息
- php - PHP没有执行