0

i have fragment with listview

myfragment :

public class LightFragment extends Fragment {
String[] name;
String[] type;
String[] swim;
int[] flag;
ListView list;
ListViewAdapter adapter;

public LightFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_light, container, false);

    name =  getResources().getStringArray(R.array.sport); 

    type = new String[] { "China", "India", "United States",
            "Indonesia", "Brazil" };

    swim = new String[] { "1,354,040,000", "1,210,193,422",
            "315,761,000", "237,641,326", "193,946,886" };

    flag = new int[] { R.drawable.light11,
            R.drawable.light12,             
            R.drawable.light21,
            R.drawable.light22,
            R.drawable.light23,
            R.drawable.light24,
            R.drawable.light25,
            R.drawable.light26,
            R.drawable.light27 };

    // Locate the ListView in fragmenttab1.xml
    list = (ListView) rootView.findViewById(R.id.listview);

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(getActivity(), name, type, swim,
            flag);
    // Binds the Adapter to the ListView
    list.setAdapter(adapter);
    // Capture clicks on ListView items
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // Send single item click data to SingleItemView Class
            Intent i = new Intent(getActivity(), SingleItemView.class);
            // Pass all data rank
            i.putExtra("rank", name);
            // Pass all data country
            i.putExtra("country", type);
            // Pass all data population
            i.putExtra("population", swim);
            // Pass all data flag
            i.putExtra("flag", flag);
            // Pass a single position
            i.putExtra("position", position);
            // Open SingleItemView.java Activity
            startActivity(i);
        }

    });

    return rootView;
}

}

Listviewadapter :

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
String[] rank;
String[] country;
String[] population;
int[] flag;
LayoutInflater inflater;

public ListViewAdapter(Context context, String[] rank, String[] country,
        String[] population, int[] flag) {
    this.context = context;
    this.rank = rank;
    this.country = country;
    this.population = population;
    this.flag = flag;
}

public int getCount() {
    return rank.length;
}

public Object getItem(int position) {
    return null;
}

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

public View getView(int position, View convertView, ViewGroup parent) {

    // Declare Variables
    TextView txtrank;
    TextView txtcountry;
    TextView txtpopulation;
    ImageView imgflag;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);

    // Locate the TextViews in listview_item.xml
    txtrank = (TextView) itemView.findViewById(R.id.name);
    txtcountry = (TextView) itemView.findViewById(R.id.type);
    txtpopulation = (TextView) itemView.findViewById(R.id.swim);
    // Locate the ImageView in listview_item.xml
    imgflag = (ImageView) itemView.findViewById(R.id.flag);

    // Capture position and set to the TextViews
    txtrank.setText(rank[position]);
    txtcountry.setText(country[position]);
    txtpopulation.setText(population[position]);

    // Capture position and set to the ImageView
    imgflag.setImageResource(flag[position]);

    return itemView;
}

}

mysingleviewitem :

public class SingleItemView extends Activity {

TextView txtname;
TextView txttype;
TextView txtswim;
TextView txtfunny;
TextView txtwalking;
TextView txtangry;
TextView txtsad;
ImageView imgflag;
String[] name;
String[] type;
String[] swim;
String[] funny;
String[] walking;
String[] angry;
String[] sad;
int[] flag;
int position;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitemview);

    Intent i = getIntent();
    // Get a single position
    position = i.getExtras().getInt("position");
    // Get the list of rank
    name = i.getStringArrayExtra("rank");
    // Get the list of country
    type = i.getStringArrayExtra("country");
    // Get the list of population
    swim = i.getStringArrayExtra("population");

    funny = i.getStringArrayExtra("rank");
    // Get the list of country
    walking = i.getStringArrayExtra("country");
    // Get the list of population
    angry = i.getStringArrayExtra("population");

    sad = i.getStringArrayExtra("rank");
    // Get the list of flag
    flag = i.getIntArrayExtra("flag");

    // Locate the TextViews in singleitemview.xml
    txtname = (TextView) findViewById(R.id.name);
    txttype = (TextView) findViewById(R.id.type);
    txtswim = (TextView) findViewById(R.id.swim);
    txtfunny = (TextView) findViewById(R.id.funny);
    txtwalking = (TextView) findViewById(R.id.walking);
    txtangry = (TextView) findViewById(R.id.angry);
    txtsad = (TextView) findViewById(R.id.sad);

    // Locate the ImageView in singleitemview.xml
    imgflag = (ImageView) findViewById(R.id.flag);

    // Load the text into the TextViews followed by the position
    txtname.setText(name[position]);
    txttype.setText(type[position]);
    txtswim.setText(swim[position]);
    txtfunny.setText(funny[position]);
    txtwalking.setText(walking[position]);
    txtangry.setText(angry[position]);
    txtsad.setText(sad[position]);

    // Load the image into the ImageView followed by the position
    imgflag.setImageResource(flag[position]);
}

}

