如何从.NET Web Service获取马拉地语数据到Spinner?TextView字体问题求助
Hey there! Let's break down solutions for your two Android development problems:
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, usingHttpURLConnection: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 (likeJSONObjector Gson) to extract theSchemeNamevalues from theGet_DepoSchemearray. Here's an example withJSONObject: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 anArrayAdapterand 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);
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 theassets/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 inonCreate—instead, apply it in theonCreateViewHoldermethod of your adapter, so each list item gets the font correctly.
内容的提问来源于stack exchange,提问作者Angel

