Java 我能';t将文本(从我的资产文件夹读取)设置到我的列表

Java 我能';t将文本(从我的资产文件夹读取)设置到我的列表,java,android,list,android-assets,Java,Android,List,Android Assets,在我的应用程序中,我想从asset读取文本。 现在我的资源文件夹中有文本,我想我的问题是无法将文本设置为列表。在此之前,我从数组中读取文本,但当我更改它时,会出现异常。如何从资源中读取文本 这是我的密码: public class ProductListFragment extends Fragment implements SearchView.OnQueryTextListener { public static final String ARG_ITEM_ID = "produc

在我的应用程序中,我想从asset读取文本。 现在我的资源文件夹中有文本,我想我的问题是无法将文本设置为列表。在此之前,我从数组中读取文本,但当我更改它时,会出现异常。如何从资源中读取文本

这是我的密码:

 public class ProductListFragment extends Fragment implements 
 SearchView.OnQueryTextListener {

 public static final String ARG_ITEM_ID = "product_list";
 ArrayList<String> dataItems = new ArrayList<String>();
 static Activity activity;
 ListView productListView;
 InputStream in;
 BufferedReader reader;
 Context context;
 String line = "1";
List<Product> products;
ProductListAdapter productListAdapter;
SearchView search_view;
SharedPreference sharedPreference;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = getActivity();
    sharedPreference = new SharedPreference();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_product_list, container,
            false);
    findViewsById(view);
    search_view = (SearchView) view.findViewById(R.id.search_view);



    productListAdapter = new ProductListAdapter(activity, products);
    productListView.setAdapter(productListAdapter);
    ReadText();
    for (int i = 0; i < 100; i++) {

        Product wp = new Product(dataItems.get(i));
        products.add(wp);
    }

    search_view.setOnQueryTextListener(this);

    return view;
}


private void findViewsById(View view) {
    productListView = (ListView) view.findViewById(R.id.list_product);
}

@Override
public void onResume() {
    getActivity().setTitle(R.string.app_name);
    //getActivity().getActionBar().setTitle(R.string.app_name);
    super.onResume();
}


@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    productListAdapter.getFilter().filter(newText);
    return false;
}

public static class ProductListAdapter extends ArrayAdapter<Product> implements Filterable {

    private Context context;
    List<Product> products;
    SharedPreference sharedPreference;
    List<Product> mStringFilterList;
    ValueFilter valueFilter;


    public ProductListAdapter(Context context, List<Product> products) {
        super(context, R.layout.product_list_item, products);

        this.context = context;
        this.products = products;
        sharedPreference = new SharedPreference();
        mStringFilterList = products;
    }

    private class ViewHolder {
        TextView productNameTxt;
        ImageView favoriteImg;
        ImageButton share;
    }

    @Override
    public int getCount() {
        return products.size();
    }

    @Override
    public Product getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.product_list_item, null);
            holder = new ViewHolder();
            holder.productNameTxt = (TextView) convertView
                    .findViewById(R.id.txt_pdt_name);
            holder.share = (ImageButton) convertView.findViewById(R.id.share);
            holder.favoriteImg = (ImageView) convertView
                    .findViewById(R.id.imgbtn_favorite);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Product product = (Product) getItem(position);
        holder.productNameTxt.setText(PersianReshape.reshape(product.getName()));
        holder.productNameTxt.setTypeface(G.defaultFont);
        holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBody = products.get(position).toString();

                sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
                context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
            }
        });
        holder.favoriteImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageView button = (ImageView) view.findViewById(R.id.imgbtn_favorite);

                String tag = button.getTag().toString();
                if (tag.equalsIgnoreCase("grey")) {
                    sharedPreference.addFavorite(activity, products.get(position));
                    Toast.makeText(activity,
                            activity.getResources().getString(R.string.add_favr),
                            Toast.LENGTH_SHORT).show();

                    button.setTag("red");
                    button.setImageResource(R.drawable.heart_red);
                } else {
                    sharedPreference.removeFavorite(activity, products.get(position));
                    button.setTag("grey");
                    button.setImageResource(R.drawable.heart_grey);
                    Toast.makeText(activity,
                            activity.getResources().getString(R.string.remove_favr),
                            Toast.LENGTH_SHORT).show();
                }

                //    return true;
            }
        });
    /*If a product exists in shared preferences then set heart_red drawable
     * and set a tag*/
        if (checkFavoriteItem(product)) {
            holder.favoriteImg.setImageResource(R.drawable.heart_red);
            holder.favoriteImg.setTag("red");
        } else {
            holder.favoriteImg.setImageResource(R.drawable.heart_grey);
            holder.favoriteImg.setTag("grey");
        }

        return convertView;
    }


    public boolean checkFavoriteItem(Product checkProduct) {
        boolean check = false;
        List<Product> favorites = sharedPreference.getFavorites(context);
        if (favorites != null) {
            for (Product product : favorites) {
                if (product.equals(checkProduct)) {
                    check = true;
                    break;
                }
            }
        }
        return check;
    }

    @Override
    public void add(Product product) {
        super.add(product);
        products.add(product);
        notifyDataSetChanged();
    }

    @Override
    public void remove(Product product) {
        super.remove(product);
        products.remove(product);
        notifyDataSetChanged();
    }

    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.length() > 0) {
                ArrayList<Product> filterList = new ArrayList<Product>();


                for (int i = 0; i < mStringFilterList.size(); i++) {
                    //  if ( (mStringFilterList.get(i).getName().toUpperCase() )
                    //  .contains(constraint.toString().toUpperCase())) {
                    if ((mStringFilterList.get(i).getName()).contains(constraint.toString())) {
                        Product product = new Product(mStringFilterList.get(i)
                                .getId(), mStringFilterList.get(i)
                                .getName());

                        filterList.add(product);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      Filter.FilterResults results) {
            products = (List<Product>) results.values;
            notifyDataSetChanged();
        }

    }
}

