M
M
madm0nkey2015-05-03 16:26:26
Java
madm0nkey, 2015-05-03 16:26:26

How to switch to a new activity from a listView element populated via SimpleCursorAdapter?

Help to understand with such problem:
There is a listView which receives the data from a DB through SimpleCursorAdapter. By clicking on a list item, you need to switch to a new Activity, it doesn’t work for me ...
I also can’t google, everywhere there are examples either with ListActivity, or with ArrayAdapter, or only with logs, without switching to another activity.
Here you also need to make sure that the colors of the elements alternate. I think this can also be done with position, but I can't even try it yet.
Here is the activity code :

public class BasicoActivity extends ActionBarActivity  {
 
        ListView lv1;
    Cursor words;
    MyDatabase db;
    String LOG_TAG = "myLogs";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basico);
 
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
 
        lv1 = (ListView) findViewById(R.id.lv1);
        db = new MyDatabase(this);
        words = db.getWords();
        startManagingCursor(words);
 
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.item_lesson,
                words,
                new String[]{"word", "trans_w"},
                new int[] {R.id.espUnit, R.id.rusUnit});
        lv1.setAdapter(adapter);
 
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int position, long id) {
 
                Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
                        + id);
 
                //выход в новое Activity
                //Intent intent1 = new Intent(BasicoActivity.this, MainActivity.class);
                //startActivity(intent1);
                }
        });
 
    }

Here is the markup code for the list item item_lesson.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/espUnit"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rusUnit"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

activity_basico.xml markup file :
<?xml version="1.0" encoding="utf-8"?>
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lv1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".BasicoActivity"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris K., 2015-05-04
@kaftanati

The working example is almost unchanged.

lv1 = (ListView) rootView.findViewById(R.id.lvItems);
lv1.setClickable(true);

lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //  selected_position_list = lv1.getFirstVisiblePosition();
            //  long lv_id = parent.getItemIdAtPosition(position);

            Intent intent = new Intent(getActivity(), ***.class);

            if (activity != null) {
                activity.startActivity(getActivity());
            }

        }
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question