0

Hello I would set a TextView in my activity after click save on dialog. I'll explain. In my Activity I have a ListView:

<ListView
        android:id="@+id/activityList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="15dp" />

And I have a "row" layout for this 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"
    />

When I click on List Item, I open dialog and pass data to 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();
}

When I open my dialog I have a editText:

<EditText
    android:id="@+id/hourUser"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

EDIT
In my AdapterJsonArray that extends BaseAdapter:

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

}

But How can I set element of listView "selected" and set TextView hours with EditText compiled in dialog?

LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89

1 Answers1

0

I'have found a solution, but with my little experience in the app development, I don't know if it's a best solution. I've found a thread that say: (link thread: Android ListView Refresh Single Row)

As Romain Guy explained a while back during the Google I/O session, the most efficient way to only update one view in a list view is something like the following (this one update the whole view data):

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

Assuming target is one item of the adapter.

And so, I've passed my ListView on dialog

private void openDialogReceipt(final JSONObject activity, final ListView listView) throws JSONException { ... }

And in Positivebutton listener:

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

hope this help someone that have little experience Like me.

Community
  • 1
  • 1
LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89