0

I have a ListView. It contains JSON I pulled from the web. I want each item in the ListView to go to the same activity and the same TextView. That's easy enough on the face of it.

However I want the TextView to show different items depending on what the user clicked on. In fact better make it two TextViews because one of them has a link. There's two lines of text, one is a link.

So if the user presses the first list item, they go to this activity and be presented with information_01. If the same user backs out and then goes to the second list item it should go to the very same activity and show information_02, and so on.

I'll show you what I have so far, it's nothing special:

title.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                  switch (position) {
                  case 0:
                      Driver p = (Driver) title.getItemAtPosition(position);
                      Intent i = new Intent(MainActivity.this, DataActivity.class);
                      i.putExtra("one_id", p.id);
                      i.putExtra("one_link", p.link);
                      MainActivity.this.startActivity(i);
                  case 1:
                      Driver p1 = (Driver) title.getItemAtPosition(position);
                      Intent i1 = new Intent(MainActivity.this, DataActivity.class);
                      i1.putExtra("two_id", p1.id);
                      i1.putExtra("two_link", p1.link);
                      MainActivity.this.startActivity(i1);
                  case 2:
                      break;
                  }

Hopefully this is possible because creating a million activities for each list item is a pain in the arse. Like I said one of them is a link which I need to send to another page on my app. So please bare in mind that showing it and clicking on it is needed.

Would great appreciate some help on the matter. Thanks in advance.

EDIT: My DataActivity.class

public class DataActivity extends Activity {

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

    //Case 0
    Intent intent0 = getIntent();
    String link0 = intent0.getStringExtra("one_link");
    String id0 = intent0.getStringExtra("one_id");

    TextView data0 = (TextView) findViewById(R.id.case0Text);
    data0.setText("ID: " + id0 + "\n\n Click here: " + link0);

    //Case 1
    Intent intent1 = getIntent();
    String id1 = intent1.getStringExtra("two_id");
    String link1 = intent1.getStringExtra("two_link");

    TextView data1 = (TextView) findViewById(R.id.case1Text);
    data1.setText("ID: " + id1 + "\n\n Click here: " + link1);
}
}
4

3 回答 3

0

像这样试试

 @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long arg3) {

            Driver driver = (Driver) parent.getAdapter().getItem(position);
                Intent intent = new Intent(MainActivity.this, DataActivity.class);
                intent.putExtra("one_id", driver.id);
                intent.putExtra("one_link", driver.link);
                MainActivity.this.startActivity(intent);

        }

希望这会奏效。

于 2013-10-15T14:55:21.413 回答
0

尝试这个

title.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {


                  Driver p = (Driver) title.getItemAtPosition(position);
                  Intent i = new Intent(MainActivity.this, DataActivity.class);
                  i.putExtra("id", p.id);
                  i.putExtra("link", p.link);

              }

数据活动类

  public class DataActivity extends Activity {

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

    //Case 0
    Intent intent0 = getIntent();
    String link0 = intent0.getStringExtra("link");
    String id0 = intent0.getStringExtra("id");

    TextView data0 = (TextView) findViewById(R.id.case0Text);
    data0.setText("ID: " + id0 + "\n\n Click here: " + link0);


}
}

您可以在 DataActivity 的 xml 中获取两个 textView,一个用于 id id,一个用于链接,并使链接的 textview 可点击,并在其上设置一个 clickListener。

于 2013-10-15T14:59:02.587 回答
0

你在哪里存储你的 json 数据?将其存储在 hashmap 中并根据 itemclick 方法的位置检索它,您不必做很多事情。

于 2013-10-15T14:47:42.577 回答