gpt4 book ai didi

Android Baseadapter TextEdit 不工作

转载 作者:行者123 更新时间:2023-11-30 01:53:11 27 4
gpt4 key购买 nike

这几天我遇到了一个奇怪的问题,我真的希望能在这里找到帮助。

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

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

这是我的基本适配器的代码:

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

}

这些是我的主要类(class)( Activity )的一些部分:

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

最佳答案

您的代码存在的问题是您假设每一行都有一个 EditText。这是一个不正确的假设。您可能只有 4-5 个 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;
}
}

这是您需要的其他类(class):

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

private class MyTextWatcher implements TextWatcher {

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

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

关于Android Baseadapter TextEdit 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703586/

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