选取多个textview不在widget中工作

时间:2014-05-13 08:56:14

标签: java android

我在appwidget中对单个textview实现了marque效果。但每当我再添加一个textview。它仅适用于最后的textview。任何人都可以为我提供解决方案。我想并行两个textview。

  <TextView
        android:id="@+id/textMarqueName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@android:color/black"
        android:duplicateParentState="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingBottom="0dp"
        android:paddingTop="0dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="@string/textMarque"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" >

        <requestFocus
            android:duplicateParentState="true"
            android:focusable="true"
            android:focusableInTouchMode="true" />
    </TextView>

    <TextView
        android:id="@+id/textMarqueDiff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textMarqueName"
        android:background="@android:color/black"
        android:duplicateParentState="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingBottom="0dp"
        android:paddingTop="0dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="@string/textMarque"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" >

        <requestFocus
            android:duplicateParentState="true"
            android:focusable="true"
            android:focusableInTouchMode="true" />
    </TextView>

---------------Here is my code..-----------------


public class Main extends AppWidgetProvider {
// private static String url =
// "http://10.0.2.2:8081/ci-restserver/index.php/api/Test/test/format/json";
private static String url = "http://api.androidhive.info/contacts/";
String htmlData = "<p><h1> This is first line starting asldfkjlasdfjlaskdfjl;aksfj  alskjdflkasf </h1></br><h1> and here comes second line agaoin afaskldfjl;aksfj lakdfjlkasjflkjadflkjasl;dfkja ldfjlaskdfjklafjklasdfjlkadfj aklsdfjlkasdf aslkdfj alskdfjlaskdf lkasjdflkjdflj</h1></p>";

private static final String TAG_CURRENCY = "contacts";

private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";

String currencyText = "";
static JSONArray currency = null;

Context _context;
AppWidgetManager _appWidgetManager;
int[] _appWidgetIds;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    _context = context;
    _appWidgetManager = appWidgetManager;
    _appWidgetIds = appWidgetIds;

    startUp();

    Log.i("Into the widget", "This is test calling");
}

public void startUp() {
    // check for Internet connectivity
    ConnectionDetector cd = new ConnectionDetector(_context);
    Boolean isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {
        Toast.makeText(_context, "Connection Available", Toast.LENGTH_LONG)
                .show();
        new GetCurrency().execute();
    } else {
        Toast.makeText(_context, "Connection Problem", Toast.LENGTH_LONG)
                .show();
    }
}

private class GetCurrency extends AsyncTask<Void, Void, Void> {

    RemoteViews updateViews = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {

        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                currency = jsonObj.getJSONArray(TAG_CURRENCY);
                Log.d("just outsied: ", "currecy asjflaskdfjl asd");
                // looping through All Contacts
                for (int i = 0; i < currency.length(); i++) {

                    JSONObject c = currency.getJSONObject(i);
                    String name = c.getString(TAG_NAME);
                    currencyText += name;
                    currencyText += " :- ";
                    String email = c.getString(TAG_EMAIL);
                    currencyText += email;
                    currencyText += "    ";
                    Log.d(String.valueOf(i), "> " + currencyText);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        updateViews = new RemoteViews(_context.getPackageName(),
                R.layout.activity_main);

//to update textViews
        updateViews.setTextViewText(R.id.textMarqueDiff, currencyText);
        updateViews.setTextViewText(R.id.textMarqueName, htmlData);

        for (int i = 0; i < _appWidgetIds.length; i++) {
            _appWidgetManager
                    .updateAppWidget(_appWidgetIds[i], updateViews);
        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

// Try this way,hope this will help you...

**activity_main.xml**
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textMarqueName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@android:color/black"
        android:duplicateParentState="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingBottom="0dp"
        android:paddingTop="0dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/textMarqueDiff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/textMarqueName"
        android:background="@android:color/black"
        android:duplicateParentState="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingBottom="0dp"
        android:paddingTop="0dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"/>

</RelativeLayout>

**MainActivity.java**
public class MainActivity extends Activity {

    private TextView textMarqueDiff;
    private TextView textMarqueName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textMarqueName =(TextView) findViewById(R.id.textMarqueName);
        textMarqueDiff =(TextView) findViewById(R.id.textMarqueDiff);

        textMarqueDiff.setSelected(true);
        textMarqueName.setSelected(true);

    }

}