为什么我从自定义ListView获取第一项?

时间:2016-03-13 12:19:02

标签: android listview

我有一个自定义列表视图,并尝试将所选项目的文本传递给另一个活动。

我在自定义适配器中创建了公共字符串,以便从另一个活动中获取此字符串。

我的自定义适配器

public class OzelAdaptor extends BaseAdapter {

LayoutInflater layoutInflater;
List<Tactics> list;
Activity activity;
public static String aciklama;
public static String baslik;
public static String özellik;

public OzelAdaptor(Activity activity,List<Tactics> mList){

    layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    list= mList;

    this.activity=activity;
}

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View satirView;

    satirView=layoutInflater.inflate(R.layout.satir,null);

    TextView tv1=(TextView) satirView.findViewById(R.id.isim);
    TextView tv2=(TextView) satirView.findViewById(R.id.fiyat);

    Tactics icecek = list.get(position);

    tv1.setText(icecek.getIsim().toString());
    tv2.setText(icecek.getFiyat().toString());

    final String ismi = icecek.getIsim().toString();

    OzelAdaptor.aciklama=icecek.getAciklama().toString();
    OzelAdaptor.baslik=icecek.getIsim().toString();
    OzelAdaptor.özellik=icecek.getFiyat().toString();

    return satirView;
}
}

这些是我的列表文本。

     iceceklerim.add(new Tactics("A Lots Of Unit", "Average: 2.9","a text"));
  iceceklerim.add(new Tactics("First Crown", "Average: 4.5","a text"));
  iceceklerim.add(new Tactics("With Mercy", "Average: 4.8","another text"));
  iceceklerim.add(new Tactics("Goblin Barrel Using", "Basic Tactic","different text"));

我的另一项活动

public class TacticActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tactic);

    String aciklama=OzelAdaptor.aciklama;
    String baslik=OzelAdaptor.baslik;
    String özellik=OzelAdaptor.özellik;

    TextView tBaslik = (TextView) findViewById(R.id.baslik);
    TextView tÖzellik  = (TextView) findViewById(R.id.özellik);
    TextView tAciklama = (TextView) findViewById(R.id.aciklama);

    String deneme= MainActivity.deneme;

    tBaslik.setText(baslik);
    tÖzellik.setText(özellik);
    tAciklama.setText(aciklama);


}
}

我想从每个项目的所选列表视图中获取所有文本的文本。

实施例;当我点击这个项iceceklerim.add(new Tactics("first", "second","third"));时,我希望每个字符串得到3个文本。

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式传递selectedItem:
在mainActivity(firstActivity)中,您可以通过这种方式在OnItemClickListener中选择项目:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        case R.id.point_list:
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);

        String item= parent.getItemAtPosition(position)
        intent.putExtra("item", item);
        startActivity(intent);


}

并在SecondActivity中:

public class TacticActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tactic);
    Intent intent = getIntent();
    String item = intent.getStringExtras("item");
    List<String> mylist = new ArrayList<String>(Arrays.asList(item.split(",")));

项目就是你想要的。