2016-07-28 17 views
0

Hallo Ich würde eine TextView in meiner Aktivität nach Klicken auf Speichern im Dialogfeld festlegen. Ich werde es erklären. In meiner Tätigkeit habe ich ein Listview:Ändern Sie TextView in ListView, wenn Sie auf Speichern klicken

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

Und ich habe eine "Reihe" Layout für diese 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" 
    /> 

Wenn ich auf List Item klicken, I-Dialog öffnen und geben Daten an 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(); 
} 

Wenn ich meinen Dialog zu öffnen habe ich eine editText:

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

EDIT
In meinem AdapterJsonArray die BaseAdapter erweitert:

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

}

Aber wie kann ich Element Listview "ausgewählt" und setzen Textview Stunden mit EditText im Dialog zusammengestellt eingestellt?

+0

können Sie den Inhalt der AdapterJsonArray-Klasse anzeigen? – Andolasoft

+0

Ja sorry, ich werde bearbeiten – LorenzoBerti

Antwort

0

Ich habe eine Lösung gefunden, aber mit meiner kleinen Erfahrung in der App-Entwicklung weiß ich nicht, ob es eine beste Lösung ist. Ich habe einen Thread gefunden, die sagen: (link thread: Android ListView Refresh Single Row)

Als Romain Guy eine Weile zurück, während der Google I/O-Sitzung, der effizienteste Weg, nur erklärt zu einer Ansicht in einer Liste zu aktualisieren Ansicht ist so etwas wie das folgende (dieses Update der ganze Sicht Daten):

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

Ziel Angenommen ist ein Element des Adapters.

Und so, ich habe meine Listview weitergegeben Dialog

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

Und in Positivebutton Zuhörer:

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

Hoffnung diese Hilfe jemand, wie ich wenig Erfahrung haben.

Verwandte Themen