Karthick M
Karthick M

Reputation: 779

Button in each row of my dynamic listview is not working?

I have using data in db and showing in listview for each row i have given an sms button to go to the smsactivity,but while clicking the sms button its not going to anyactiviyt,it simply there,not showing any error or logcat error also

this is my activity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item2);
        mDbHelper = new GinfyDbAdapter(this);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());


    }


    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    private void fillData() {
        mDbHelper.open();      
        Cursor projectsCursor = mDbHelper.fetchAllProjects();
        //startManagingCursor(projectsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE,GinfyDbAdapter.CATEGORY_COLUMN_CONTENT,GinfyDbAdapter.CATEGORY_COLUMN_COUNT};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text2,R.id.text1,R.id.count};

        dataAdapter  = new SimpleCursorAdapter(
                this, R.layout.activity_row2,
                projectsCursor,
                from,
                to,
                0);
        setListAdapter(dataAdapter);



        EditText myFilter = (EditText) findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                dataAdapter.getFilter().filter(s.toString());
            }
        });

        dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return mDbHelper.fetchProjectByName(constraint.toString());
            }
        });





        tts = new TextToSpeech(this, this);
        final ListView lv = getListView();
        txtText = (TextView) findViewById(R.id.text1);
        lv.setTextFilterEnabled(true);



    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                //btnaudioprayer.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.activity_main1, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        createProject();
        return super.onMenuItemSelected(featureId, item);
    }


    private void createProject() {
        Intent i = new Intent(this, AddyourprayerActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);  
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }


    private void speakOut() {

        // String text = txtText.getText().toString();
        // String text = "Android speech";

        tts.speak(typed, TextToSpeech.QUEUE_FLUSH, null);
    }

    class CustomAdapter extends SimpleCursorAdapter {

        private LayoutInflater mInflater;

        @SuppressWarnings("deprecation")
        public CustomAdapter(Context context, int layout, Cursor c, String[] from,
                int[] to) {
            super(context, layout, c, from, to);
            // TODO Auto-generated constructor stub
            mInflater= LayoutInflater.from(context);
        }



        public View getView(final int position, View convertView, ViewGroup parent, Cursor cursor)
        {

            ViewHolder holder;

            if(convertView==null){
                convertView= mInflater.inflate(R.layout.activity_row2, null);

                convertView = inflater.inflate(R.layout.activity_row2, parent, false);
                holder = new ViewHolder();
                holder.tv = (TextView) convertView.findViewById(R.id.text1);
                holder.tv1 = (TextView) convertView.findViewById(R.id.text2);
                holder.buttonsms = (Button) convertView.findViewById(R.id.buttonsms);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            int col1 = cursor.getColumnIndex("title");
            final String title = cursor.getString(col1 );
            int col2 = cursor.getColumnIndex("content");
            final String content = cursor.getString(col2 );

            holder.tv.setText( title);
            holder.tv1.setText( content);


            holder.buttonsms.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    StringBuffer sb2 = new StringBuffer();
                    sb2.append("Title:");
                    sb2.append(Html.fromHtml(title));
                    sb2.append(",Content:");
                    sb2.append(Html.fromHtml(content));
                    sb2.append("\n");
                    String strContactList1 = (sb2.toString().trim());
                    sendsmsdata(strContactList1);

                }

            });

            // bindView(v,context,cursor);
            return convertView;
        }

        public class ViewHolder {
            TextView tv,tv1;
            Button buttonsms;
        }
    }




    public void sendsmsdata(String strContactList1) {

        Intent intent3=new Intent(YourPrayerActivity.this,SendSMSActivity.class);

        intent3.putExtra("firstKeyName", strContactList1);
        startActivity(intent3);

    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }



}

I am using simpleadapter for fetching data from db,after that for button function i am using customadapter to fetch data from listview,class customadapter extper SimpleCursorAdapter

Here i mention my xml file also.

 <LinearLayout
                    android:id="@+id/Share"
                    android:layout_width="0.0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1.0"
                    android:background="@drawable/button_new_feed"
                    android:orientation="horizontal" >


                    <Button
                        android:id="@+id/buttonsms"
                         android:layout_width="25.0dip"
                        android:layout_height="fill_parent"
                        android:layout_marginLeft="4.0dip"
                        android:src="@drawable/sms" />


                    <ImageButton
                        android:id="@+id/mail"
                        android:layout_width="25.0dip"
                        android:layout_height="fill_parent"
                        android:layout_marginLeft="6.0dip"
                        android:src="@drawable/mail" />

                </LinearLayout>

Upvotes: 1

Views: 160

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

It was hard to find out what went wrong

You have this and CustomAdapter. You have button inside the activity_row2 inflated for your row items.

    dataAdapter  = new SimpleCursorAdapter(
            this, R.layout.activity_row2,
            projectsCursor,
            from,
            to,
            0);

You have defined a CustomAdapter but never use it.

String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE,GinfyDbAdapter.CATEGORY_COLUMN_CONTENT,GinfyDbAdapter.CATEGORY_COLUMN_COUNT};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text2,R.id.text1,R.id.count};
        dataAdapter = new CustomAdapter (YourPrayerActivity .this, R.layout.activity_row2, projectsCursor, from, to);

Also Change your CustomAdapter as below

class CustomAdapter extends SimpleCursorAdapter {

        private LayoutInflater mInflater;


        @SuppressWarnings("deprecation")
        public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
                 {
            super(context, layout, c, from, to);
            mInflater= LayoutInflater.from(context);
            Toast.makeText(YourPrayerActivity.this, "text", 1000).show();
            // TODO Auto-generated constructor stub
        }


         @Override
            public void bindView(View view, Context context, final Cursor cursor){
                int row_id = cursor.getColumnIndex("_id");  //Your row id (might need to replace)
                TextView tv = (TextView) view.findViewById(R.id.text1);
                final TextView tv1 = (TextView) view.findViewById(R.id.text2);
                int col1 = cursor.getColumnIndex("title");
                String title = cursor.getString(col1 );
                int col2 = cursor.getColumnIndex("content");
                final String content = cursor.getString(col2 );
                tv.setText( title);
                tv1.setText( content);
                Button button = (Button) view.findViewById(R.id.buttonsms);
                button.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v){
                        Log.i("....................",""+cursor.getCount());
                        Toast.makeText(YourPrayerActivity.this, "text",400000).show();
                    }
                });
            }

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent){
                LayoutInflater inflater = LayoutInflater.from(context);
                View v = inflater.inflate(R.layout.activity_row2, parent, false);

                bindView(v,context,cursor);
                return v;
            }
        }

Upvotes: 1

vs.thaakur
vs.thaakur

Reputation: 619

buttonSMS.setFocusable(false);

set this property

Upvotes: 1

Related Questions