首页 文章

我应该在哪里实现点击功能帮助我代码[关闭]

提问于
浏览
0
public class ListViewMainActivity extends ListActivity {

// declare class variables
private ArrayList<Item> m_parts = new ArrayList<Item>();
private Runnable viewParts;
private ItemAdapter m_adapter;
Dbhelper mydb =new Dbhelper(this);
ArrayList<Item> abc = new ArrayList<Item>();;


/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // instantiate our ItemAdapter class
    m_adapter = new ItemAdapter(this, R.layout.list_item, abc);
    setListAdapter(m_adapter);

    // here we are defining our runnable thread.
    viewParts = new Runnable(){
        public void run(){
            handler.sendEmptyMessage(0);
        }
    };

    // here we call the thread we just defined - it is sent to the handler below.
    Thread thread =  new Thread(null, viewParts, "MagentoBackground");
    thread.start();
}

private Handler handler = new Handler()
 {
    public void handleMessage(Message msg)
    {
        // create some objects
        // here is where you could also request data from a server
        // and then create objects from that data.

        abc = mydb.getweek();
        m_adapter = new ItemAdapter(ListViewMainActivity.this, R.layout.list_item, abc);

    // display the list.
    setListAdapter(m_adapter);
    }
};

}

适配器代码:

package com.example.myexpenses;

import java.util.ArrayList;

公共类ItemAdapter扩展ArrayAdapter {

// declaring our ArrayList of items
private ArrayList<Item> objects;
Context context;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public ItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.objects = objects;
}

/*
 * we are overriding the getView method here - this is what defines how each
 * list item will look.
 */
public View getView(int position, View convertView, ViewGroup parent){

    // assign the view we are converting to a local variable
    View v = convertView;

    // first check to see if the view is null. if so, we have to inflate it.
    // to inflate it basically means to render, or show, the view.
    if (v == null)

{LayoutInflater inflater =(LayoutInflater)getContext() . getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.list_item,null); }

Item i = objects.get(position);

    if (i != null) {

        // This is how you obtain a reference to the TextViews.
        // These TextViews are created in the XML files we defined.

        TextView ttd = (TextView) v.findViewById(R.id.toptextdata);

        TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);

        // check to see if each individual textview is null.
        // if not, assign some text!

        if (ttd != null){
            ttd.setText(i.getName());
        }

        if (mtd != null){
            mtd.setText("$" + i.getPrice());
        }

        if (bt != null){
            bt.setText(i.getDetails());
        }
    }

    // the view must be returned to our activity
    return v;

}

}

1 回答

  • 0

    有很多选择来做这件事 . 我会给你两个,他们很容易 .

    • yourListView.setOnItemClickListener() Link-1

    • public class YourListActivity extends ListActivity implements OnItemClickListener { } Link-2

    祝好运 .

相关问题