我无法将数据设置为自定义列表视图..
教程链接为learn2crack
教程本身就可以。当我与我的代码结合使用时,数据无法到达listview ..
首先,
MainActivity扩展FragmentActivity实现了OnTabChangeListener,OnPageChangeListener .. 我会显示来自
的代码MyPageAdapter扩展了PageAdapter ..我会留下一些代码,因为代码太长而无法发布,代码可能会被混淆看..如果需要,我会修改它..
请检查我的代码..
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;
}
}
请原谅我一些花括号丢失..我只会显示我如何将数据设置为listview ..我是android新手..请耐心等待,并教我一点点.. 提前谢谢..
P.S我的应用程序概述是我从rss获取数据并将数据设置为listview ..在我更改为自定义列表视图之前,它完全正常工作。但是我更改为自定义列表,发生此错误。
答案 0 :(得分:0)
写一个单独的功能
例如:
public void setMyListAdapter()
// set your adapter over here
从onPostExecute()
调用此函数答案 1 :(得分:0)
修正..实际上,learn2crack 自定义列表教程不适合我的代码..
所以,我改变了AndroidExample的代码..现在,问题已经修复,AsyncTask也没问题。