Я не могу установить данные в пользовательских ListView ..данные не могут добраться до пользовательских ListView в классе AsynTask
Учебник ссылка learn2crack
Учебник нормально itself..When я совмещаю с моим кодом, данные не могут достичь ListView ..
Во-первых,
MainActivity расширяет FragmentActivity реализует OnTabChangeListener, OnPageChangeListener .. и я покажу Код от
MyPageAdapter расширяет PageAdapter .. Я оставил некоторые коды, потому что код слишком долго, чтобы получить возможность отправлять и коды могут быть спутаны с see..If нужно, я буду изменять его ..
Пожалуйста, проверьте мой код ..
MainTabActivity.java
public class MainTabActivity extends FragmentActivity implements OnTabChangeListener,OnPageChangeListener{
TextView txtGoldDate, txtGoldTime, txtGoldPrice;
SQLiteDatabase db;
private TabHost host;
private ViewPager pager;
DatabaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
acquireId();
host.setup();
TabSpec tab1 = host.newTabSpec("Tab1");
tab1.setIndicator("", getResources().getDrawable(R.drawable.mygold));
tab1.setContent(R.id.Tab1);
TabSpec tab2 = host.newTabSpec("Tab2");
tab2.setIndicator("", getResources()
.getDrawable(R.drawable.mycurrency2));
tab2.setContent(R.id.Tab2);
TabSpec tab3 = host.newTabSpec("Tab3");
tab3.setIndicator("",
getResources().getDrawable(R.drawable.myfinalmyanmar));
tab3.setContent(R.id.Tab3);
host.addTab(tab1);
host.addTab(tab2);
host.addTab(tab3);
pager.setAdapter(new MyPageAdapter(this));
pager.setOnPageChangeListener(this);
host.setOnTabChangedListener(this);
}
private void acquireId() {
// TODO Auto-generated method stub
host = (TabHost) findViewById(android.R.id.tabhost);
pager = (ViewPager) findViewById(R.id.pager);
txtGoldDate = (TextView) findViewById(R.id.txtGoldDate);
txtGoldTime = (TextView) findViewById(R.id.txtGoldTime);
txtGoldPrice = (TextView) findViewById(R.id.txtGoldPrice);
}
public void onPageSelected(int pageNumber) {
// TODO Auto-generated method stub
host.setCurrentTab(pageNumber);
}
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
System.out.println("Tab ID: " + tabId);
int pageNumber = 0;
if (tabId.equals("Tab1")) {
pageNumber = 0;
} else if (tabId.equals("Tab2")) {
pageNumber = 1;
} else if (tabId.equals("Tab3")) {
pageNumber = 2;
}
pager.setCurrentItem(pageNumber);
}
MyPageAdapter.java
public class MyPageAdapter extends PagerAdapter {
ListView myListViewGold;
LayoutInflater li1;
Button btnRefreshGold;
public MyPageAdapter(Context ctx) {
this.ctx = ctx;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (position == 1) {
btnRefreshGold.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ConnectToServer cNTS;
cNTS = new ConnectToServer(myListViewGold,goldValue);
cNTS.execute(urlGoldAddress);
}
});
((ViewPager) container).addView(v1, 0);
return v1;
}
public void acquireIdAtPageAdapter() {
// id
li1 = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v1 = li1.inflate(R.layout.gold_layout, null);
btnRefreshGold = (Button) v1.findViewById(R.id.btnRefreshGold);
myListViewGold = (ListView) v1.findViewById(R.id.myListViewGold);
txtGoldDate = (TextView) v1.findViewById(R.id.txtGoldDate);
txtGoldTime = (TextView) v1.findViewById(R.id.txtGoldTime);
txtGoldPrice = (TextView) v1.findViewById(R.id.txtGoldPrice);
}
ConnectToServer.java
public class ConnectToServer extends AsyncTask {
public ConnectToServer(ListView listview,String[] myArr) {
this.myListView = listview;
this.myArr=myArr;
}
@Override
protected Object doInBackground(Object... params) {
addGoldNode(ng, i);
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
CustomList adapter = new CustomList(ctx,myArr,imageId);
myListView.setAdapter(adapter);
Log.i("Arr value at postExecute", myArr[1]+myArr[2]);
System.out.println("myListView id onPostExecute"+myListView.getId());
};
public void addGoldNode(NodeList ng, int i) {
Element entry = (Element) ng.item(i);
String rawDate = entry.getAttribute("Date");
String dateValue[] = rawDate.split(" ");
String date = "";
String time = "";
String priceValue = entry.getAttribute("Value");
priceValue += " USD ";
if (dateValue.length > 0 && priceValue != null) {
date = dateValue[0];
time += dateValue[1]+" (UTC)";
System.out.println("date " + date);
System.out.println("time " + time);
System.out.println("priceValue " + priceValue);
myArr[1] = date;
myArr[2] = time ;
myArr[3] = priceValue;
System.out.println("Gold Value "+myArr[1] + " "+myArr[2]+" "+myArr[3]);
}
CustomList.java
public class CustomList extends ArrayAdapter<String> {
private Context context;
private String[] web=new String[6];
String date;
private Integer[] imageId;
public CustomList(Context context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
TextView text3 = (TextView) rowView.findViewById(R.id.txt3);
TextView text2 = (TextView) rowView.findViewById(R.id.txt2);
TextView text1 = (TextView) rowView.findViewById(R.id.txt1);
//TextView textErr=(TextView)rowView.findViewById(R.id.txtError);
Log.i("Custom List Value ", web[1]+web[2]+web[3]);
text1.setText(web[1]);
text2.setText(web[2]);
text3.setText(web[3]);
imageView.setImageResource(imageId[0]);
return rowView;
}
}
Пожалуйста, прости меня некоторые фигурные скобки являются missing..I'll только показать, как я делаю набор данных в listview..I'm android newbie..пожалуйста, будьте терпеливы и научите мало мне .. Заранее спасибо ..
PS мой обзор приложения Я получаю данные от rss и задаю данные в listview..Перед тем, что я перехожу к пользовательскому списку, это полностью горе rk..Но я перехожу к пользовательскому списку, эта ошибка возникает.
Я установил данные в onPostExecute() .. myArr [] является глобальным в ConnectToServer.java .. Это правильная концепция? – lynndragon
Что такое 'ctx' в' onPostExecute() 'и откуда эта переменная. Это должен быть 'Context', но в конструкторе я не вижу параметр Context'. – guptakvgaurav
Я использую ConnectToServer как внутренний класс в MainActivity .. поток класса - MainActivity (MyPageAdapter, ConnectToServer). Итак, MyPageAdapter и ConnectToServer являются внутренним классом MainActivity..ctx - это просто MainActivity..Простите, моя ошибка .. Теперь я редактирую мой пост с MainActivity – lynndragon