为什么我的代码无法正常工作? [英] why my code doesnt work correctly ?

查看:69
本文介绍了为什么我的代码无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我试图以编程方式制作一个Android的UI。

第1部分:

确实在第一步我从Web服务器获取数据并将其保存在数组中。第一步效果很好,没有任何问题。

这里是第一步的代码:

  protected   String  doInBackground( String  ... Urls) {
字符串数据= ;
HttpURLConnection urlConnection = null;
尝试 {
网址myUrl =新网址( http://10.0.2.2:80/Urgence/Default.aspx?type= +型);
urlConnection =(HttpURLConnection)myUrl.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream()));
字符串 temp = ;
// 数据用于存储服务器的响应
while ((temp = in.readLine())!= null)
{
Data = Data + temp;
}
}
catch (Exception ex){
Log.d( Er> doInBackground,ex.toString());
urlConnection.disconnect();
}
最后 {
urlConnection.disconnect();
}
返回数据; // 它将结果发送到onPostExcute
}
protected void onPostExecute( String 数据){
try {
serverResponsesArray = Data.split( / 3 );
makeItems(serverResponsesArray);
dialog.cancel();
}
catch (Exception ex){
Log.d( Er> onPostExecute,ex.toString());
}
最后 {
dialog.cancel();
}
}





第2部分:


在第二步中我想制作一个用户界面并在某些行中显示我的数据!

如果您注意到我的代码,您可以意识到我使用TextView来显示数组的成员(主题,作者,主要文本,日期)。起初我使用TextView显示(日期,作家和主题),之后我用它来显示主要文本。现在它显示日期,时间和作者,但它不显示正文!令人惊讶的是它增加了行的高度,但我看不到任何正文!我调试了我的代码3次,但它没有任何问题!现在帮我一个忙,告诉我代码中的确切问题是什么?提前谢谢

第二部分代码



  public   void  makeItems( String  Data []){
int marginHorizo​​ntal =( int )getResources()。getDimension(R.dimen.lpll_marginhor); // 它用于设置llc的保证金
int marginVertical =( int )getResources()。getDimension(R.dimen.lpll_marginver); // 它用于设置保证金
int txtMargin =( int )getResources()。getDimension(R.dimen.lptvmargin); // 它用于设置TextView的边距
字体face = Typeface.createFromAsset(getAssets(), Font / ADOBEARABIC-REGULAR.OTF);
LayoutParams lpll; //
LinearLayout llm =(LinearLayout)findViewById(R .id.contentMainLLayout); // 主线性布局
LinearLayout llc; // 线性布局作为项目的行
TextView tv;
LayoutParams lptv; // 它用于设置TextView的LayoutParams
字符串 separateDataArray [] = new 字符串 [ 4 ] ;
尝试 {
for int i = 0; i< = 2; i ++){
separateDataArray = Data [i] .split( - 4 ); //
llc = new LinearLayout( this );
llc.setOrientation(LinearLayout.HORIZONTAL);
lpll = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
lpll.setMargins(marginVertical,marginHorizo​​ntal,marginVertical,marginHorizo​​ntal);
llc.setLayoutParams(lpll);
if (i%2 == 0){
llc.setBackgroundColor(getResources()。getColor(R.color.bcfbc8));
}
else {
llc.setBackgroundColor(getResources()。getColor(R.color.bcd1fb));
}
lptv = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); //
lptv.setMargins(txtMargin,txtMargin,txtMargin,txtMargin); //
// 电视节目日期,作家和主题
tv = new TextView( this ); //
tv.setTypeface(face); //
tv.setTextSize(getResources( ).getDimension(R.dimen.content_item_txtsize)); // *
tv.setText ( موضوع: + separateDataArray [ 0 ] + \ n + نویسنده: + separateDataArray [ 2 ] + \ n + تاریخ: + separateDataArray [ 1 ]); //
tv.setLayoutParams(LPTV);
llc.addView(tv);
// 新电视节目主要文字
tv = new TextView();
tv.setTypeface(face); //
tv.setTextSize(getResources ()。getDimension(R.dimen.content_item_txtsize)); // *
tv。 setGravity(Gravity.CENTER);
tv.setText(separateDataArray [ 3 ]);
tv.setLayoutParams(lptv);
llc.addView(tv);
//
llm.addView(llc);
}
}
catch (Exception ex){
Log.d( Er> makeItems,ex.toString());
}
}

解决方案

我想我解决了!只需更改此行:

 llc.setOrientation(LinearLayout.VERTICAL); 



