我有一个活动和一个App Widget Provider活动。我有一些字符串从网络上的数据库获取信息并显示。 (他们有两个文字观点) 但我不知道如何将它们包含在我的小部件中。 我在我的活动上复制了两个文本视图并粘贴到我的小部件活动上,但他们没有显示来自数据库的任何信息。 当然我有很多文本视图可以获得一些信息,但我只需要" index_payam_t"和" index_txt2_t" ! 我的活动代码是:
package com.irprogram.ted;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class index extends Activity {
private SharedPreferences sp;
private ImageView update,share,New,edit,setting,exit;
private TextView welcome,name,txt1,tedad,txt2,payam;
private database db;
private Typeface times;
private Typeface homa;
private Typeface nazanin;
public static String res="";
private int count=0;
public static int count2=0;
public static String countS="";
public static String news="";
private int countL=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index);
db=new database(this);
tarif();
times=Typeface.createFromAsset(getAssets(), "Font/times.ttf");
homa=Typeface.createFromAsset(getAssets(), "Font/homa.ttf");
nazanin=Typeface.createFromAsset(getAssets(), "Font/nazanin.ttf");
welcome.setTypeface(homa);
txt1.setTypeface(homa);
name.setTypeface(times);
txt2.setTypeface(homa);
payam.setTypeface(nazanin);
getcount();
sp=getApplicationContext().getSharedPreferences("userP", 0);
final String s= sp.getString("username", "");
name.setText(s);
tedad.setText("0");
payam.setText("-");
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(tedad.getText().toString().equals("0")){
Toast.makeText(getApplicationContext(), "no new update!!!", Toast.LENGTH_LONG).show();
}else{
update(tedad.getText().toString());
}
}
});
share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent ed=new Intent(index.this,sharedlist.class);
startActivity(ed);
}
});
New.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent ed=new Intent(index.this,newshare.class);
ed.putExtra("username", s);
startActivity(ed);
}
});
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent ed=new Intent(index.this,editprofile.class);
ed.putExtra("username", s);
startActivity(ed);
}
});
setting.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent ed=new Intent(index.this,setting.class);
startActivity(ed);
}
});
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
sp=getApplicationContext().getSharedPreferences("userP", 0);
Editor edit=sp.edit();
edit.putString("username", "");
edit.putInt("status", 0);
edit.commit();
finish();
}
});
}
private void tarif(){
update=(ImageView) findViewById(R.id.index_update);
share=(ImageView) findViewById(R.id.index_share);
New=(ImageView) findViewById(R.id.index_new);
edit=(ImageView) findViewById(R.id.index_edit);
setting=(ImageView) findViewById(R.id.index_setting);
exit=(ImageView) findViewById(R.id.index_exit);
welcome=(TextView) findViewById(R.id.index_welcome1_t);
name=(TextView) findViewById(R.id.index_name_t);
txt1=(TextView) findViewById(R.id.index_txt1_t);
tedad=(TextView) findViewById(R.id.index_tedad_t);
txt2=(TextView) findViewById(R.id.index_txt2_t);
payam=(TextView) findViewById(R.id.index_payam_t);
}
private void getcount(){
new getcount("http://example.com/app/count.php").execute();
new gfn("http://example.com/app/gfn.php").execute();
final Timer tm=new Timer();
final ProgressDialog pd=new ProgressDialog(index.this);
pd.setMessage("در حال دریافت تعداد جملات اماده دریافت"+"\n"+"برای استفاده از برنامه در حالت آفلاین کلید بازگشت را بزنید");
pd.show();
pd.setOnCancelListener(new ProgressDialog.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
new getcount("http://example.com/app/update.php").cancel(true);
new gfn("http://example.com/app/update.php").cancel(true);
count=0;
tm.cancel();
}
});
tm.scheduleAtFixedRate(new TimerTask(){
public void run() {
runOnUiThread(new Runnable(){
public void run() {
count++;
if(count==30){
pd.cancel();
tm.cancel();
new getcount("http://example.com/app/update.php").cancel(true);
new gfn("http://example.com/app/update.php").cancel(true);
Toast.makeText(getApplicationContext(), "خطا در برقراری ارتباط", Toast.LENGTH_LONG).show();
tedad.setText("0");
}
if(!countS.equals("")){
pd.setMessage("در حال دریافت اخبار آنلاین"+"\n"+"برای استفاده از برنامه در حالت آفلاین کلید بازگشت را بزنید");
}
if(!countS.equals("") && !news.equals("")){
pd.cancel();
db.open();
int countL=db.count("content");
db.close();
int s=Integer.parseInt(countS)-countL;
tedad.setText(s+"");
payam.setText(news);
countS="";
news="";
tm.cancel();
}
}
});
}
}, 1, 1000);
}
private void update(String count){
final String te=count;
new gfc("http://example.com/app/gfc.php",count,this).execute();
final Timer tm=new Timer();
final ProgressDialog pd=new ProgressDialog(index.this);
//pd.setMessage("در حال دریافت اطلاعات");
pd.setCancelable(false);
pd.show();
pd.setOnCancelListener(new ProgressDialog.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
new gfc("http://example.com/app/gfc.php",te,index.this).cancel(true);
tm.cancel();
}
});
tm.scheduleAtFixedRate(new TimerTask(){
public void run() {
runOnUiThread(new Runnable(){
public void run() {
int s=Integer.parseInt(tedad.getText().toString())-count2;
pd.setMessage(" در حال دریافت اطلاعات"+"\n"+"جملات باقی مانده "+s+"\n"+"بریا لغو عملیات بر روی کلید بازگشت کلیک کنید");
if(!res.equals("")){
pd.cancel();
count2=0;
Toast.makeText(getApplicationContext(), "update comleted!!!", Toast.LENGTH_LONG).show();
getcount();
tm.cancel();
}
}
});
}
}, 1, 1000);
}
}
答案 0 :(得分:0)
在Android中实施App Widgets时,您必须使用RemoteViews
而不是View
s,并且您应该使用方法{设置TextView
的文本{3}},
您可以查看setTextViewText()
教程,其中提供了大量有关处理App小部件及其RemoteViews
的信息。