and here my text read line by line:
   private void ReadText() {
    try {
        in = this.context.getAssets().open("text.txt");
        reader = new BufferedReader(new InputStreamReader(in));
        while (line != null) {
            line = reader.readLine();
            if (line != null)
                dataItems.add(line);
            else
                break;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
公共类ProductListFragment扩展了片段实现
SearchView.OnQueryTextListener{
公共静态最终字符串ARG\u ITEM\u ID=“产品列表”;
ArrayList dataItems=新的ArrayList();
静态活动;
ListView产品ListView;
输入流输入;
缓冲读取器;
语境;
字符串行=“1”;
列出产品清单;
ProductListAdapter ProductListAdapter;
搜索视图搜索视图;
SharedReference SharedReference;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
activity=getActivity();
SharedReference=新的SharedReference();
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u产品清单、容器、,
假);
FindViewById(视图);
search\u view=(SearchView)view.findviewbyd(R.id.search\u view);
productListAdapter=新的productListAdapter(活动、产品);
setAdapter(productListAdapter);
ReadText();
对于(int i=0;i<100;i++){
产品wp=新产品(dataItems.get(i));
添加(可湿性粉剂);
}
search_view.setOnQueryTextListener(这个);
返回视图;
}
私有void findViewsById(视图){
productListView=(ListView)view.findViewById(R.id.list\u产品);
}
@凌驾
恢复时公开作废(){
getActivity().setTitle(R.string.app_name);
//getActivity().getActionBar().setTitle(R.string.app_name);
super.onResume();
}
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
productListAdapter.getFilter().filter(新文本);
返回false;
}
公共静态类ProductListAdapter扩展ArrayAdapter实现可筛选{
私人语境;
列出产品清单;
SharedReference SharedReference;
列出mStringFilterList;
ValueFilter-ValueFilter;
公共产品列表适配器(上下文、列表产品){
超级(上下文、右布局、产品列表、产品);
this.context=上下文;
这一点。产品=产品;
SharedReference=新的SharedReference();
mStringFilterList=产品;
}
私有类视窗持有者{
TextView productNameTxt;
图像视图偏爱img;
图像按钮共享;
}
@凌驾
public int getCount(){
返回产品。大小();
}
@凌驾
公共产品getItem(内部位置){
退货。获得(职位);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(活动.布局\充气机\服务);
convertView=充气机。充气(R.layout.product\u list\u项,空);
holder=新的ViewHolder();
holder.productNameTxt=(TextView)convertView
.findviewbyd(R.id.txt\u pdt\u名称);
holder.share=(ImageButton)convertView.findViewById(R.id.share);
holder.favoriteImg=(图像视图)convertView
.findviewbyd(R.id.imgbtn_收藏夹);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
产品产品=(产品)获取项目(位置);
holder.productNameTxt.setText(persianReformate.Reformate(product.getName());
holder.productNameTxt.setTypeface(G.defaultFont);
holder.share.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向共享内容=新意向(意向.行动\发送);
sharingcontent.setType(“文本/普通”);
字符串shareBody=products.get(position.toString();
共享内容putExtra(Intent.EXTRA_文本,共享体);
context.startActivity(Intent.createChooser(共享内容,“通过共享”);
}
});
holder.favoriteImg.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
ImageView按钮=(ImageView)view.findViewById(R.id.imgbtn_收藏夹);
字符串标记=button.getTag().toString();
if(标记为“灰色”){
SharedReference.addFavorite(活动、产品、获取(职位));
Toast.makeText(活动,
activity.getResources().getString(R.string.add_favr),
吐司。长度(短)。show();
按钮。设置标签(“红色”);
按钮。setImageResource(R.drawable.heart_-red);
}否则{
SharedReference.removeFavorite(活动、产品、获取(职位));
按钮。设置标签(“灰色”);
按钮。setImageResource(R.drawable.heart_灰);
Toast.makeText(活动,
activity.getResources().getString(R.string.remove_favr),
吐司。长度(短)。show();
}
//返回true;
}
});
/*如果产品存在于共享首选项中,则设置heart\u red drawable
*并设置一个标签*/
if(检查收藏夹项目(产品)){
holder.favoriteImg.setImageResource(R.drawable.heart_-red);
AssetManager am = context.getAssets();
InputStream is = am.open("text.txt");
 private String readTxt(){

     InputStream inputStream = getResources().openRawResource(R.raw.toc);
//     InputStream inputStream = getResources().openRawResource(R.raw.internals);
     System.out.println(inputStream);
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

     int i;
  try {
   i = inputStream.read();
   while (i != -1)
      {
       byteArrayOutputStream.write(i);
       i = inputStream.read();
      }
      inputStream.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

     return byteArrayOutputStream.toString();
    }
}