ListView中的Android EditText无法编辑

时间:2014-04-01 10:13:11

标签: android xml listview android-listview android-edittext

我无法编辑EditText。如何实现? 并且还想知道如何从所有Edittext获取数据。我在这里给我的课程和xml文件

MainActivity.java

package com.example.listviewdynamic;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

Button submit;
public static final String[] commodity = new String[] { "1", "2",
        "3", "4" };
public static final String[] quantity = new String[] { "50", "40", "30",
        "15", };
public static final String[] rate = new String[] { "2", "5", "3", "121", };
public static final String[] issue_qty = new String[] { "0.0", "0.0",
        "0.0", "0.0", };

ListView listView;
List<RowItem> rowItems;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < commodity.length; i++) {
        RowItem item = new RowItem(commodity[i], quantity[i], rate[i],
                issue_qty[i]);
        rowItems.add(item);
    }
    submit = (Button) findViewById(R.id.submit);
    listView = (ListView) findViewById(R.id.list);
    listView.setItemsCanFocus(true);
    listView.setClickable(false);
    CustomListAdapter adapter = new CustomListAdapter(this, rowItems);
    listView.setAdapter(adapter);

    // How to get the data ?
    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Code To get data with id's from EditText
        }
    });

}

public void onItemSelected(AdapterView<?> listView, View view,
        int position, long id) {
    EditText yourEditText = (EditText) view.findViewById(R.id.issue_qty);
    listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
    yourEditText.requestFocus();
}

public void onNothingSelected(AdapterView<?> listView) {
    listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}

}

CustomListAdapter.java

package com.example.listviewdynamic;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;

public CustomListAdapter(Context context, List<RowItem> items) {
    this.context = context;
    this.rowItems = items;
}

private class ViewHolder {

    TextView item;
    TextView quantity;
    TextView rate;
    EditText issue_quantity;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_items, null);
        holder = new ViewHolder();
        holder.quantity = (TextView) convertView
                .findViewById(R.id.quantity);
        holder.item = (TextView) convertView.findViewById(R.id.item);
        holder.rate = (TextView) convertView.findViewById(R.id.rate);
        holder.issue_quantity = (EditText) convertView
                .findViewById(R.id.issue_qty);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.quantity.setText(rowItem.getQuantity());
    holder.item.setText(rowItem.getItem());
    holder.rate.setText(rowItem.getRate());
//  holder.issue_quantity.setText(rowItem.getIssue_quantity());

    return convertView;
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}
}

RowItem.java

package com.example.listviewdynamic;

public class RowItem {

private String rate;
private String quantity;
private String item;
private String issue_quantity;

public RowItem(String item, String quantity, String rate, String issueQTY) {
    this.issue_quantity = issueQTY;
    this.item = item;
    this.quantity = quantity;
    this.rate = rate;
}

public String getRate() {
    return rate;
}

public void setRate(String rate) {
    this.rate = rate;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getItem() {
    return item;
}

public void setItem(String item) {
    this.item = item;
}

public String getIssue_quantity() {
    return issue_quantity;
}

public void setIssue_quantity(String issue_quantity) {
    this.issue_quantity = issue_quantity;
}

}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:text="Submit" >
</Button>

</LinearLayout>

list_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/item"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<TextView
    android:id="@+id/quantity"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<TextView
    android:id="@+id/rate"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<EditText
    android:id="@+id/issue_qty"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:paddingLeft="10dp"
    android:editable="true"
    android:paddingRight="10dp" />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

EditText不适合在ListView内使用。或者,您可以使用ScrollView并动态填充EditText