ورود

View Full Version : مشکل در کدی که نوشتم (راهنمایی)



sina4everafter
دوشنبه 30 تیر 1393, 08:59 صبح
سلام. ممنون میشم اگر راهنماییم کنید. این کد و نوشتم ولی نمی دونم چرا وقتی دکمه ها ایجاد میشن هیچ متنی و نمیگیرن.


import java.io.InputStream;


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;


import android.content.Context;




public class Category {

//---XmlPullParserFactory---
XmlPullParserFactory xmlPullParserFactoryObject;

//---Context---
Context context;

int counter = 0;

//---Array---
String[] arrayCategory;

//---Category Method---
public Category(Context context){
this.context = context;

//---try...catch---
try {

//---XmlPullParser---
xmlPullParserFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParserObject = xmlPullParserFactoryObject.newPullParser();

//---Xml file from assets folder---
InputStream input_Stream = context.getApplicationContext().getAssets().open("sample.xml");
xmlPullParserObject.setInput(input_Stream, null);

//---while---

while (xmlPullParserObject.getEventType() != XmlPullParser.END_DOCUMENT) {

//---Create Array---
arrayCategory = new String[counter];

if (xmlPullParserObject.getEventType() == XmlPullParser.START_TAG &&
xmlPullParserObject.getName().equals("button")) {
arrayCategory[counter] = xmlPullParserObject.getAttributeValue(1);
counter++;
}
xmlPullParserObject.next();
}

} catch (Exception e) {
e.printStackTrace();
}
}

public String[] getAllCats(){

return arrayCategory;
}
}


کلاس اصلی:


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends Activity {

Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

//---Create instance from Category---
String[] returnCatsArrayString = new Category(this).getAllCats();

LinearLayout li = (LinearLayout) findViewById(R.id.li);

for (int i = 0; i < returnCatsArrayString.length; i++) {

Button btn = new Button(this);
li.addView(btn);
btn.setText(returnCatsArrayString[i]);
}
}
}