2016-07-28 19 views
0

你好我会在点击保存对话框后在我的活动中设置一个TextView。 我会解释。 在我的活动我有一个ListView:在点击保存对话框时修改ListView上的TextView

<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" 
    /> 

当我点击列表项,我打开对话框,然后将数据传递给对话框:

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" /> 

编辑
在我AdapterJsonArray延伸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; 
} 

}

但是如何设置的ListView元素 “选择”,并与EditText上设置的TextView小时对话编译?

+0

可以显示AdapterJsonArray类的内容吗? – Andolasoft

+0

对不起,我会编辑 – LorenzoBerti

回答

0

我找到了一个解决方案,但是我在应用程序开发方面的一点经验,我不知道它是否是最好的解决方案。 我发现说一个线程: (链接线:Android ListView Refresh Single Row

由于罗曼盖伊的谷歌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; 
    } 

假设目标是适配器的一个项目。

所以,我通过我的ListView的对话框

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

希望这有助于有人说,没有经验和我一样。