You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从.NET Web Service获取马拉地语数据到Spinner?TextView字体问题求助

Hey there! Let's break down solutions for your two Android development problems:

1. Loading Marathi Data from .NET Web Service into a Spinner

First, let's make sure we're handling character encoding correctly—Marathi uses Unicode, so your app and the web service need to communicate in UTF-8. Here's a step-by-step approach:

  • Step 1: Ensure UTF-8 support in your network request
    When calling your .NET Web Service, explicitly set the request to accept UTF-8 responses. For example, using HttpURLConnection:

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Accept-Charset", "UTF-8");
    conn.setRequestMethod("GET");
    

    On the .NET side, confirm your Web Service is configured to output UTF-8 (check that the response content type includes charset=utf-8—this fixes most garbled text issues).

  • Step 2: Parse the JSON response
    Use a JSON parser (like JSONObject or Gson) to extract the SchemeName values from the Get_DepoScheme array. Here's an example with JSONObject:

    String jsonResponse = // Your web service response string
    JSONObject rootObj = new JSONObject(jsonResponse);
    JSONArray schemeArray = rootObj.getJSONArray("Get_DepoScheme");
    
    List<String> schemeNames = new ArrayList<>();
    for (int i = 0; i < schemeArray.length(); i++) {
        JSONObject schemeObj = schemeArray.getJSONObject(i);
        // Extract the Marathi scheme name directly (UTF-8 is supported natively)
        String marathiName = schemeObj.getString("SchemeName");
        schemeNames.add(marathiName);
    }
    
  • Step 3: Attach the data to your Spinner
    Create an ArrayAdapter and bind it to your Spinner. Android supports UTF-8 text by default, so no extra configuration is needed here:

    Spinner spinner = findViewById(R.id.your_spinner_id);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
            android.R.layout.simple_spinner_item, schemeNames);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    
2. Fixing Custom Font Not Applying to TextView in onCreate

If setting the custom font in onCreate isn't working, here are the most common fixes:

  • Check your font file location
    Make sure your font file (e.g., marathi_font.ttf) is placed in the assets/fonts/ directory. Android is case-sensitive, so double-check the filename matches exactly what you're referencing in code.

  • Verify Typeface initialization
    Ensure you're loading the font correctly. Here's the proper way:

    TextView textView = findViewById(R.id.your_textview_id);
    Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/marathi_font.ttf");
    textView.setTypeface(customFont);
    

    If you're using a Material Components theme, check if your theme's default text style is overriding the font—you may need to explicitly set the font in your layout XML or create a custom text style that inherits your font.

  • Use a Custom TextView for consistent results
    If your TextView has multiple states (pressed, selected) or is used across multiple screens, creating a custom TextView class ensures the font is applied everywhere:

    public class CustomFontTextView extends TextView {
        public CustomFontTextView(Context context) {
            super(context);
            initFont();
        }
    
        public CustomFontTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initFont();
        }
    
        private void initFont() {
            Typeface customFont = Typeface.createFromAsset(getContext().getAssets(), "fonts/marathi_font.ttf");
            setTypeface(customFont);
        }
    }
    

    Then use this custom TextView in your layout:

    <com.yourpackage.CustomFontTextView
        android:id="@+id/your_textview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="मुदत ठेव"/>
    
  • Check for RecyclerView/ListView scenarios
    If your TextView is inside a RecyclerView or ListView, don't set the font in onCreate—instead, apply it in the onCreateViewHolder method of your adapter, so each list item gets the font correctly.


内容的提问来源于stack exchange,提问作者Angel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:50:42