lundi 29 février 2016

i have to display contents in sqlite database on listview but it it not displaying

** Here i want to display the contents** package com.example.datewithme; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener;

public class Welcome extends Activity{
Intent i=getIntent();
TextView tv;
ListView lv;
SQLiteDatabase db=null;
int k=0;
private ArrayList<String> Questions = new ArrayList<String>();
private DBHelper mHelper;
private SQLiteDatabase dataBase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        lv = (ListView) findViewById(R.id.listView1);

        /*String[] values = new String[100] ;
        DBHelper db = new DBHelper(getApplicationContext());
        Cursor cs= db.getQuestions();
        k=0;

        while(cs.moveToNext())
        {
            String q = cs.getString(0);
            values[k++]=q;

        }
        try{
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_list_item_1, android.R.id.text1, values);


                // Assign adapter to ListView
                lv.setAdapter(adapter); 

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

                       // ListView Clicked item index
                       int itemPosition     = position;

                       // ListView Clicked item value
                       String  itemValue    = (String) lv.getItemAtPosition(position);

                        // Show Alert 
                        Toast.makeText(getApplicationContext(),
                          "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                          .show();

                      }


                    }); 
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }*/

        tv=(TextView)findViewById(R.id.user);
        //db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
        String newString;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                newString= null;
            } else {
                newString= extras.getString("name");
            }
        } else {
            newString= (String) savedInstanceState.getSerializable("name");
        }
        tv.setText("Hello"+newString);

    }
         @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
            }
             public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.item1: 
                     startActivity(new Intent(Welcome.this, TimeLine.class));
                    return true;
                case R.id.item2: 
                   startActivity(new Intent(Welcome.this, About.class));
                    return true;
                case R.id.item3: 
                    Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show(); 
                    return true;            
                case R.id.item4: 
                    this.finish();
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    return true;

                default:
                    return super.onOptionsItemSelected(item);
                }

            }
             private void displayData() {
                    dataBase = mHelper.getWritableDatabase();
                    Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
                            + DBHelper.TABLE_NAME, null);

                    Questions.clear();
                    if (mCursor.moveToFirst()) {
                        do {
                            Questions.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.KEY_NAME)));


                        } while (mCursor.moveToNext());
                    }
                    DisplayAdapter disadpt = new DisplayAdapter(Welcome.this,Questions);
                    lv.setAdapter(disadpt);
                    mCursor.close();
                }
        }

> Blockquote
Here is my display adapter

package com.example.datewithme;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * adapter to populate listview with data
 * @author ketan(Visit my <a
 *         href="http://ift.tt/LC4ev7">blog</a>)
 */
public class DisplayAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> questions;


    public DisplayAdapter(Context c, ArrayList<String> questions) { 
        this.mContext = c;

        this.questions = questions;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return questions.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int pos, View child, ViewGroup parent) {
        Holder mHolder;
        LayoutInflater layoutInflater;
        if (child == null) {
            layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            child = layoutInflater.inflate(R.layout.list, null);
            mHolder = new Holder();
            mHolder.txt_id = (TextView) child.findViewById(R.id.tv_que);
            child.setTag(mHolder);
        } else {
            mHolder = (Holder) child.getTag();
        }
        mHolder.txt_id.setText(questions.get(pos));

        return child;
    }

    public class Holder {
        TextView txt_id;
    }

}

Aucun commentaire:

Enregistrer un commentaire