您好,我会在单击“保存对话框”后在我的活动中设置TextView。 我会解释一下。 在我的Activity中,我有一个ListView:
<ListView
android:id="@+id/activityList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp" />
我有一个这个ListView的“行”布局:
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:padding="8dp"
/>
<TextView
android:id="@+id/hours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:padding="8dp"
/>
当我点击List Item时,我打开对话框并将数据传递给Dialog:
private void createListActivities(){
AdapterJsonArray adapterActivities = new AdapterJsonArray(ReceiptsActivity.this, datas);
ListView listView = (ListView)findViewById(R.id.activityList);
assert listView != null;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
JSONObject data = datas.getJSONObject(position);
openDialogReceipt(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
listView.setAdapter(adapterActivities);
}
private void openDialogReceipt(final JSONObject activity) throws JSONException {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = Activity.this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_receipt_activity, null);
alertDialog.setView(dialogView);
alertDialog.setTitle("Time");
alertDialog.setMessage(activity.getString("property"));
alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try {
/** get Text hour */
EditText hour = (EditText)dialogView.findViewById(R.id.hourUser);
assert hour != null;
// in this point I have to pass data to activity Item clicked in ListView
} catch (JSONException e) {
e.printStackTrace();
}
}
});
alertDialog.setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = alertDialog.create();
b.show();
}
当我打开对话框时,我有一个editText:
<EditText
android:id="@+id/hourUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
修改
在扩展BaseAdapter的 AdapterJsonArray 中:
public class AdapterJsonArray extends BaseAdapter implements ListAdapter {
private final Activity activity;
private final JSONArray jsonArray;
public AdapterJsonArray(Activity activity, JSONArray jsonArray) {
assert activity != null;
assert jsonArray != null;
this.jsonArray = jsonArray;
this.activity = activity;
}
@Override
public int getCount() {
return jsonArray.length();
}
@Override public JSONObject getItem(int position) {
return jsonArray.optJSONObject(position);
}
@Override
public long getItemId(int position) {
JSONObject jsonObject = getItem(position);
return jsonObject.optLong("id");
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v;
if (convertView == null) {
v = inflater.inflate(R.layout.list_view_activities, null);
} else {
v = convertView;
}
TextView description = (TextView) v.findViewById(R.id.text);
try {
description.setText(jsonArray.getJSONObject(position).getString("property"));
} catch (JSONException e) {
e.printStackTrace();
}
// questionable logic
return v;
}
}
但是如何设置listView的元素“selected”并使用在对话框中编译的EditText设置TextView小时?
答案 0 :(得分:0)
我已找到解决方案,但由于我在应用开发方面的经验不足,我不知道它是否是最佳解决方案。 我找到了一个说: (链接主题:Android ListView Refresh Single Row)
正如Romain Guy在Google I / O会话期间解释的那样,仅在列表视图中更新一个视图的最有效方法是以下内容(此更新整个视图数据):
ListView list = getListView();
int start = list.getFirstVisiblePosition();
for(int i=start, j=list.getLastVisiblePosition();i<=j;i++)
if(target==list.getItemAtPosition(i)){
View view = list.getChildAt(i-start);
list.getAdapter().getView(i, view, list);
break;
}
假设target是适配器的一个项目。
所以,我已经在对话框
上传递了我的ListViewprivate void openDialogReceipt(final JSONObject activity, final ListView listView) throws JSONException { ... }
在Positivebutton听众中:
alertDialog.setPositiveButton("Salva", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try {
/** get Text hour it's the textView that i would populate */
EditText hour = (EditText)dialogView.findViewById(R.id.hour);
assert hour != null;
int start = listView.getFirstVisiblePosition();
for(int i=start, j=listView.getLastVisiblePosition();i<=j;i++)
if(activity==listView.getItemAtPosition(i)){
View view = listView.getChildAt(i-start);
listView.getAdapter().getView(i, view, listView);
TextView hoursText = (TextView) view.findViewById(R.id.hours);
hoursText.setText("textToUpdate");
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
希望这能帮助那些没有经验的人,比如我。