mercredi 28 octobre 2015

when click particular list to be display toast content of name

Am created list view using array adapter when i click list item to be display toast

please find the error of the below code and suggest me the bugs. This the main code.`ArrayList imageArry = new ArrayList(); ContactImageAdapter adapter; Button next; ListView list; public DBCap mdb=null; public Cursor c=null; public SQLiteDatabase datab=null; public static final String DATABASE_NAME="CAP.db"; public static final int DATABASE_VERSION=1; public byte[] photo = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mdb=new DBCap(getApplicationContext(), DATABASE_NAME,null, DATABASE_VERSION);
    next=(Button)findViewById(R.id.button1);
    list=(ListView)findViewById(R.id.listView1);
    datab=mdb.getWritableDatabase();

    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(MainActivity.this,First.class);
            startActivity(i);
        }
    });

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View View , int position,
                long id) {
            // TODO Auto-generated method stub

            String m_name = c.getString(c.getColumnIndex("NAME"));
            photo=c.getBlob(c.getColumnIndex("PROFILE"));
            Toast.makeText(getApplicationContext(),"Selected MemberID : " + m_name, Toast.LENGTH_SHORT).show();
        }
    });

    // Reading all contacts from database
    List<Contact> contacts = mdb.getAllContacts();
    for (Contact cn : contacts) {
        String log = "ID:" + cn.getID() + " Name: " + cn.getName()
                + " ,Image: " + cn.getImage();

        // Writing Contacts to log
        Log.d("Result: ", log);
        //add contacts data in arrayList
        imageArry.add(cn);
    }
    adapter = new ContactImageAdapter(this, R.layout.parent,
            imageArry);
    //ListView dataList = (ListView) findViewById(R.id.list);
    list.setAdapter(adapter);
}

sqlite`private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "CAP.db"; private static final String TABLE_MEMBER = "demo"; public DBCap(Context context, String db, CursorFactory factory, int databversion) { super(context, db, factory, databversion); } @Override public void onCreate(SQLiteDatabase datab) { // TODO Auto-generated method stub datab.execSQL("CREATE TABLE demo(NAME VARCHAR,PROFILE BLOB);"); }

// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM demo ORDER BY name";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
//contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(0));
contact.setImage(cursor.getBlob(1));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// close inserting data from database
db.close();
// return contact list
return contactList;

}
@Override
public void onUpgrade(SQLiteDatabase datab, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

Array adapter

public class ContactImageAdapter  extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.textView1);
holder.imgIcon = (ImageView)row.findViewById(R.id.imageView1);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact picture = data.get(position);
holder.txtTitle.setText(picture ._name);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}

Aucun commentaire:

Enregistrer un commentaire