但我不知道原因!


hello . i am trying to make an android's UI programmatically.
Part 1 :
indeed at the first step i get the data from web server and save it in array . the first step works very well and it doesn't have any problem .
here this is the code of first step :

protected String doInBackground(String...Urls){
			String Data="";
			HttpURLConnection urlConnection = null;	
			try{
	    	    URL myUrl=new URL("http://10.0.2.2:80/Urgence/Default.aspx?type="+Type);
	    	    urlConnection = (HttpURLConnection)myUrl.openConnection();		
	    		BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));    		
	    		String temp="";	
	    		// Data is used to store Server's Response 
	    		while((temp=in.readLine())!=null)
	    		{    			
	    			 Data=Data+temp;   		
	    		}    
			}
			catch(Exception ex){
				Log.d("Er>doInBackground", ex.toString());
				urlConnection.disconnect();	
			}
			finally{
				urlConnection.disconnect();
				}
				return Data;// it sends Result to onPostExcute 			
		}
		protected void onPostExecute(String Data){
			try{
			serverResponsesArray=Data.split("/",3);
			makeItems(serverResponsesArray);
			dialog.cancel();			
			}
			catch(Exception ex){
				Log.d("Er>onPostExecute",ex.toString());
			}
			finally{
			 dialog.cancel();
			}
	    }



part 2 :

in second step i want to make an UI and show my data in some Rows !
if you notice to my code you can realize that i use a TextView to show array's members(subject,writer,main text,Date) . at first i use TextView for showing (Date,writer and subject) and after that i use it to show the main text . now it shows Date , Time and writer but it doesn't show the main text ! amazingly it increases the row's height but i cant see any main text ! i have debugged my code 3 times but it didnt have any problem !! now do me a favor and tell me what is the exact problem in my code ?! thanks in advance
code of second part :

public void makeItems(String Data[]){
		int marginHorizontal=(int)getResources().getDimension(R.dimen.lpll_marginhor);// it is used to set llc's margin
		int marginVertical=(int)getResources().getDimension(R.dimen.lpll_marginver);// it is used to set margin
		int txtMargin=(int)getResources().getDimension(R.dimen.lptvmargin);// it is used to set TextView's margin
		Typeface face=Typeface.createFromAsset(getAssets(),"Font/ADOBEARABIC-REGULAR.OTF");
		LayoutParams lpll;//		
		LinearLayout llm=(LinearLayout)findViewById(R.id.contentMainLLayout);//main linear Layout
		LinearLayout llc;// Linear Layout as a Item's Row
		TextView tv;
		LayoutParams lptv;// it is used to set TextView's LayoutParams		
		String separateDataArray[]=new String[4];
		try{
		for(int i=0;i<=2;i++){
			separateDataArray=Data[i].split("-",4);//
			llc=new LinearLayout(this);
			llc.setOrientation(LinearLayout.HORIZONTAL);
			lpll= new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
			lpll.setMargins(marginVertical,marginHorizontal,marginVertical,marginHorizontal);
			llc.setLayoutParams(lpll);
			if(i%2==0){
			llc.setBackgroundColor(getResources().getColor(R.color.bcfbc8));	
			}
			else{
				llc.setBackgroundColor(getResources().getColor(R.color.bcd1fb));	
			}
			lptv=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);//
			lptv.setMargins(txtMargin,txtMargin,txtMargin,txtMargin);//
			// tv shows date , writer and subject
			tv=new TextView(this);//			
			tv.setTypeface(face);//
			tv.setTextSize(getResources().getDimension(R.dimen.content_item_txtsize));//*						
			tv.setText(" موضوع : "+separateDataArray[0]+"\n"+" نویسنده : "+separateDataArray[2]+"\n"+" تاریخ : "+separateDataArray[1]);//			
			tv.setLayoutParams(lptv);			
			llc.addView(tv);
			// new tv shows main text 
			tv=new TextView(this);
			tv.setTypeface(face);//
			tv.setTextSize(getResources().getDimension(R.dimen.content_item_txtsize));//*	
			tv.setGravity(Gravity.CENTER);
			tv.setText(separateDataArray[3]);			
			tv.setLayoutParams(lptv);	
			llc.addView(tv);			 
			//
			llm.addView(llc);
			}
		}
		catch(Exception ex){
			Log.d("Er>makeItems",ex.toString());
		}
	}

解决方案

i think i solved it ! just change this line :

llc.setOrientation(LinearLayout.VERTICAL);


but i dont know the reason !


这篇关于为什么我的代码无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