我一直在尋找這個很長的時間,但沒有發現真正有用的......我有一個自定義視圖的佈局,佈局被加載。所以在我的init()中,我啓動了一個到我的服務器的TCP連接,它返回一個ArrayList。我希望ArrayList被傳遞給onDraw(),我使用它的值。我的問題是我可以調用一個方法將局部變量設置爲新值,但由於某種原因,onDraw()方法仍然使用未填充的空ArrayList。我認爲這段代碼更好地解釋了它......這是正確的做法還是我最初的接近錯誤?如何在1rst之後將變量傳遞給Custum視圖onDraw()
HistoryActivity:
public class HistoryActivity extends AppCompatActivity {
private ListView history_List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initialize();
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
finish();
return true;
}
private void initialize(){
history_List = (ListView) findViewById(R.id.history_List);
new TCP_Client(new TCP_Client.AsyncResponse() {
@Override
public void processFinish(String[] output) {
ArrayList<String[]> day_Averrage_History = new ArrayList<String[]>();
for(String encoded_Array:output){
day_Averrage_History.add(Encoder.unserialize(encoded_Array));
}
Diagramm_Builder diagramm_builder = new Diagramm_Builder(HistoryActivity.this);
diagramm_builder.set_Drawing_Parameters(day_Averrage_History);//Here I pass the variable
diagramm_builder.invalidate();
}
}
).execute(new String[]{"r","dah","TecHFreaKAV"});
}
}
DiagrammBuilder:
public class Diagramm_Builder extends View {
public Context ctx;
private ArrayList<String[]> parameters;
public Diagramm_Builder(Context context){
super(context);
}
public Diagramm_Builder(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void set_Drawing_Parameters(ArrayList<String[]> given_Parameters){
parameters = given_Parameters;
}
public Diagramm_Builder(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = this.getContext();
}
public ArrayList<String[]> get_Drawing_Parameters(){
try {
Log.e("Message", "Lenght = " + this.parameters.size());
}catch(NullPointerException e){
Log.e("Message","Lenght = 0");
}
return parameters;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(get_Drawing_Parameters() == null) {
Rect test_rect = new Rect();
test_rect.set(1, 1, canvas.getWidth(), canvas.getHeight());
Paint color = new Paint();
color.setColor(Color.DKGRAY);
color.setStyle(Paint.Style.FILL);
canvas.drawRect(test_rect, color);
}else{
Log.e("Message","Recived Array");
}
}
}
的Consule輸出兩次E:/消息:長度= 0,這意味着該ArrayList沒有到達...
我會非常感謝任何幫助;-)