**
我已经解析了一个XML文件,并在动态布局中膨胀。点击
在任何布局上打开一个对话框片段,但我无法检测到哪个布局
已被选中。我想让该布局中的文本视图通过
在通过单击布局调用的对话框片段中。我试过setId(count)
,
但是当我在点击监听器上获得id时,它会给出最后一个布局的id。因为我是初学者,请编辑我的代码或任何带有详细信息的链接。 2
**
//code of fragment is below:
package com.appknoll.studentdatacollection.midwayapp;
import android.app.ActionBar;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
public class FragmentOne extends Fragment {
public static FragmentOne newInstance(){
return new FragmentOne();
}
List<XMLParserObject> posts = null;
XMLParser parser = new XMLParser();
ViewGroup layout;
GridView gridView;
GridViewCustomAdapter gridViewCustomAdapter;
List<Attributes.Name> name = new ArrayList<Attributes.Name>();
public static final int PICKER = 1;
Integer id;
String lo;
String size;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
layout=(ViewGroup)rootView.findViewById(R.id.mainLayout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getActivity(),""+getTag(),Toast.LENGTH_SHORT).show();
FragmentTransaction ft = getFragmentManager().beginTransaction();
FragmentManager fm = getActivity().getSupportFragmentManager();
DFragment dialogFragment = new DFragment ();
Bundle args = new Bundle();
args.putString("item_name",lo);
dialogFragment.setArguments(args);
Toast.makeText(getActivity(),""+id, Toast.LENGTH_LONG).show();
dialogFragment.setTargetFragment(dialogFragment, PICKER);
dialogFragment.show(fm, "Sample Fragment");
ft.commit();
}
});
try {
posts = parser.parse(getActivity().getAssets().open("rt_items.xml"));
//posts = parser.parse(getActivity().getAssets().open("rt_images"));
int count = 0;
for (XMLParserObject post : posts) {
String name2=post.getName().toString();
if(contains( name2, "Sushi" )) {
count++;
LinearLayout lii = new LinearLayout(this.getContext());
lii.setLayoutParams(new LinearLayout.LayoutParams(600, 800));
//lii.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
lii.setPadding(10, 10, 10, 10);
lii.setId(count);
id= lii.getId();
//Toast.makeText(getActivity(), ""+id, Toast.LENGTH_SHORT).show();
lii.setTag(post.getName());
//Toast.makeText(getActivity(),""+lii.getTag(), Toast.LENGTH_SHORT).show();
lii.setBackgroundResource(R.drawable.border);
lii.setOrientation(LinearLayout.VERTICAL);
layout.addView(lii);
TextView name = new TextView(this.getContext());
name.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
name.setText(post.getName());
name.setTextSize(30);
name.setTextColor(Color.parseColor("#191970"));
lii.addView(name);
TextView Price = new TextView(this.getContext());
Price.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Price.setText(Integer.toString(post.getUnit_price()) + "/-");
Price.setTextSize(40);
Price.setTextColor(Color.parseColor("#FF0000"));
lii.addView(Price);
TextView description = new TextView(this.getContext());
description.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
description.setText(post.getDescription());
description.setTextSize(15);
description.setTextColor(Color.parseColor("#000000"));
lii.addView(description);
ImageView image = new ImageView(this.getContext());
String image2 = post.getName().toString();
if (contains(image2, "1")){
image.setImageResource(R.drawable.sushiroll1);
} else if(contains(image2, "2")){
image.setImageResource(R.drawable.sushiroll2);
}else if(contains(image2, "3")){
image.setImageResource(R.drawable.sushiroll3);
}
lii.addView(image);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rootView;
}
public boolean contains( String haystack, String needle ) {
haystack = haystack == null ? "" : haystack;
needle = needle == null ? "" : needle;
return haystack.toLowerCase().contains( needle.toLowerCase() );
}
}