从列表视图中获取项值而不调用onItemClickListner

时间:2018-02-15 16:20:15

标签: java android

我想做什么: 我想获取所有List View项并将其发送到其他活动 或者获取List View项而不声明o nItemClickListner当我获得列表项目时我希望它们存储到另一个列表中并将该列表发送到其他活动,我可以从列表中获取所有项目

问题 如何获取单个列表视图项的值包含多个字段,即编辑文本

我在做什么

 public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.save_information){

            list = new ArrayList();

            for (int i =0 ; i < arrayAdapter.getCount() ; i++){

                Language_data_comes_here l = arrayAdapter.getItem(i);
                list.add(l);

            }



            Intent i = new Intent (Language_Activity.this , Selecting_Activity.class);
            i.putExtra("List" , list);
            startActivity(i);

        }
        return super.onOptionsItemSelected(item);
    }

语言活动

   package com.example.wildstone.cv_maker;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class Language_Activity extends AppCompatActivity {

    ListView listView ;
    FloatingActionButton floatingActionButton ;
    Custom_Adapter_for_Language arrayAdapter ;
    ArrayList<Language_data_comes_here> arr ;
    Toolbar toolbar ;
    String language_1 = null ;
    ArrayList list ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language_);

        Toolbar toolbar1 = (Toolbar) findViewById(R.id.toolbar_custom);
        setSupportActionBar(toolbar1);

        floatingActionButton = (FloatingActionButton) findViewById(R.id.floating_action_button);
        floatingActionButton.setImageResource(R.mipmap.plus);
        floatingActionButton.setBackgroundColor(Color.WHITE);

        listView = (ListView) findViewById(R.id.list_view);
        arr = new ArrayList<>();
        arrayAdapter = new Custom_Adapter_for_Language(Language_Activity.this , arr);

        listView.setAdapter(arrayAdapter);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final AlertDialog.Builder builder = new AlertDialog.Builder(Language_Activity.this);
                LayoutInflater layoutInflater = getLayoutInflater() ;

                View view  = layoutInflater.inflate(R.layout.enter_language , null);
                final EditText language = (EditText) view.findViewById(R.id.language_entered);

                toolbar = (Toolbar) view.findViewById(R.id.toolbar);

                toolbar.setTitle("  Add Language !");
                toolbar.setTitleTextColor(Color.WHITE);
                toolbar.setLogo(R.mipmap.language);

                Button add_language = (Button) view.findViewById(R.id.enter_language);

                add_language.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        arr.add(new Language_data_comes_here( R.mipmap.language , language.getText().toString()));


                        arrayAdapter.notifyDataSetChanged();
                        builder.setCancelable(true);
                        language.setText("");

                        getListCount();
                    }
                });

                builder.setCancelable(true);
                builder.setView(view);

                AlertDialog alertDialog = builder.create() ;
                alertDialog.show();
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                arr.remove(position);
                arrayAdapter.notifyDataSetChanged();
                getListCount();
                return false;
            }
        }) ;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.button_for_toolbar, menu);
        return true ;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.save_information){

            list = new ArrayList();

            for (int i =0 ; i < arrayAdapter.getCount() ; i++){

                Language_data_comes_here l = arrayAdapter.getItem(i);
                list.add(l);

            }

            Intent i = new Intent (Language_Activity.this , Selecting_Activity.class);
            i.putExtra("List" , list);
            startActivity(i);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {

        AlertDialog.Builder builder  = new AlertDialog.Builder(Language_Activity.this);

        builder.setTitle("Attention !");
        builder.setCancelable(true);
        builder.setMessage("Please Click on the SAVE button at the top right corner ! ");
        builder.setPositiveButton("close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog alertDialog = builder.create() ;
        alertDialog.show();
    }

    public void getListCount () {

        int count = listView.getAdapter().getCount();
        TextView t = (TextView) findViewById(R.id.b);
        t.setText(String.valueOf(count));
    }

    public void gettingListViewElements () {

        int count = listView.getAdapter().getCount();

        switch (count){

            case 1 :

                Language_data_comes_here getValue = arr.get(0);
                String getFirstValue = String.valueOf(getValue);

                break ;
            case 2 :
                break ;
            case 3 :
                break ;
            case 4 :
                break ;
            case 5 :
                break ;
            case 6 :
                break ;
            case 7 :
                break ;
            case 8 :
                break ;
            case 9 :
                break ;
            case 10 :
                break ;

            default:
                break ;
        }    
    }


    private void getting_data_from_fields_and_sending_on_click_listner() {
        gettingListViewElements();
    }

}

语言适配器活动

package com.example.wildstone.cv_maker;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Custom_Adapter_for_Language extends ArrayAdapter<Language_data_comes_here>
{
    Context context ;;

    public Custom_Adapter_for_Language(Context context, ArrayList<Language_data_comes_here> array) {
        super(context,0 , array);

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View v = convertView ;

        if (v == null){

            v = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_view ,parent ,false);

        }

        Language_data_comes_here language_data_comes_here = getItem(position);

        ImageView imageView = (ImageView) v.findViewById(R.id.custom_image_display_list_item);
        TextView textView = (TextView) v.findViewById(R.id.custom_text_display_list_item);

        textView.setText(language_data_comes_here.getLanguage());
        imageView.setImageResource(R.mipmap.language);

return v ;
    }
}

语言数据

package com.example.wildstone.cv_maker;

/**
 * Created by wildstone on 08/02/2018.
 */

public class Language_data_comes_here {

    private String data ;
    private int image ;

    Language_data_comes_here(int image , String data){

        this.data = data ;
    }

    public int getImage() {
        return image;
    }

    public String getLanguage() {
        return data;
    }
}

1 个答案:

答案 0 :(得分:1)

不知怎的,我通过

解决了这个问题
    Language_data_comes_here d = (Language_data_comes_here) listView.getAdapter().getItem(0);