Android Baseadapter TextEdit不能正常工作

时间:2015-09-21 20:01:40

标签: android android-edittext baseadapter

我有一些奇怪的问题,我真的希望在这里找到帮助。

我有一个ListView使用Baseadapter和自定义单元格。我的单元格拥有ImageView和EditText。

我希望用户在EditText上输入评论并根据需要上传图片。但现在问题是:如果我想将单元格上传到服务器,我什么都得不到。我无法读取TextEdit字段中的信息,我不知道为什么会发生这种情况。

这是我的baseadapter的代码:

package com.example.other;

import java.util.List;

import com.example.decidenow.R;
import com.example.item.AnswerItem;

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


public class AnswerItemAdapter extends BaseAdapter {

private Context context;
protected List<AnswerItem> listCars;
private LayoutInflater inflater;

public AnswerItemAdapter(Context context, List<AnswerItem> listCars ) {
    this.listCars = listCars;
    this.inflater = LayoutInflater.from(context);
    this.context = context;
}

public int getCount() {
    return listCars.size();
}

public AnswerItem getItem(int position) {
    return listCars.get(position);
}

public long getItemId(int position) {
    return listCars.get(position).getID();
}

public View getView(int position, View convertView, ViewGroup parent) {
    final AnswerItem car = listCars.get(position);
    ViewHolder holder;

    if (convertView == null) {

        holder = new ViewHolder();
        convertView = this.inflater.inflate(R.layout.new_poll_item,
                parent, false);

        holder.imageview = (ImageView) convertView
                .findViewById(R.id.item_image);
        holder.answer = (EditText) convertView
                .findViewById(R.id.item_answer);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    car.setAnswer(holder.answer);
    car.setImageView(holder.imageview);

    holder.imageview.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            Activity origin = (Activity) context;
            origin.startActivityForResult(Intent.createChooser(intent, "uploading image"), car.getID());
        }
    });
    return convertView;
}

private class ViewHolder {
    ImageView imageview;
    EditText answer;
}
}

我的自定义单元格:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/layout_answer"
android:padding="10dp"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:id="@+id/item_comment"
    android:background="@color/main"
    android:orientation="horizontal">
    <TextView
        android:text="@string/answer_box"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ecf0f1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_answer"
    android:hint="@string/answer" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:layout_weight="3.34">

    <ImageView
        android:contentDescription="@string/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_image"
        android:src="@drawable/insert_photo"
        android:layout_gravity="center_horizontal"
        android:adjustViewBounds="true"
        android:maxWidth="80dp"
        android:maxHeight="80dp" />

</LinearLayout>
</LinearLayout>

AnswerItem存储细胞:

package com.example.item;

import android.widget.EditText;
import android.widget.ImageView;

public class AnswerItem {
private EditText answer;
private ImageView view;
private int ID;

public AnswerItem(int i) {
    ID = i;
}

public void setID(int i) {
    ID = i;
}

public int getID() {
    return ID;
}

public ImageView getImageView() {
    return view;
}

public EditText getAnswer() {
    return answer;
}

public void setImageView(ImageView v) {
    view = v;
}

public void setAnswer(EditText a) {
    answer = a;
}

}

这些是我主要课程(活动)的一部分:

...
ListView list_poll_item;
AnswerItemAdapter adapter;
ArrayList<AnswerItem> arrayPolls = new ArrayList<>();
list_poll_item   = (ListView) findViewById(R.id.list_answer);
list_poll_item.setAdapter(adapter);
...
arrayPolls.get(i).getAnswer().getText().toString()

不,我没有任何错误。我只得到一个空的EditText字符串。

我真的希望你能帮我这个问题很烦人。 提前谢谢。

编辑: 更新

我的适配器现在看起来像这样:

package com.example.other;

import java.util.List;

import com.example.decidenow.R;
import com.example.item.AnswerItem;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;


public class AnswerItemAdapter extends BaseAdapter {

private Context context;
protected List<AnswerItem> listCars;
private LayoutInflater inflater;

public AnswerItemAdapter(Context context, List<AnswerItem> listCars ) {
    this.listCars = listCars;
    this.inflater = LayoutInflater.from(context);
    this.context = context;
}

public int getCount() {
    return listCars.size();
}

public AnswerItem getItem(int position) {
    return listCars.get(position);
}

public long getItemId(int position) {
    return listCars.get(position).getID();
}

public View getView(int position, View convertView, ViewGroup parent)             {
    final AnswerItem car = listCars.get(position);
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = this.inflater.inflate(R.layout.new_poll_item,
                parent, false);

        holder.imageview = (ImageView) convertView
                .findViewById(R.id.item_image);
        holder.answer = (EditText) convertView
                .findViewById(R.id.item_answer);

        holder.myTextWatcher = new MyTextWatcher(holder.answer);
        holder.answer.addTextChangedListener(holder.myTextWatcher);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.answer.setText(car.getComment());
    holder.myTextWatcher.setCar(car);

    car.setImageView(holder.imageview);

    holder.imageview.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            Activity origin = (Activity) context;
            origin.startActivityForResult(Intent.createChooser(intent, "uploading image"), car.getID());
        }
    });
    return convertView;
}

private class ViewHolder {
    EditText answer;
    ImageView imageview;
    MyTextWatcher myTextWatcher;
}

private class MyTextWatcher implements TextWatcher {

    private AnswerItem car;
    private EditText mEditText;

    public MyTextWatcher(EditText mEditText) {
        this.mEditText = mEditText;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (car != null) {
            car.setComment(mEditText.getText().toString());
        }
    }

    public void setCar(AnswerItem car) {
        this.car = car;
    }
}
}

1 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您假设每行都有block in multi' /home/deployer/apps/wripl-capture/shared/bundle/ruby/2.2.0/gems/redis-3.2.1/lib/redis.rb:37:in 。这是一个不正确的假设。您可能只有4-5个EditText个实例,这些实例的所有行都具有相同的值。

您需要做的是在EditText添加其他字段和方法:

AnswerItem

然后在private String comment; public String getComment(){ return comment; } public void setComment(String comment) { return this.comment = comment; }

getView()

以下是您需要的其他课程:

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = this.inflater.inflate(R.layout.new_poll_item,
                    parent, false);

            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.item_image);
            holder.answer = (EditText) convertView
                    .findViewById(R.id.item_answer);

            holder.myTextWatcher = new MyTextWatcher(holder.answer);
            holder.answer.addTextChangedListener(holder.myTextWatcher);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // The order needs to be exactly this way because setText will call
        // AfterTextChanged on the previous row
        holder.myTextWatcher.setCar(car);
        holder.answer.setText(car.getComment());

        car.setImageView(holder.imageview);

        holder.imageview.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);

                Activity origin = (Activity) context;
                origin.startActivityForResult(Intent.createChooser(intent, "uploading image"), car.getID());
            }
        });
        return convertView;
    }
}