我正在研究JSON。我解析了JSON,我可以显示我的JSON listview (图像和文本),还成功创建了widget,我想在我的小部件中显示我的JSON的第一个项目的标题.i还写了这段代码(如果我删除小部件然后我可以在小部件中显示第一个标题)但小部件没有更新.JSON的第一个项目更新但小部件的标题没有更新。什么是错的?如果有人知道解决方案,请帮助我谢谢 这是我的代码:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/aaaaaaaaaaaaaaa"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="BriWidget"
android:label="@string/widget1name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.eightbitcloud.example.widget.8BITCLOCK_WIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget" />
</receiver>
<activity
android:name="com.brige.SpleshScreen"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class BriWidget extends AppWidgetProvider {
public static String CLOCK_WIDGET_UPDATE = "CLOCK_WIDGET_UPDATE";
RemoteViews remoteViews;
int appWidgetId;
public File file;
public Bitmap bitmap;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// initializing widget layout
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_demo);
// register for button event
remoteViews.setOnClickPendingIntent(R.id.sync_button,
buildButtonPendingIntent1(context));
// updating view with initial data
remoteViews.setTextViewText(R.id.desc, getDesc());
// request for widget update
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
MyWidgetIntentReceiver.clickCount++;
// initiate widget update request
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
return PendingIntent.getActivity(context, 0, intent, 0);
}
public static PendingIntent buildButtonPendingIntent1(Context context) {
++MyWidgetIntentReceiver.clickCount;
// initiate widget update request
Intent intent = new Intent();
intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
private static CharSequence getDesc() {
return "Beka";
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, BriWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
}
public class MyWidgetIntentReceiver extends BroadcastReceiver {
public static int clickCount = 0;
public static String CLOCK_WIDGET_UPDATE = "CLOCK_WIDGET_UPDATE";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WidgetUtils.WIDGET_UPDATE_ACTION)) {
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_demo);
// updating view
//remoteViews.setTextViewText(R.id.title, getTitle());
remoteViews.setTextViewText(R.id.desc, getDesc(""));
// re-registering for click listener
remoteViews.setOnClickPendingIntent(R.id.sync_button,
BriWidget.buildButtonPendingIntent(context));
BriWidget
.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}
private String getDesc(String abc) {
abc = (MainActivity.itemList.get(0).get(MainActivity.KEY_title))
.toString();
return abc;
}
}
public class BRIgeAdapter extends BaseAdapter {
Context mContext;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
private int screenSize;
public BRIgeAdapter(Context context, ArrayList<HashMap<String, String>> d,
int screenSize) {
this.mContext = context;
this.data = d;
this.screenSize = screenSize;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(context.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView journal = (TextView) vi.findViewById(R.id.smalljournal);
TextView title = (TextView) vi.findViewById(R.id.smalltitle);
TextView description = (TextView) vi
.findViewById(R.id.smallDescription);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.smallthumb);
TextView statId = (TextView) vi.findViewById(R.id.smallstatID);
TextView DateTime = (TextView) vi.findViewById(R.id.smallDateTime);
HashMap<String, String> itemList = new HashMap<String, String>();
itemList = data.get(position);
journal.setText(itemList.get(MainActivity.KEY_journal));
statId.setText(itemList.get(MainActivity.KEY_statID));
journal.setTypeface(MainActivity.tf2);
String titleString = itemList.get(MainActivity.KEY_title);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String DateTimeTxt = itemList.get(MainActivity.KEY_pubDate).replace(
"T", " ");
try {
Date _d = df.parse(DateTimeTxt);
SimpleDateFormat new_df = new SimpleDateFormat("dd.MM.yyyy");
String _s = new_df.format(_d);
DateTime.setText(_s);
} catch (ParseException e) {
e.printStackTrace();
}
if (screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL)
description.setVisibility(View.INVISIBLE);
else
description.setVisibility(View.VISIBLE);
title.setText(titleString);
title.setTypeface(MainActivity.tf2);
description.setText(itemList.get(MainActivity.KEY_description));
description.setTypeface(MainActivity.tf2);
String url = itemList.get(MainActivity.KEY_image);
imageLoader.DisplayImage(url, thumb_image);
return vi;
}
}
MainActivity.java类
public class MainActivity extends Activity {
public String URL = "********************************";
public static String KEY_title = "title";
public static String KEY_description = "description";
public static String KEY_image = "image";
public static String KEY_journal = "journal";
public static String KEY_JournalID = "JournalID";
public static String KEY_pubDate = "pubDate";
public static String KEY_statID = "statID";
public JSONArray jsonarray;
public ListView list;
public JSONParser jsonparser;
static BRIgeAdapter adapter;
ProgressDialog pDialog, pDialog1;
static String fontPath2 = "font.ttf";
public static Typeface tf2;
public static ArrayList<HashMap<String, String>> itemList;
public ImageLoader imageLoader;
static final int DIALOG_ERROR_CONNECTION = 1;
;
public static String dateTime;
private ArrayList<Content> contents = new ArrayList<Content>();
public TransparentProgressDialog pd;
public HashMap<String, String> map;
private int screenSize;
public LoadDataAllChanelsToServer loadData;
AlertDialogManager alert = new AlertDialogManager();
Writer writer;
public File yourFile;
View menu_Slide;
@SuppressLint("CutPasteId")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
itemList = new ArrayList<HashMap<String, String>>();
list = (ListView) findViewById(R.id.listView1);
adapter = new BRIgeAdapter(MainActivity.this, itemList, screenSize);
Thread.setDefaultUncaughtExceptionHandler(new UnCaughtException(this,
this));
loadData = new LoadDataAllChanelsToServer();
menu_Slide = (findViewById(R.id.menu_button));
tf2 = Typeface.createFromAsset(getAssets(), fontPath2);
pd = new TransparentProgressDialog(this, R.drawable.loader);
loadData.execute();
}
private class LoadDataAllChanelsToServer extends
AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pd.show();
}
@Override
protected String doInBackground(String... urls) {
jsonparser = new JSONParser();
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", jsonobject.getString(KEY_JournalID));
map.put("pubDate", jsonobject.getString(KEY_pubDate));
map.put("statID", jsonobject.getString(KEY_statID));
Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),
jsonobject.getString("title"),
jsonobject.getString("pubDate"),
jsonobject.getString("description"),
jsonobject.getString("JournalID"),
jsonobject.getString("statID"));
contents.add(cont);
itemList.add(map);
dateTime = itemList.get(itemList.size() - 1).get(
KEY_pubDate);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return itemList.toString();
}
@Override
protected void onPostExecute(String result) {
try {
if (pd != null) {
pd.dismiss();
}
} catch (Exception e) {
}
try {
adapter = new BRIgeAdapter(MainActivity.this, itemList,
screenSize);
list.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
@Override
public void onBackPressed() {
createDialog();
}
private void createDialog() {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Are you sure you want to exit?");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.super.onBackPressed();
}
}
);
alertDlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDlg.create().show();
}
}
什么是错的?有人知道解决方案吗?
答案 0 :(得分:0)
要在每个特定时间间隔后使用警报管理器更新窗口小部件。