在ListView

时间:2016-05-26 01:15:54

标签: android listview

我是Android的新手,我已经阅读了一些关于自定义ListView的tutos,我正在做一个应用程序来操纵数据我用这个小应用程序恢复了我的问题。屏幕是这样的:enter image description here

在这里有一个列表视图,每个项目都有一个TextView和一个EditText,最后一个控件是用户输入我需要的金额,当用户按下按钮清除首先保存在arraylist中的金额信息,然后清除领域。我怎么接近这个 我的代码:
activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rimacy.preguntalistview.MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView1"
        android:layout_weight="1"
        android:focusable="false"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:id="@+id/btnClear"/>
</LinearLayout>

listview_item.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">

    <TextView
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:text="item"
        android:id="@+id/tv1" />

    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:hint="Input amount"
        android:id="@+id/edit1"
        android:layout_weight="1"
        android:inputType="number"/>
</LinearLayout>

MainActivity.java

package com.rimacy.preguntalistview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<Inventory> data = new ArrayList<>();
        data.add(new Inventory("candies",0));
        data.add(new Inventory("milk",0));
        data.add(new Inventory("soda",0));
        data.add(new Inventory("paper",0));
        data.add(new Inventory("bags",0));
        //... the items are unknow in run time

        MyAdapter adp = new MyAdapter(this,R.layout.listview_item,data);
        ListView listView = (ListView)findViewById(R.id.listView1);
        listView.setAdapter(adp);

        Button btnClear = (Button) findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // set de value of each EditText to "amount" field in each object in the ArrayList "data"
                // perform a clear of all EditText in the listview
                // set the focus in the firs EditText
            }
        });


    }
    class Inventory{
        String name;
        Integer amount;

        public Inventory(String name, Integer amount) {
            this.name = name;
            this.amount = amount;
        }

        public String getName() {
            return name;
        }
    }
    class MyAdapter extends ArrayAdapter<Inventory>{
        private ArrayList<Inventory> items;
        public MyAdapter(Context context, int resource, ArrayList<Inventory> items) {
            super(context, resource, items);
            this.items = items;
        }
        public View getView(int position, final View convertView, ViewGroup parent) {
            View v=convertView;
            TextView editText;
            if (v == null){
                LayoutInflater inflater =  LayoutInflater.from(getContext());
                v = inflater.inflate(R.layout.listview_item, null);
            }
            editText = (TextView)v.findViewById(R.id.tv1);
            editText.setText(items.get(position).getName());
            return v;
        }
    }
}

更新
我这样做

**

package com.rimacy.preguntalistview;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    ArrayList<Inventory> data = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        data.add(new Inventory("candies",0));
        data.add(new Inventory("milk",0));
        data.add(new Inventory("soda",0));
        data.add(new Inventory("paper",0));
        data.add(new Inventory("bags",0));
        //... the items are unknow in run time
        final MyAdapter adp = new MyAdapter(this,R.layout.listview_item,data);
        final ListView listView = (ListView)findViewById(R.id.listView1);
        listView.setAdapter(adp);
        adp.notifyDataSetChanged();
        Button btnClear = (Button) findViewById(R.id.btnClear);
        if (btnClear!= null){
            btnClear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // set de value of each EditText to "amount" field in each object in the ArrayList "data"
                    // perform a clear of all EditText in the listview
                    // set the focus in the firs EditText
                    for (Inventory i:data){
                        i.getTxt().setText("");
                    }
                    adp.notifyDataSetChanged();
                }
            });
        }
    }
    class Inventory{
        String name;
        Integer amount;
        EditText txt;
        public void setName(String name) {
            this.name = name;
        }
        public EditText getTxt() {
            return txt;
        }
        public void setTxt(EditText txt) {
            this.txt = txt;
        }
        public Inventory(String name, Integer amount) {
            this.name = name;
            this.amount = amount;
        }
        public String getName() {
            return name;
        }
    }
    class MyAdapter extends ArrayAdapter<Inventory>{
        private ArrayList<Inventory> items;
        public MyAdapter(Context context, int resource, ArrayList<Inventory> items1) {
            super(context, resource, items1);
            items = items1;
        }
        public View getView(int position, final View convertView, ViewGroup parent) {
            View v=convertView;
            TextView editText;
            EditText txt;
            if (v == null){
                LayoutInflater inflater =  LayoutInflater.from(getContext());
                v = inflater.inflate(R.layout.listview_item, null);
            }
            txt = (EditText)v.findViewById(R.id.edit1);
            editText = (TextView)v.findViewById(R.id.tv1);
            items.get(position).setTxt(txt);
            editText.setText(items.get(position).getName());
            return v;
        }
    }
}

在Inventory类中添加了一个新成员来存储在getView事件中分配的editText的引用,并使用getTxt方法调用此引用并使用空字符串调用setText方法,但它仅在我按下按钮时有效两次为什么??

0 个答案:

没有答案