首页 > 解决方案 > 从 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”发送。

如果你能帮助我将不胜感激

此致。

标签: javajsonapiandroid-studiopost

解决方案


推荐阅读