사용자 지정 목록보기에 데이터를 설정할 수 없습니다 ..
튜토리얼 링크는 learn2crack입니다.
튜토리얼 자체는 괜찮습니다. 코드와 결합하면 데이터가 목록보기에 도달 할 수 없습니다 ..
첫째,
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;
}
}
중괄호가 빠진 걸 용서 해주세요 .. 목록보기에 데이터를 설정하는 방법 만 보여 드리겠습니다. 저는 안드로이드 초보자입니다 .. 인내심을 갖고 조금만 가르쳐주세요. 미리 감사드립니다 ..
PS 내 앱 개요는 rss에서 데이터를 가져오고 데이터를 listview로 설정하는 것입니다. 사용자 지정 listview로 변경하기 전에 완전히 작동합니다.하지만 사용자 지정 목록으로 변경하면이 오류가 발생합니다.
Fixed .. 사실, learn2crack 사용자 지정 목록 자습서가 내 코드에 맞지 않습니다 ..
그래서 AndroidExample 에서 코드를 변경합니다. 이제 문제가 해결되었으며 AsyncTask로도 괜찮습니다 ..
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다