gpt4 book ai didi

java - 从 ListView 获取项目值而不调用 onItemClickListner

转载 作者:行者123 更新时间:2023-12-02 11:37:36 25 4
gpt4 key购买 nike

我想做的事情:我想获取所有 List View 项目并将其发送到其他 Activity 或获取 ListView 项而不声明 onItemClickListner 当我获取列表项时,我希望它们存储到另一个列表中,并将该列表发送到其他 Activity ,我可以在其中获取所有列表项列表中的项目

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

我在做什么

 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);
}

语言 Activity

   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();
}

}

语言适配器 Activity

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;
}
}

最佳答案

不知何故,我解决了这个问题

    Language_data_comes_here d = (Language_data_comes_here) listView.getAdapter().getItem(0);

关于java - 从 ListView 获取项目值而不调用 onItemClickListner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com