开发者

re-use variable from another class file to AppWidget Android

everyone, i've a little problem how to use variable from another class file to App Widget.

Here, my little sourcecode :

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
        开发者_JS百科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;
    }

}

}

And 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);
    }

}
 }

On public void run() in HelloWidget.Java, i want to call/use listItem.getXYZ() from TimeKu.Java with same result. How? Very great Thanks for your help.

Regards,

Widy.


It's generally a bad idea for one class to have access to members of another class without very good reason.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