2014-12-19 56 views
0

我有列表视图从点击的列表视图加载特定的字符串?

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> 

细节: ListViewItem的与字符串数组 “运动” 的标题,并根据布局管理数组字符串。

和点击后打开singleviewitem列表视图,但我想出现从<string-array name = "Lari">采取等等<string-array name = "sport">

根据列表视图的话让例如我点击列表视图与运行标题会出来就是

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

请帮帮忙,

对不起,我的英语不好

注:我牛逼ried getstringarray的位置,但只提高字符串根据点击列表视图的位置

+0

可能重复[?如何从列表视图对象setOnItemClickListener在安卓(http://stackoverflow.com/问题/ 7073577 /如何获取对象从列表视图在setonitemclicklistener在Android) – 2014-12-19 01:46:32

回答

0

由于名誉我不能留下评论,但你可以简单地从选定的数组中获取文本并将其与任何名称进行比较。 如果我理解正确,你的问题是你不能从res /数组加载数组与同样的名称,单击列表视图中的项目。

我解决了它通过搜索数组点击的单词,如果匹配 - 接受它。 所有内部onItemClickListener 这里的行动。例如:

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); 
} 

然后你就可以通过这个意图阵列,并填写你的下一个listview。 我希望这是你想要的。

编辑:当你得到点击listitem的名字,你可能会在res/arrays中找到相同的数组。 之后,只需通过Intent传递该数组,然后使用Adapter填充此数组的下一个列表。

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

在接下来的活动中,你可以得到这个数组:

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

之后,你可以填写您的新名单。

如果你说你想第三个列表项,你可以抓住它里面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; 
    } 
+0

thx答复,我想要的是当前第三个listview被点击它会出现singleview的单词/串的字符串,数组基于最后三分之一的字符串(从字符串数组取得的listview中的字符串,我只在lightfragment中仅显示标题),无论如何,声誉是什么?对不起新手 – janobyl 2014-12-19 22:01:30

+0

@janobyl,我编辑答案,看看) – Yurets 2014-12-20 17:28:57

+0

你能解释一下我在哪个类放代码?和完整的代码,我迷惑 – janobyl 2014-12-21 06:39:18

相关问题