0

I've four textviews and a button in listview. when user clicks on the button i want to get the value of these four textviews and pass to the database. I'm confuse how to save the data of selected item to sqlite.

public class PlannningListViewAdapter extends BaseAdapter{

    Context context;
    public ArrayList<Planning> planArraylist;
    private static LayoutInflater inflater = null;
    public Activity planActivity;

    public PlannningListViewAdapter(Context context,
            ArrayList<Planning> planArraylist) {
        this.context = context;
        this.planArraylist = planArraylist;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

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

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

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

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

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.single_lv_item, null);
        }

        Planning p = planArraylist.get(position);

        TextView tvDocCode = (TextView) convertView.findViewById(R.id.Plan_no);
        TextView tvDocName = (TextView) convertView.findViewById(R.id.doc_name);
        TextView tvmon = (TextView) convertView.findViewById(R.id.mon);
        TextView tvAdr = (TextView) convertView.findViewById(R.id.adr);
        Button   btn_save =(Button)convertView.findViewById(R.id.button1);
        btn_save.setOnClickListener(ButtonClickListener);


        tvDocCode.setText(p.getDocCode());
        tvDocName.setText(p.getDocName());
        tvmon.setText(p.getTerrCode());
        tvAdr.setText(p.getAdr());
        return convertView;
    }
    private OnClickListener ButtonClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
           // Log.v(TAG, "item clicked, row %d", position);
        }
    };

}
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Andrain
  • 872
  • 1
  • 16
  • 43
  • What is the problem you are facing? I can't see any code where you are trying to save the data into DB. – Rohit5k2 Feb 01 '15 at 18:42

1 Answers1

2

You can use setTag()and getTag() method on your button.

Please look at the code below:

You have to change getItem method:

@Override
public Planning getItem(int arg0) {
    return planArraylist.get(arg0);
}

Update your getView method:

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

    ...

    Button   btn_save =(Button)convertView.findViewById(R.id.button1);
    btn_save.setTag(position);

    ...
}


private OnClickListener ButtonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        final int position = (int)v.getTag();
        Planning planning = getItem(position);
        yourDatabaseHelper. getRuntimeExceptionDao(Planning.class).createOrUpdate(planning);
    }
};

One more thing. You should implement ViewHolder please read about this pattern, and the best solution for you is use RecyclerView, getting position is already implemented there in RecyclerView.ViewHolder.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
  • Thanks so much. could you please show me how can i pass value to database – Andrain Feb 01 '15 at 18:40
  • Do you use some library to write it to Db or just DatabaseHelper? – Konrad Krakowiak Feb 01 '15 at 18:42
  • i'm using databasehelper and i know the method of insertion, but i'm not getting how to get text values using position – Andrain Feb 01 '15 at 18:50
  • Which text values do you mean? I thing that you want to insert your object. – Konrad Krakowiak Feb 01 '15 at 18:52
  • If you want to get values from Planning object. You have to get it from array by getItem position method as in my post and inset it to db – Konrad Krakowiak Feb 01 '15 at 18:54
  • I'm glad that I could help you. Thanks. – Konrad Krakowiak Feb 01 '15 at 18:55
  • konrad i've not tested it yet in my project. can i ask you if i face any problem? – Andrain Feb 01 '15 at 18:58
  • Create gist with your code https://gist.github.com/ or public your problem on stack in different topic and give me link I will help you – Konrad Krakowiak Feb 06 '15 at 13:29
  • And ping me by comment on comment – Konrad Krakowiak Feb 06 '15 at 13:29
  • i asked a question here. please see and thanks in advance. http://stackoverflow.com/questions/28382479/set-progress-dialog-in-base-adapter – Andrain Feb 07 '15 at 13:08
  • Hi I saw that someone helped you :) do you need more help? – Konrad Krakowiak Feb 07 '15 at 19:08
  • Konrad those answers did not solve my problem, i'm getting the same old exception if you could see it otherwise no problem. – Andrain Feb 09 '15 at 07:34
  • Could you attach your log? – Konrad Krakowiak Feb 09 '15 at 07:46
  • i've readded log. please check on this link. http://stackoverflow.com/questions/28382479/set-the-progress-dialog-in-base-adapter – Andrain Feb 09 '15 at 08:08
  • The problem is not with db saving. It is with the display dialog - so lets go to next your topic. :) http://stackoverflow.com/questions/28382479/set-the-progress-dialog-in-base-adapter – Konrad Krakowiak Feb 09 '15 at 08:21
  • hi konrad. dear i have a ques could you please take a look to it. – Andrain Apr 10 '15 at 04:55
  • http://stackoverflow.com/questions/29553967/splash-screen-get-stuck-and-never-get-passed – Andrain Apr 10 '15 at 05:25
  • Konrad please take a look at this question, I'm not getting any solution http://stackoverflow.com/questions/31108269/android-count-and-delete-rows-from-sqlite?noredirect=1#comment50232251_31108269 – Andrain Jun 29 '15 at 06:37
  • Konrad sorry for disturbing you I'm not getting any response. I would appreciate if you look into my problem at this link http://stackoverflow.com/questions/31129835/how-to-get-time-from-network-in-android?noredirect=1#comment50271678_31129835 – Andrain Jun 30 '15 at 05:21
  • @Anita I saw that someone helped you. Is this solution right to you? – Konrad Krakowiak Jun 30 '15 at 06:43
  • hi Konrad. dear I have a task, please advice me how to achieve it http://stackoverflow.com/questions/31414398/how-to-get-the-content-of-listview-with-a-button-outside-the-list/31415932#31415932 – Andrain Jul 16 '15 at 08:40
  • hi Konrad, hope you are doing great. sorry for disturbing you but I've a serious problem and i did not get any response on stack, i would be really great full if you see my post http://stackoverflow.com/questions/32687463/android-app-sends-duplicate-records-to-server-through-restful-webservice?noredirect=1#comment53219487_32687463 – Andrain Sep 22 '15 at 07:41
  • dear i'm waiting please let me know if you could help me. – Andrain Sep 22 '15 at 07:41
  • @Andrain I am sorry but I am very busy now. I can promise nothing to you. I am sorry – Konrad Krakowiak Sep 22 '15 at 07:43
  • Its ok Konrad, you can see when you get free time, I can wait. I'am facing this problem from a month but I did not get the root cause,you are the only one experienced person I know.. – Andrain Sep 22 '15 at 07:50