简体   繁体   中英

SimpleCursorAdapter in android

This is my first time creating android app. and what i want to create now is to make a list and when a user click a name in the list the detail of the click name will show. I am trying to create simple cursor adapter. But an error occur

The constructor SimpleCursorAdapter(QuestionsNew, int, Cursor, String[], int[]) is undefined.

What do you think is the possible reason for this. Below is some part of my code

package tk.wew.wew;

import info.androidhive.sqlite.model.LocalStorage;
import info.androidhive.sqlite.model.Message;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import static tk.askd.askd.CommonUtilities.*;

public class QuestionsNew extends Fragment {
    Cursor cursor;
    ListView nameList;
    protected ListAdapter adapter;
    // TextView tvCompany = (TextView)
    // findViewById(R.id.tvContactListCompany);
    private static final String TAG = "Question";

    private ListView list;

    private static List questions;

    ArrayList<String> senders = new ArrayList<String>(); 
    ArrayList<String> shortMsg = new ArrayList<String>();
    ArrayList<Integer> imageId = new ArrayList<Integer>();

    SimpleCursorAdapter myCursorAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;

        //CommonUtilities.sendAQuestion(getActivity(), "msg123Kjjj11", "richmund_10023", "lofrancs1231", "Apil ka?");

        questions = CommonUtilities.getAllQuestions(getActivity(), "N", null);

        if(questions.size() > 0){
            view = inflater.inflate(R.layout.questions_main_layout, container, false);
            AskdDatabaseHelper msg_db = new AskdDatabaseHelper(getActivity());

            List<Message> messages = msg_db.getAllQuestions("N", "lofrancs1231");


            Cursor cursor = msg_db.getAllQuestions3();

         // Setup mapping from cursor to view fields:
            String[] fromFieldNames = new String[] { "KEY_MSG_MESSAGE_ID","KEY_MSG_MESSAGE"  };
            int[] toViewIDs = new int[] { R.id.KEY_MSG_MESSAGE_ID, R.id.KEY_MSG_MESSAGE_ID };

           /* SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, // Context
                            R.layout.message_list_item, // Row layout template
                            cursor, // cursor (set of DB records to map)
                            fromFieldNames, // DB Column names
                            toViewIDs // View IDs to put information in
            );*/

           myCursorAdapter = new SimpleCursorAdapter(QuestionsNew.this,
                    R.layout.message_list_item,
                    cursor,
                    fromFieldNames,
                    toViewIDs);

            ListView contactList = (ListView) getView().findViewById(R.id.lvContacts);
            contactList.setAdapter(myCursorAdapter);


            for(int m=0; m<questions.size(); m++){
                senders.add(messages.get(m).getFromUser());
                shortMsg.add(messages.get(m).getMessage());
                imageId.add(R.drawable.ic_launcher);
            }

        } else {
            view = inflater.inflate(R.layout.fragments_question_new, container, false);
            ((TextView)view.findViewById(R.id.textView)).setText("No New Questions");
        }      

        return view;
    }

    @Override
    public void onStart(){
        super.onStart();

        Log.v(TAG, ": New onStart");

        if(questions.size() > 0){

            list = (ListView) getView().findViewById(R.id.list);

            CustomLst adapter = new CustomLst(getActivity(), senders, imageId, shortMsg);

            list.setAdapter(adapter);

            list.setOnItemClickListener(new OnItemClickListener(){

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

//                  Cursor listCursor = (Cursor) parent.getItemAtPosition(position);
//                  Cursor listCursor = (Cursor) parent.getAdapter().getItem(position);
//                  Log.d("TAG", (String) parent.getItemAtPosition(id));

                    Toast.makeText(getActivity(), 
                            "Position:"+ parent.getItemIdAtPosition((int) id), Toast.LENGTH_LONG)
                            .show();

//                  int message = listCursor.getInt(listCursor.getColumnIndex(AskdDatabaseHelper.KEY_MSG_MESSAGE_ID));
                    /*   
                Intent intent = new Intent(getActivity(), QuestionDetail.class);
                    intent.putExtra("Message_ID", listCursor.getString(listCursor.getColumnIndex(AskdDatabaseHelper.KEY_MSG_MESSAGE_ID)));
                    startActivity(intent);
                    // ListView clicked item index
                    int Position = position;

                    // ListView clicked item value
                    String itemValue = (String) list.getItemAtPosition(Position);

                    // show alert
//                  Toast.makeText(getActivity(), 
//                          "Position:" + Position + " Listitem:" + itemValue, Toast.LENGTH_LONG)
//                          .show();
*/                  
                }

            });
        } else {
            Log.e(TAG, "No new question");
        }

    }

}

Change your constructor as below

 myCursorAdapter = new SimpleCursorAdapter(getActivity(),
                R.layout.message_list_item,
                cursor,
                fromFieldNames,
                toViewIDs);

The first param is a Context . This QuestionsNew.this is not a valid context. Fragment does not extend Context .

Activity

java.lang.Object
   ↳    android.content.Context // check this
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity // check this

Fragment

java.lang.Object
   ↳    android.app.Fragment // check this

So get context in fragment use getActivity()

public final Activity getActivity ()

Added in API level 11 Return the Activity this fragment is currently associated with.

Also

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) This constructor was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

It is same when you use ArrayAdapter in Fragment class. The first param to the constructor of ArrayAdapter is a valid context. So even in that case you need to use getActivity() instead of FragmentName.this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM