我需要,当我点击XML文件时,将数据从文件传递到应用程序...如果我传递文件的路径静态它工作...但如果我使用intent filter
所以我动态传递数据它不工作......我给你看了两个代码:
没有意图过滤器:
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/ordinazioni/26:Maggio:2017-15:02:40 PM.xml");
FileInputStream is = new FileInputStream(file);
. . .
有意图过滤器:
Intent intentt = getActivity().getIntent();
String intenttt=intentt.getData().toString();
System.out.println("INTENT:"+intenttt);
//File filee = new File(intenttt);
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() +intenttt);
System.out.println("FILE:"+file);
FileInputStream is = new FileInputStream(file);
. . .
谁能帮助我?
OnCreate中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
int a=0;
String status="";
boolean statuss = false;
/* File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/ordinazioni/26:Maggio:2017-15:02:40 PM.xml");*/
Intent intentt = getActivity().getIntent();
String intenttt=intentt.getData().toString();
System.out.println("INTENT:"+intenttt);
//File filee = new File(intenttt);
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() +intenttt);
System.out.println("FILE:"+file);
FileInputStream is = new FileInputStream(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
Element element = doc.getDocumentElement();
element.normalize();
NodeList nList = doc.getElementsByTagName("checkboxes_pizza");
for (int i = 0; i < nList.getLength(); i++) {
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
//tv1.setText(tv1.getText()+"\nName : " + getValue("name", element2)+"\n");
String id = getValue("id", element2);
a = Integer.parseInt(id);
status = getValue("status", element2);
statuss= Boolean.parseBoolean(status);
System.out.println("XML:" + a);
hm.add(a);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = nodeList.item(0);
return node.getNodeValue();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the custom_spinner_items for this fragment
//super.onDestroy();
SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
ck=(CheckBox)rootView.findViewById(R.id.chk_box);
//lv = (ListView)getView().findViewById(R.id.listview);
//lv = (ListView) rootView.findViewById(R.id.listview);
//ListAdapter listAdapter = new MyListAdapter(getContext());
//lv.setAdapter(listAdapter);
context = getActivity();
mInstance = getActivity();
btnLogout = (Button) rootView.findViewById(R.id.btnLogout);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView)getView().findViewById(R.id.listview);
//ck=(CheckBox)getView().findViewById(R.id.chk_box);
displayPlanetList();
}
private void displayPlanetList() {
planetList = new ArrayList<Planet>();
planetList.add(new Planet(0, "Margherita", "6", "€", "(pomodoro e mozzarella)"));
planetList.add(new Planet(1,"Diavola", "7","€","(pomodoro,mozzarella e salsiccia piccante)"));
planetList.add(new Planet(2,"Bufalina", "5","€","(pomodoro e mozzarella di bufala)"));
planetList.add(new Planet(3, "Marinara", "5", "€", "(pomodoro)"));
planetList.add(new Planet(4,"Viennese", "4", "€", "(pomodoro,mozzarella e wrustel)"));
plAdapter = new PlanetAdapter(planetList, getContext(),imageId,hm) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String pero="";
int la=0;
int pos = lv.getPositionForView(buttonView);
System.out.println("POS:"+pos);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
Toast.makeText(
getActivity(),
"Clicked on Pizza: " + la + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();
}
}
};
lv.setAdapter(plAdapter);
}
CUSTOM ADAPTER:
public abstract class PlanetAdapter extends ArrayAdapter<Planet> implements CompoundButton.OnCheckedChangeListener, View.OnClickListener
{
private List<Planet> planetList;
private Context context ;
ArrayList<Birra> objects;
public boolean Checked;
private Integer[] imageId;
String ciao;
static PlanetAdapter plAdapter;
String selState ;
boolean[] itemChecked;
ArrayList<Integer> hmmm;
public PlanetAdapter(List<Planet> planetList, Context context,Integer[] imageId,ArrayList<Integer>hmm) {
super(context, R.layout.single_listview_item,planetList);
//itemChecked = new boolean[planetList.size()];
this.planetList = planetList;
this.context = context;
this.imageId = imageId;
this.itemChecked=new boolean[planetList.size()];
this.hmmm=hmm;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
@Override
public void onClick(View v) {
}
public class PlanetHolder {
public TextView planetName;
public TextView distView;
public TextView valuta;
public CheckBox chkBox;
// public EditText edit;
public TextView caratteristica;
public TextView id;
public ImageView imageView;
public Spinner spinner;
public NumberPicker np;
}
@Override
public int getCount() {
return planetList.size();
}
@Override
public Planet getItem(int position) {
return planetList.get(position);
}
static ArrayList<Integer> itemss;
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View row = convertView;
PlanetHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.single_listview_item, parent, false);
holder = new PlanetHolder();
holder.planetName = (TextView) row.findViewById(R.id.name);
holder.distView = (TextView) row.findViewById(R.id.dist);
holder.valuta = (TextView) row.findViewById(R.id.valuta);
holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
// holder.edit = (EditText) row.findViewById(R.id.editText);
holder.spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
holder.np=(NumberPicker)row.findViewById(R.id.np);
// holder.edit2 = (EditText) row.findViewById(R.id.editText2);
// holder.iterator = (Button) row.findViewById(R.id.iterator);
// Button iterator=(Button) row.findViewById(R.id.iterator);
// iterator.setOnClickListener(this.PlanetHolder);
holder.caratteristica=(TextView) row.findViewById(R.id.caratteristica);
holder.imageView = (ImageView) row.findViewById(R.id.ivImage);
// holder.edit.setVisibility(View.GONE);
// holder.edit.setEnabled(false);
holder.spinner.setVisibility(View.GONE);
// holder.edit.setVisibility(View.GONE);
// holder.id = (TextView) row.findViewById(R.id.id);
row.setTag(holder);
} else {
holder = (PlanetHolder) row.getTag();
}
final Planet p = planetList.get(position);
System.out.println("stampato P" + p);
holder.imageView.setImageResource(imageId[position]);
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.valuta.setText("" + p.getValuta());
holder.chkBox.setChecked(p.isSelected());
holder.chkBox.setTag(p);
holder.caratteristica.setText(p.getCaratteristica());
// holder.edit.setEnabled(false);
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
boolean isChecked = states.getBoolean("holder.chkBox" + holder.planetName.getText().toString(), false);
System.out.println(isChecked);
//holder.chkBox.setChecked(true);
if (isChecked) {
holder.spinner.setVisibility(View.VISIBLE);
holder.np.setVisibility(View.VISIBLE);
} else {
holder.chkBox.setChecked(false);
// holder.edit.setVisibility(View.GONE);
// holder.edit.setEnabled(false);
holder.spinner.setVisibility(View.GONE);
holder.np.setVisibility(View.GONE);
}
holder.chkBox.setOnCheckedChangeListener(PlanetAdapter.this);
final PlanetHolder finalHolder = holder;
Planet z=getItem(position);
System.out.println(z);
/* holder.chkBox.setChecked(true);
holder.spinner.setVisibility(View.VISIBLE);
holder.np.setVisibility(View.VISIBLE);*/
// holder.chkBox.setChecked(true);
//holder.chkBox.findViewById(R.id.chk_box).setVisibility(View.INVISIBLE);
/*for(int i=0;i<planetList.size();i++){
if(4==position)
finalHolder.chkBox.setChecked(true);
}*/
System.out.println("ARRAYLIST:"+hmmm);
for(int i=0;i<hmmm.size();i++) {
int a=hmmm.get(i);
//if(4==position)
// finalHolder.chkBox.setChecked(true);
for (int j = 0; j < planetList.size(); j++) {
if(a==position)
finalHolder.chkBox.setChecked(true);
}
}
holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (finalHolder.chkBox.isChecked()) {
itemChecked[position]=true;
System.out.println("POSITION:"+position);
// finalHolder.edit.setVisibility(View.VISIBLE);
// finalHolder.edit.setEnabled(true);
finalHolder.spinner.setVisibility(View.VISIBLE);
finalHolder.np.setVisibility(View.VISIBLE);
finalHolder.np.setMaxValue(10);
finalHolder.np.setMinValue(0);
finalHolder.np.setWrapSelectorWheel(true);
// finalHolder.np.getMinValue();
// System.out.println("valore picker alla selezione della checkbox"+finalHolder.np.getMinValue());
// finalHolder.edit2.setVisibility(View.VISIBLE);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//finalHolder.spinner.setAdapter(adapter);
finalHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
finalHolder.spinner.setSelection(position);
// selState = finalHolder.spinner.getSelectedItem().toString();
// p.setQuantità(finalHolder.edit.getText().toString().trim());
p.setTipo(finalHolder.spinner.getSelectedItem().toString());
// finalHolder.edit2.setText(selState);
// System.out.println(selState);
System.out.println("p.getTipo()"+p.getTipo());
/* Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
finalHolder.np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
p.setQuantità2(newVal);
System.out.println(p.getQuantità2());
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putInt("finalHolder.picker" + finalHolder.planetName.getText().toString(), finalHolder.np.getId());
editor.commit();
}
});
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox" + finalHolder.planetName.getText().toString(), true);
editor.commit();
} else {
finalHolder.spinner.setVisibility(View.GONE);
finalHolder.np.setVisibility(View.GONE);
finalHolder.np.setValue(0);
}
}
});
return row;
}
答案 0 :(得分:1)
尝试使用它:
传递数据时,filepath是包含动态文件路径的字符串:
intent.putExtra("filename",filepath);
获得它时:
String intenttt=intentt.getExtras().getString("filename");