I
I
ibir2014-02-24 19:56:57
Android
ibir, 2014-02-24 19:56:57

ListView and transition to new activity: how?

There is a custom ListView:

<LinearLayout
   … >
    <ListView
        android:id="@+id/listView1" >
    </ListView>
</LinearLayout>

With cell:
<TextView
            android:id="@+id/text1" />
<TextView
            android:id="@+id/text2" />
<Button
            android:id="@+id/button1"/>

In the list, text1, text2 and button1 are displayed in 1 cell.
With content and adapter
public class Spisok extends Activity {

  private ArrayList<HashMap<String, Object>> Irr;
  private static final String Form1 = "form1"; // Форма1
  private static final String Form2 = "form2"; // Форма2
  private static final String Form3 = "form3"; // Форма3
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_spisok);
    setTitle("Irr");

    ListView listView = (ListView) findViewById(R.id.listView1);

    // создаем массив списков
    Irr = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> hm;

    hm = new HashMap<String, Object>();
    hm.put(Form1, "некоторый текст 1");
    hm.put(Form2, "некоторый текст 2");
    hm.put(Form3, "некоторый текст 3");
    Irr.add(hm);

    hm = new HashMap<String, Object>();
    hm.put(Form1, "некоторый текст 1");
    hm.put(Form2, "некоторый текст 2");
    hm.put(Form3, "некоторый текст 3");
    Irr.add(hm);
    
    SimpleAdapter adapter = new SimpleAdapter(this, Irr,
        R.layout.list_item, new String[] { Form1, Form2, Form3 },
        new int[] { R.id.text1, R.id.text2, R.id.text3 });
    listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<> arg0, View arg1, int position, long arg3) {
        Intent intent = new Intent (this, SecondActivity.class );
        intent.putExtra("key", "transferedText")
        startActivity(intent);
} });

It is necessary that when clicking on the Button (the cell name is button1), a new Activity is loaded containing "R.id.text3" from "form3" of exactly the cell, the button of which was clicked.
It remains to add - intent.putExtra

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ibir, 2014-03-22
@ibir

Learn wise men, figs who helped when they asked on 4 forums ...

listView.setOnItemClickListener(new OnItemClickListener() 
            {
            	public void onItemClick(AdapterView<?> parent, View view,  int position, long id) 
            	{
            		String key = ((TextView) view.findViewById(R.id.text1)).getText().toString();
            		Intent intent = new Intent(getApplicationContext(), Prob.class);
            		intent.putExtra(TAG_NAME, key);
            		startActivity(intent);
            		}
            	});
        }

Intent intent = getIntent();
    String Name = intent.getStringExtra(TAG_NAME);

This is how we extract the desired value in the right place, and even in the right order (I applied a filter, and the values ​​\u200b\u200bare lost).
Learn wise men, figs who helped when they asked on 4 forums ...

I
itspers, 2014-02-25
@itspers

Do not invent, there is no custom adapter or foliage.
If you had a custom adapter, you could just put a click on the button.
Try to hang a listener on the entire list, and see if it will send a copy of the button in the view (I never did this - but it suddenly works). If not, just write a full-fledged adapter.

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<> arg0, View arg1, int arg2,
                long arg3) {
            //возможно arg1 будет здесь кнопкой или текстом, хотя скорей всего всей вьюхой, но попробовать стоит

           //если прокатит - arg2 - номер в массиве
        }
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question