我使用放在hasmap中的数据库填充了我的ListView
。 ListView
中的每一行(hashmap)都有EditText
。我的问题是我不知道如何获得hasmap中EditText
的值。
以下是代码:
selectedFileNames = new String[listSelectedFileNames.size()];
for (int i = 0; i < listSelectedFileNames.size(); i++) {
selectedFileNames[i] = listSelectedFileNames.get(i);
}
selectedFileUri = new String[listSelectedFileUri.size()];
for (int i = 0; i < listSelectedFileUri.size(); i++) {
selectedFileUri[i] = listSelectedFileUri.get(i);
}
String myDate = tv_Date.getText().toString();
mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < listSelectedFileNames.size(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(FILE_NAME, selectedFileNames[i]);
map.put(DESC,"");
map.put(UPLOADED_BY, "User");
map.put(DATE_UPLOADED, myDate);
map.put(ACTION, "Delete");
map.put(ID, String.valueOf(i));
map.put(FILE_URI, selectedFileUri[i]);
mylist.add(map);
}
myCustomAdapterIreport = new CustomArrayAdapterIreport(getApplicationContext(), mylist, R.layout.attribute_selected_ireport_file,
new String[]{FILE_NAME, DESC, UPLOADED_BY, DATE_UPLOADED, ACTION, ID, FILE_URI},
new int[]{R.id.tv_iFile, R.id.txt_iDesc,R.id.tv_iUploadedBy,R.id.tv_iDateUploaded, R.id.tv_iAction,
R.id.tv_RowId, R.id.tv_iUri}, true);
lv_AttachedFileData.setAdapter(myCustomAdapterIreport);
这是我的适配器的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:orientation="horizontal" >
<LinearLayout
android:id="@+id/listHeader1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_iFile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="@color/black"
android:layout_weight="1"
android:gravity="center"
android:singleLine="true"
android:paddingRight="3dp"/>
<EditText
android:id="@+id/txt_iDesc"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_weight="1"
android:textSize="13sp"
android:textColor="@color/black"
android:singleLine="true"
android:paddingRight="3dp"
android:gravity="center"
android:background="@drawable/bg_ireporttxtbg"
android:ems="10"/>
<TextView
android:id="@+id/tv_iUploadedBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13sp"
android:textColor="@color/black"
android:gravity="center"
android:singleLine="true"/>
<TextView
android:id="@+id/tv_iDateUploaded"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13sp"
android:gravity="center"
android:clickable="true"
android:textColor="@color/black"/>
<TextView
android:id="@+id/tv_iAction"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13sp"
android:textColor="@color/blue"
android:gravity="center"
android:clickable="true"
android:text="Delete"
android:singleLine="true"/>
<TextView
android:id="@+id/tv_iUri"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textSize="13sp"
android:gravity="center"
android:clickable="true"
android:visibility="gone"
android:text="Uri" />
<TextView
android:id="@+id/tv_RowId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
请帮助或建议我解决此问题的任何示例..谢谢!以下是我的自定义适配器的代码
public CustomArrayAdapterIreport(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from,
int[] to, boolean chosenValues) {
super(context, data, resource, from, to);
this.context = context;
mData = data;
this.unfilteredValues = mData;
this.resource = resource;
this.from = from;
this.to = to;
this.arraylistAttach = new ArrayList<String>();
this.arraylistAttachId = new ArrayList<String>();
this.chosenValues = chosenValues;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rowView = null;
try{
rowView = inflater.inflate(resource, null, true);
textViewTitle = (TextView) rowView.findViewById(to[0]);
txt_Desc = (EditText) rowView.findViewById(to[1]);
tv_CreatedBy = (TextView) rowView.findViewById(to[2]);
tv_DateCreated= (TextView) rowView.findViewById(to[3]);
final TextView tv_Action = (TextView) rowView.findViewById(to[4]);
final TextView tv_rowId = (TextView) rowView.findViewById(to[5]);
tv_Uri = (TextView)rowView.findViewById(to[6]);
final String FileKey = from[0];
String DescKey = from[1];
String UploadedByKey = from[2];
String DateUploadKey = from[3];
String ActionKey = from[4];
final String idKey = from[5];
String FileUri = from[6];
final String FileName = unfilteredValues.get(position).get(FileKey).toString();
String Desc = unfilteredValues.get(position).get(DescKey).toString();
String UploadedBy = unfilteredValues.get(position).get(UploadedByKey).toString();
String DateUpload = unfilteredValues.get(position).get(DateUploadKey).toString();
String Action = unfilteredValues.get(position).get(ActionKey).toString();
String AttachId = unfilteredValues.get(position).get(idKey).toString();
String FileNameUri = unfilteredValues.get(position).get(FileUri).toString();
textViewTitle.setText(FileName);
txt_Desc.setText(Desc);
tv_CreatedBy.setText(UploadedBy);
tv_DateCreated.setText(DateUpload);
tv_Action.setText(Action);
tv_rowId.setText(AttachId);
tv_Uri.setText(FileNameUri);
});
}catch (Exception e){
e.printStackTrace();
}catch (OutOfMemoryError E){
E.printStackTrace();
}
return rowView;
}
public ArrayList<String> getArrayListConsumer() {
return this.arraylistAttach;
}
public ArrayList<String> getArrayListConsumerId() {
return this.arraylistAttachId;
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new SimpleFilter();
}
return mFilter;
}
public int getCount() {
return unfilteredValues.size();
}
private class SimpleFilter extends Filter {
@SuppressWarnings("unchecked")
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
String prefixString = null == prefix ? null : prefix.toString().toLowerCase();
ArrayList<HashMap<String, String>> unfilteredValues;
if (null != prefixString && prefixString.length() > 0) {
synchronized (mData) {
unfilteredValues = (ArrayList<HashMap<String, String>>) mData.clone();
}
for (int i = unfilteredValues.size() - 1; i >= 0; --i) {
HashMap<String, String> h = unfilteredValues.get(i);
String str = (String)h.get(from[0]).toString();
if (!str.toLowerCase().startsWith(prefixString)) {
unfilteredValues.remove(i);
}
}
//Log.i(Constants.TAG, String.valueOf(unfilteredValues.size()));
results.values = unfilteredValues;
results.count = unfilteredValues.size();
} else {
synchronized (mData) {
results.values = mData;
results.count = mData.size();
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
unfilteredValues = (ArrayList<HashMap<String, String>>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
首先,你必须在java类中启动textView:
EditText txt_iDesc = (EditText)findViewById(R.id.txt_iDesc);
然后将变量设置为侦听文本中的更改,并在检测到文本更改时更改变量
txt_iDesc.setOnTextChangedListener(this);
我也是新手,但希望这会有所帮助
答案 1 :(得分:0)
通过做一些研究,我终于解决了我的问题。我做的是,我得到ListView的索引,并获得可以在该索引上找到的EditText的值。下面的代码显示了我如何保存HashMap中的所有值,包括EditText的值。
SQLiteDatabase db = databaseHandler.getWritableDatabase();
db.beginTransaction();
int counter = 0;
for(HashMap<String, String> map : mylist)
{
ContentValues cv = new ContentValues();
String string = "";
for (int i = 0; i < lv_AttachedFileData.getCount(); i++)
{
if (i == counter)
{
View view=lv_AttachedFileData.getChildAt(i);
EditText editText=(EditText) view.findViewById(R.id.txt_iDesc);
string = editText.getText().toString();
cv.put(Constants.ATTACH_REPORTCODE, ReportCode);
cv.put(Constants.ATTACH_FILENAME, map.get(FILE_NAME));
cv.put(Constants.ATTACH_DESCRIPTION, string);
cv.put(Constants.ATTACH_FILELOCATION, map.get(FILE_URI));
cv.put(Constants.ATTACH_CREATEDBY, map.get(UPLOADED_BY));
cv.put(Constants.ATTACH_DATECREATED, map.get(DATE_UPLOADED));
db.insert(Constants.TABLE_ATTACH, null, cv);
}
}
counter++;
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
答案 2 :(得分:-1)
看看这里 -
public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (EditText) row.findViewById(R.id.textView1);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.textName.setText(user.getName());
System.out.println(user.getName());
return row;
}
static class UserHolder {
EditText textName;
}
}
完整代码的链接here-