没有文本 R.string 链接可能?字符串 [] 名称 = ;

Posted

技术标签:

【中文标题】没有文本 R.string 链接可能?字符串 [] 名称 = ;【英文标题】:No text R.string linking possible? String[] names = ;没有文本 R.string 链接可能?字符串 [] 名称 = ; 【发布时间】:2020-10-20 22:22:35 【问题描述】:

您好,我想使用本教程创建一个包含搜索、图像和文本的 Litsview 页面,到目前为止效果很好。

Listview Tutorial

但是我不能使用 strings.xml 中的文本吗?

String[] names = R.string.perkablenkungname,;  // error

String[] names = getResources().getString(R.string.perkablenkungname),;  ///no error App crash

String[] names = getString(R.string.perkablenkungname),;   /// no error App crash

对不起我的英语不好!

【问题讨论】:

【参考方案1】:

试试这个方法

像这样在string.xml 中创建一个数组

<array name="namesArray">
    <item>Item One</item>
    <item>Item Two</item>
    <item>Item Three</item>
</array>

现在像这样访问你的数组

 String[] names = getResources().getStringArray(R.array.namesArray); 

【讨论】:

应用程序也不以此开头。它崩溃而没有错误消息 @whazza83 你能分享你的代码吗?【参考方案2】:
package com.whazza.searchtest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity 


    String[] perks = getResources().getStringArray(R.array.namesArray);
    String[] name = "This is apple","This is banana","This is kiwi";
    String[] lehrbar = "This is","This is","This is";
    int[] images = R.drawable.apple,R.drawable.banana,R.drawable.kiwi;

    List<ItemsModel> itemsModelList = new ArrayList<>();

    ListView listView;

    CustomAdapter customAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listview);


        for(int i = 0;i < perks.length;i++)

            ItemsModel itemsModel = new ItemsModel(perks[i],name[i],lehrbar[i],images[i]);

            itemsModelList.add(itemsModel);

        

         customAdapter = new CustomAdapter(itemsModelList,this);

        listView.setAdapter(customAdapter);


    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 


        getMenuInflater().inflate(R.menu.search_menu,menu);

        MenuItem menuItem = menu.findItem(R.id.searchView);

        SearchView searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
            @Override
            public boolean onQueryTextSubmit(String query) 
                return false;
            

            @Override
            public boolean onQueryTextChange(String newText) 

                Log.e("Main"," data search"+newText);

                customAdapter.getFilter().filter(newText);



                return true;
            
        );


        return true;

    


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) 

        int id = item.getItemId();


        if(id == R.id.searchView)

            return true;
        
        return super.onOptionsItemSelected(item);
    


    public class CustomAdapter extends BaseAdapter implements Filterable 

        private List<ItemsModel> itemsModelsl;
        private List<ItemsModel> itemsModelListFiltered;
        private Context context;

        public CustomAdapter(List<ItemsModel> itemsModelsl, Context context) 
            this.itemsModelsl = itemsModelsl;
            this.itemsModelListFiltered = itemsModelsl;
            this.context = context;
        



        @Override
        public int getCount() 
            return itemsModelListFiltered.size();
        

        @Override
        public Object getItem(int position) 
            return itemsModelListFiltered.get(position);
        

        @Override
        public long getItemId(int position) 
            return position;
        

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 
            View view = getLayoutInflater().inflate(R.layout.row_items,null);


            TextView perks = view.findViewById(R.id.perks);
            TextView name = view.findViewById(R.id.name);
            TextView lehrbar = view.findViewById(R.id.lehrbar);
            ImageView imageView = view.findViewById(R.id.images);

            perks.setText(itemsModelListFiltered.get(position).getPerks());
            name.setText(itemsModelListFiltered.get(position).getName());
            lehrbar.setText(itemsModelListFiltered.get(position).getLehrbar());
            imageView.setImageResource(itemsModelListFiltered.get(position).getImages());

            view.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Log.e("main activity","item clicked");
                    startActivity(new Intent(MainActivity.this,ItemsPreviewActivity.class).putExtra("items",itemsModelListFiltered.get(position)));

                
            );

            return view;
        



        @Override
        public Filter getFilter() 
            Filter filter = new Filter() 
                @Override
                protected FilterResults performFiltering(CharSequence constraint) 

                    FilterResults filterResults = new FilterResults();
                    if(constraint == null || constraint.length() == 0)
                        filterResults.count = itemsModelsl.size();
                        filterResults.values = itemsModelsl;

                    else
                        List<ItemsModel> resultsModel = new ArrayList<>();
                        String searchStr = constraint.toString().toLowerCase();

                        for(ItemsModel itemsModel:itemsModelsl)
                            if(itemsModel.getPerks().contains(searchStr) || itemsModel.getName().contains(searchStr))
                                resultsModel.add(itemsModel);
                               
                            
                             filterResults.count = resultsModel.size();
                                filterResults.values = resultsModel;
                        


                    

                    return filterResults;
                

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) 

                    itemsModelListFiltered = (List<ItemsModel>) results.values;
                    notifyDataSetChanged();

                
            ;
            return filter;
        
    





【讨论】:

以上是关于没有文本 R.string 链接可能?字符串 [] 名称 = ;的主要内容,如果未能解决你的问题,请参考以下文章

如何使标签文本中的链接可用于语音(可访问性)? (iOS,Xamarin)

笨方法学python--字符串和文本

带有引用文本的href的HTML链接?

Android setText/R.string/values

如何将字符串中的 URL 呈现为可点击的超链接?

Android java比较xml字符串和edittext