大家,我有一个小问题,如何将另一个类文件中的变量用于App Widget。
在这里,我的小源代码:
TimeKu.Java:
public class TimeKu extends Activity {
private ListView lvUsers;
private ArrayList<UserAP> mListUsers;
@Override
public void onCreate(Bundle savedInstanceState) {
.....
}
public ArrayList<UserAP> getUsers(){
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
try {
dbAdapter.createDataBase();
} catch (IOException e) {
Log.i("*** select ",e.getMessage());
}
dbAdapter.openDataBase();
//query langsung ke database di sistem buat perbandingan
String query="SELECT * FROM ********** WHERE date(tanggal) = date('now')";
ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
dbAdapter.close();
ArrayList<UserAP> usersList = new ArrayList<UserAP>();
for (int i = 0; i < stringList.size(); i++) {
ArrayList<String> list = stringList.get(i);
UserAP user = new UserAP();
try {
user.tanggal = list.get(1);
user.xyz = list.get(2);
user.abc = list.get(3);
} catch (Exception e) {
Log.i("***" + TimeKu.class.toString(), e.getMessage());
}
usersList.add(user);
}
return usersList;
}
// ***ListAdapter***
public class ListAdapter extends ArrayAdapter<UserAP> {
private ArrayList<UserAP> mList;
private Context mContext;
public ListAdapter(Context context, int textViewResourceId,ArrayList<UserAP> list) { // --CloneChangeRequired
super(context, textViewResourceId, list);
this.mList = list;
this.setmContext(context);
}
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
try{
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item, null);
}
final UserAP listItem = mList.get(position);
if (listItem != null) {
// konfigurasi list view dari konten di database
( (TextView) view.findViewById(R.id.tgl) ).setText( listItem.getTanggal()+"");
( (TextView) view.findViewById(R.id.xyz) ).setText( listItem.getXYZ()+"");
( (TextView) view.findViewById(R.id.abc) ).setText( listItem.getABC()+"");
}}catch(Exception e){
Log.i(TimeKu.ListAdapter.class.toString(), e.getMessage());
}
return view;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public Context getmContext() {
return mContext;
}
}
}
HelloWidget.Java:
public class HelloWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
ArrayList<UserAP> list = null;
timer.scheduleAtFixedRate(new MyTime(context, list, appWidgetManager), 1, 1000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
private ArrayList<UserAP> mList;
public MyTime(Context context, ArrayList<UserAP> list, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
this.mList = list;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.sholatwidget);
thisWidget = new ComponentName(context, HelloWidget.class);
}
public void run() {
final UserAP listItem = mList.get(0);
remoteViews.setTextViewText(R.id.xyz,
"" + **[I Want to Call listItem.getXYZ() Here from TimeKu.Java. How?]**);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
在HelloWidget.Java中的public void run()上,我想调用/使用来自TimeKu.Java的listItem.getXYZ(),结果相同。怎么样? 非常感谢您的帮助。
此致
Widy。
答案 0 :(得分:0)
一个班级在没有充分理由的情况下访问另一个班级的成员通常是一个坏主意。