我想获得的所有的EditText元素是我的ListView里面present的所有值。 这是我的code:
最终的ListView editorList =(ListView控件)findViewById(R.id.editorList);
最后EditorAdapter适配器=新EditorAdapter(上下文数据);
editorList.setAdapter(适配器);
按钮commitButton =(按钮)findViewById(R.id.commit_button);
commitButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
尝试{
清单//System.out.println("Size:+ editorList.getChildCount());
的for(int i = 0; I< data.size();我++){
的System.out.println(列出的大小:+ data.size());
的EditText值= adapter.getItem(ⅰ);
字符串为PropertyValue = value.getText()的toString()。
的System.out.println(为PropertyValue:+为PropertyValue);
}
}赶上(例外五){
e.printStackTrace();
}
}
});
这是我的适配器类:
包in.omerjerk preferenceseditor。
公共类EditorAdapter扩展了BaseAdapter {
上下文语境;
ArrayList的< HashMap的<字符串,字符串>>数据;
的EditText [] mHolders;
公共EditorAdapter(上下文的背景下,ArrayList的< HashMap的<字符串,字符串>>数据){
this.context =背景;
this.data =数据;
的System.out.println(没有在节点的项目。+ data.size());
mHolders =新的EditText [data.size()];
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回data.size();
}
@覆盖
公众的EditText的getItem(INT POS){
// TODO自动生成方法存根
返回mHolders [POS]
}
@覆盖
众长getItemId(INT POS){
// TODO自动生成方法存根
返回POS机;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
ViewHolder持有人;
如果(convertView == NULL){
的System.out.println(转换的观点是空);
持有人=新ViewHolder();
convertView = LayoutInflater.from(上下文).inflate(R.layout.row_edit_string,空,假);
holder.editPropertyValue =(EditText上)convertView.findViewById(R.id.propertyValue);
holder.propertyName =(TextView中)convertView.findViewById(R.id.propertyName);
holder.propertyName.setText(data.get(位置)获得(propertyName的));
holder.editPropertyValue.setText(data.get(位置)获得(propertyName的));
convertView.setTag(保持器);
}其他{
的System.out.println(转换VIEW NOT NULL);
支架=(ViewHolder)convertView.getTag();
holder.propertyName.setText(data.get(位置)获得(propertyName的));
holder.editPropertyValue.setText(data.get(位置)获得(propertyName的));
convertView.setTag(保持器);
mHolders [位置] =新的EditText(上下文);
mHolders [位置] = holder.editPropertyValue;
}
返回convertView;
}
}
我收到我的输出奇怪的错误。所述mHolders阵列只包含高达6-7元件和这些元件重复整个阵列在。我能够得到的EditText的值,但该错误是不正确的。
解决方案 //尝试这种方式修改为每UR要求我有HashMap中的用户的ArrayList insted的节点列表中
** activity_main.xml **
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:填充=5DP
机器人:重力=中心
机器人:方向=垂直>
<的ListView
机器人:ID =@ + ID / LST
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =1
机器人:cacheColorHint =#00000000
机器人:分隔=@空
机器人:dividerHeight =0dp
机器人:smoothScrollbar =真/>
<按钮
机器人:ID =@ + ID / getAllValue
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:文本=获取全部价值/>
< / LinearLayout中>
** row_edit_string.xml **
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:填充=5DP
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / propertyName的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
<的EditText
机器人:ID =@ + ID / editPropertyValue
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP/>
< / LinearLayout中>
**主要活动**
包com.example.MyTest;
进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.inputmethod.InputMethodManager;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;
进口的java.util.ArrayList;
进口的java.util.HashMap;
公共类MainActivity延伸活动{
私人的ListView LST;
私人按钮getAllValue;
私人EditorAdapter适配器;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
LST =(ListView控件)findViewById(R.id.lst);
getAllValue =(按钮)findViewById(R.id.getAllValue);
ArrayList的< HashMap的<字符串,字符串>>数据=新的ArrayList< HashMap的<字符串,字符串>>();
HashMap的<字符串,字符串> ROW1 =新的HashMap<字符串,字符串>();
row1.put(名,姓);
row1.put(值,FirstValue);
data.add(ROW1);
HashMap的<字符串,字符串> ROW2 =新的HashMap<字符串,字符串>();
row2.put(姓名,SecondName);
row2.put(值,SecondValue);
data.add(ROW2);
HashMap的<字符串,字符串> ROW3 =新的HashMap<字符串,字符串>();
row3.put(姓名,ThirdName);
row3.put(值,ThirdValue);
data.add(ROW3);
HashMap的<字符串,字符串> ROW4 =新的HashMap<字符串,字符串>();
row4.put(姓名,FourthName);
row4.put(值,FourthValue);
data.add(ROW4);
HashMap的<字符串,字符串> row5 =新的HashMap<字符串,字符串>();
row5.put(姓名,FifthName);
row5.put(值,FifthValue);
data.add(row5);
适配器=新EditorAdapter(这个数据);
lst.setAdapter(适配器);
getAllValue.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图查看){
字符串allValues =;
ArrayList的<字符串> valueList =新的ArrayList<字符串>();
的for(int i = 0; I< adapter.getCount();我++){
allValues + =((HashMap的<字符串,字符串>)adapter.getItem(I))。获得(价值)+,;
valueList.add(((HashMap的<字符串,字符串>)adapter.getItem(I))获得(价值));
}
//使用这个valueList根据乌拉圭回合的要求
allValues = allValues.substring(0,allValues.length() - 1);
Toast.makeText(MainActivity.this,allValues,Toast.LENGTH_LONG).show();
}
});
}
}
类EditorAdapter扩展了BaseAdapter {
上下文语境;
ArrayList的< HashMap的<字符串,字符串>>数据;
公共EditorAdapter(上下文的背景下,ArrayList的< HashMap的<字符串,字符串>>数据){
this.context =背景;
this.data =数据;
}
@覆盖
公众诠释getCount将(){
返回data.size();
}
@覆盖
公共对象的getItem(INT POS){
返回data.get(POS);
}
@覆盖
众长getItemId(INT POS){
返回POS机;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
持有人=新ViewHolder();
convertView = LayoutInflater.from(上下文).inflate(R.layout.row_edit_string,空,假);
holder.editPropertyValue =(EditText上)convertView.findViewById(R.id.editPropertyValue);
holder.propertyName =(TextView中)convertView.findViewById(R.id.propertyName);
convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
holder.propertyName.setText(data.get(位置)获得(名字));
holder.editPropertyValue.setText(data.get(位置)获得(价值));
convertView.setTag(保持器);
返回convertView;
}
类ViewHolder {
的EditText editPropertyValue;
TextView的propertyName的;
}
}
I want to get all the values of all the EditText elements that are present inside my ListView. This is my code :
final ListView editorList = (ListView) findViewById(R.id.editorList);
final EditorAdapter adapter = new EditorAdapter(context, data);
editorList.setAdapter(adapter);
Button commitButton = (Button) findViewById(R.id.commit_button);
commitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//System.out.println("Size of List : " + editorList.getChildCount());
for(int i =0;i< data.size() ;i++){
System.out.println("Size of List : " + data.size());
EditText value = adapter.getItem(i);
String propertyValue = value.getText().toString();
System.out.println("PropertyValue : " + propertyValue);
}
} catch (Exception e){
e.printStackTrace();
}
}
});
This is my Adapter class :
package in.omerjerk.preferenceseditor;
public class EditorAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String,String>> data;
EditText[] mHolders;
public EditorAdapter(Context context, ArrayList<HashMap<String,String>> data){
this.context = context;
this.data = data;
System.out.println("No . of items in nodes"+data.size());
mHolders = new EditText[data.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public EditText getItem(int pos) {
// TODO Auto-generated method stub
return mHolders[pos];
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null){
System.out.println("CONVERT VIEW IS NULL");
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row_edit_string,null,false);
holder.editPropertyValue = (EditText) convertView.findViewById(R.id.propertyValue);
holder.propertyName = (TextView) convertView.findViewById(R.id.propertyName);
holder.propertyName.setText(data.get(position).get("propertyName"));
holder.editPropertyValue.setText(data.get(position).get("propertyName"));
convertView.setTag(holder);
}else{
System.out.println("CONVERT VIEW NOT NULL");
holder = (ViewHolder) convertView.getTag();
holder.propertyName.setText(data.get(position).get("propertyName"));
holder.editPropertyValue.setText(data.get(position).get("propertyName"));
convertView.setTag(holder);
mHolders[position] = new EditText(context);
mHolders[position] = holder.editPropertyValue;
}
return convertView;
}
}
I'm getting strange error in my output. The mHolders array only contain upto 6-7 elements and these elements are repeated in the entire array. I'm able to get the values of EditText but the error is not correct.
解决方案// try this way and modify as per ur requirement i have user ArrayList of HashMap insted of NodeList
**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="center"
android:orientation="vertical">
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:divider="@null"
android:dividerHeight="0dp"
android:smoothScrollbar="true" />
<Button
android:id="@+id/getAllValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Get All Value"/>
</LinearLayout>
**row_edit_string.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/propertyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editPropertyValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
**MainActivity**
package com.example.MyTest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
private ListView lst;
private Button getAllValue;
private EditorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lst=(ListView)findViewById(R.id.lst);
getAllValue=(Button)findViewById(R.id.getAllValue);
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
HashMap<String,String> row1 = new HashMap<String, String>();
row1.put("name","FirstName");
row1.put("value","FirstValue");
data.add(row1);
HashMap<String,String> row2 = new HashMap<String, String>();
row2.put("name","SecondName");
row2.put("value","SecondValue");
data.add(row2);
HashMap<String,String> row3 = new HashMap<String, String>();
row3.put("name","ThirdName");
row3.put("value","ThirdValue");
data.add(row3);
HashMap<String,String> row4 = new HashMap<String, String>();
row4.put("name","FourthName");
row4.put("value","FourthValue");
data.add(row4);
HashMap<String,String> row5 = new HashMap<String, String>();
row5.put("name","FifthName");
row5.put("value","FifthValue");
data.add(row5);
adapter = new EditorAdapter(this,data);
lst.setAdapter(adapter);
getAllValue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String allValues="";
ArrayList<String> valueList = new ArrayList<String>();
for (int i=0;i<adapter.getCount();i++){
allValues +=((HashMap<String,String>)adapter.getItem(i)).get("value")+ ",";
valueList.add(((HashMap<String,String>)adapter.getItem(i)).get("value"));
}
// use this valueList as per ur requirement
allValues = allValues.substring(0,allValues.length()-1);
Toast.makeText(MainActivity.this,allValues,Toast.LENGTH_LONG).show();
}
});
}
}
class EditorAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String,String>> data;
public EditorAdapter(Context context, ArrayList<HashMap<String,String>> data){
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int pos) {
return data.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row_edit_string,null,false);
holder.editPropertyValue = (EditText) convertView.findViewById(R.id.editPropertyValue);
holder.propertyName = (TextView) convertView.findViewById(R.id.propertyName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.propertyName.setText(data.get(position).get("name"));
holder.editPropertyValue.setText(data.get(position).get("value"));
convertView.setTag(holder);
return convertView;
}
class ViewHolder {
EditText editPropertyValue;
TextView propertyName;
}
}