2011-03-16 17 views
7

मुझे डीबी से डेटा प्राप्त हो रहा है और ग्रिडव्यू ठीक में प्रदर्शित किया गया है। लेकिन मुझे प्रत्येक पाठ के नीचे एक अलग बटन डालने की ज़रूरत है। जब मैं बटन पर क्लिक करता हूं, तो मुझे कुछ सामान करना पड़ता है। यहाँ मैंने डीबी से पुनर्प्राप्त डेटा के लिए कस्टमलिस्ट एडाप्टर का उपयोग किया। मैं यह कैसे कर सकता हूं?एंड्रॉइड में ग्रिडव्यू के लिए कस्टम एडाप्टर

मेरे कोड ..

public class HomePage extends Activity { 
    private ArrayList<SingleElementDetails> allElementDetails=new ArrayList<SingleElementDetails>(); 
    DBAdapter db=new DBAdapter(this); 
    String category, description; 
    String data; 
    String data1; 
    GridView gridview; 
    Button menu; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.homepage); 

    menu=(Button)findViewById(R.id.menus); 




    menu.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) 
     { 
      gridview=(GridView)findViewById(R.id.gridview); 
      allElementDetails.clear(); 
      db.open(); 
      long id; 
      //id=db1.insertTitle1(category, description,r_photo); 
      Cursor cursor = db.getAllTitles1(); 
      while (cursor.moveToNext()) 
      { 
       SingleElementDetails single=new SingleElementDetails(); 
       single.setCateogry(cursor.getString(1)); 
       single.setDescription(cursor.getString(2)); 
       single.setImage(cursor.getBlob(3)); 
       allElementDetails.add(single); 

      } 
      db.close(); 
     CustomListAdapter adapter=new CustomListAdapter(HomePage.this,allElementDetails); 
     gridview.setAdapter(adapter); 

     } 
    }); 
    } 

}

मेरे customListAdapter ..

import java.io.ByteArrayInputStream; 
import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomListAdapter extends BaseAdapter { 
private ArrayList<SingleElementDetails> allElementDetails; 

private LayoutInflater mInflater; 

public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) { 
    allElementDetails = results; 
    mInflater = LayoutInflater.from(context); 
} 

public int getCount() { 
    return allElementDetails.size();   
} 

public Object getItem(int position) { 
    return allElementDetails.get(position); 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    convertView = mInflater.inflate(R.layout.listview1, null); 
    ImageView imageview = (ImageView) convertView.findViewById(R.id.image); 
    TextView textview = (TextView) convertView.findViewById(R.id.category_entry); 
    TextView textview1 = (TextView) convertView.findViewById(R.id.description_entry); 
    textview.setText(allElementDetails.get(position).getCategory()); 
    textview1.setText(allElementDetails.get(position).getDescription()); 

    byte[] byteimage=allElementDetails.get(position).getImage(); 
    ByteArrayInputStream imageStream = new ByteArrayInputStream(byteimage); 
    BitmapFactory.Options op=new BitmapFactory.Options(); 
    op.inSampleSize=12; 
    Bitmap theImage= BitmapFactory.decodeStream(imageStream,null,op); 
    imageview.setImageBitmap(theImage); 
    return convertView; 
}  

}

+0

कृपया आप 'R.layout.listview1' की xml फ़ाइल जोड़ने के लिए कोई आपत्ति होगी, तो मैं अपने उदाहरण से समझ सकते हैं, मैं एक ऐसी ही मामला है। –

उत्तर

8

इसके बजाय CustomListAdapter का उपयोग कर के, आप अपने खुद एडाप्टर बनाना पड़ेगा कि बेसएडाप्टर को बढ़ाता है, प्रत्येक ग्रिड आइटम (लीनारलाउट का विस्तार) के लिए एक लेआउट बनाएं एस क्लास में आपका टेक्स्टव्यू तब एक बटन है।

अच्छा टुट:

Custom GridView

+0

आपके उत्तर के लिए धन्यवाद..मैंने अपने कस्टमलिस्ट एडाप्टर कोड के साथ अपना प्रश्न अपडेट किया है..यह बेसएडाप्टर भी बढ़ाता है..यहाँ मैं उन बटन कहां जोड़ सकता हूं? – sanjay

+0

ग्रेट !! मैंने यह किया है .. ठीक काम कर रहा है .. बहुत धन्यवाद .. – sanjay

+1

एक लिंक में ट्यूटोरियल बहुत पुराना है - उन लोगों के लिए जिन्होंने मेरे 2011 के जवाब को मेरे जैसा नहीं देखा है;) – Srneczek

संबंधित मुद्दे