myarray :

<string-array name="sport">
    <item >Lari</item>
    <item >Bola</item>
    <item >Balap</item>
    <item >tinju</item>
    <item >renang</item>
</string-array>


<string-array name="Lari">
    <item >100 meter</item>
    <item >mulai</item>
    <item >finish</item>
    <item >piala</item>
</string-array>

detail: ListViewItem with the title of the string array "sport" and managed according to the layout of the array string.

and when clicked will open singleviewitem listview but I want the words that appear taken from <string-array name = "Lari"> and so on according listview of <string-array name = "sport">

so for example I click listview with Running title will be out is

<item >100 meter</item>
<item >mulai</item>
<item >finish</item>
<item >piala</item>

Please help,

sorry, my english is bad

nb: I've tried getstringarray the position but only raises the string according to the position of the clicked listview

janobyl
  • 3
  • 1
  • possible duplicate of [how to get object from listview in setOnItemClickListener in android?](http://stackoverflow.com/questions/7073577/how-to-get-object-from-listview-in-setonitemclicklistener-in-android) – Nikola Despotoski Dec 19 '14 at 01:46

1 Answers1

0

Because of reputation I cannot leave a comment, but you can simply get text from chosen array and compare it to any name. If I understood correctly your problem is you cannot load array from res/arrays with same name that clicked item in listview has.

I solved it by searching in array clicked word and if matched - took it. All actions here inside onItemClickListener For example:

String[] myString = null;
String text = parent.getItemAtPosition(position).toString();

if(text.equalsIgnoreCase("Lari")){
    myString = getResources().getStringArray(R.array.lari);
}else if(text.equalsIgnoreCase("sport")){
    myString = getResources().getStringArray(R.array.sport);
}

And then you may pass this array through intent and fill your next listview. I hope this is what you want.

EDIT: When you got the name of clicked listitem, you may find same array in res/arrays. After that just pass this array through Intent and fill next list with this array using Adapter.

Intent intent = new Intent(this, YourNextActivity.class);
intent.putExtra("justNameForArray", mString);

In next activity you can get this array:

Intent intent = getIntent();
String[] arrayFromPreviousClass = intent.getStringArrayExtra("justNameForArray");

And after that you may fill out your new list.

If you say you wanna third listitem, you may catch it inside onItemClickListener:

    switch(position){
        case 0:
            //do smthg;
        break;
        case 1:
           //do smthg;
        break;
        case 2:
        //here you may get your string array and pass it through intent to another activity with another view
        break;
   }
Yurets
  • 3,999
  • 17
  • 54
  • 74
  • thx for reply,what I want is currently third listview is clicked it will appear singleview with the words / strings of string-array based on a string in the last third (string on listview taken from the string-array I made only show titles only in lightfragment),anyway, what is reputation? sorry newbie – janobyl Dec 19 '14 at 22:01
  • can you explained in what class i put code ? and full code, i confused – janobyl Dec 21 '14 at 06:39
  • @janobul, inside of method `OnItemClickListener` use switch-case (the latest code in my answer). I'm sorry, but I cannot write whole code for youm because of few things: 1. I don't know whole process of your application; 2. You're learning. Even if I find time and spend it for you, you will not get more knowledge, but only copy/paste. – Yurets Dec 21 '14 at 13:01