在动态生成的列表的行视图末尾添加静态按钮

时间:2016-05-13 07:29:46

标签: android listview

我们通过从共享首选项中获取值来创建列表视图。工作流程将是这样的。

  1. 客户登录应用程序并点击“收藏夹”标签。对于第一次,将出现带有切换开关的模态视图。
  2. 客户可以选择切换开关以添加他们喜欢的选项并保存。
  3. 在下次登录时,当客户点击“收藏夹”标签时,该列表将显示其之前选择的选项。
  4. 现在我们要在按钮处添加一个静态按钮(用于编辑),点击该按钮将显示带有切换开关页面的模态视图。
  5. 但是当我尝试在我的XML文件中添加按钮时,我会在每一行中获得按钮。你在这方面的帮助将非常明显。

    布局(XML文件)

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp"    
    
    android:layout_marginTop="15dp"
    >
    
    
    <ImageView
        android:id="@+id/logo"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
         >
    
    </ImageView>
    
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="14dp" >
    </TextView>  
    
    </LinearLayout>
    

    dummy.java

     import kvbank.kvb_epassbook.util.MobileArrayAdapter;
     import android.app.ListActivity;
     import android.os.Bundle;
     import android.widget.ArrayAdapter;
     import android.widget.ListView;
     import android.widget.Toast;
     import android.view.View;
     import android.widget.ListView;
     import android.os.Bundle;
     import android.support.v7.app.ActionBarActivity;
     import android.view.KeyEvent;
     import android.view.View;
     import android.widget.AdapterView;
     import android.widget.Button;
     import android.widget.EditText;
     import android.widget.ListView;
     import android.widget.TextView;
     import java.util.ArrayList;
     public class dummy extends  ListActivity {
     //String[] MOBILE_OS = new String[15] ;
     ArrayList<String> MOBILE_OS = new ArrayList<String>();
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        final SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(this);
    
        int count1 = sp.getInt("count", -1);
        String cc = String.valueOf(count1);
        System.out.println(cc);
    
        if((count1 == 0 ) || (count1==-1)){
            Intent i = new Intent(this, favorites.class);
            startActivity(i);
    
                Toast.makeText(getApplicationContext(), "No favorites Set!!! =)",
                           Toast.LENGTH_LONG).show();
        }
    
    
        else
        {
    
    
              SharedPreferences.Editor editor = sp.edit();
    
    boolean ad = sp.getBoolean("accdet", false);
    if(ad == true){
    MOBILE_OS.add("AccountDetails") ;   
     }
    boolean as =sp.getBoolean("accsumm", false);
    if(as == true){
    MOBILE_OS.add("AccountSummary") ;   
     }
    boolean ast =sp.getBoolean("accstmt", false);
    if(ast == true){
     MOBILE_OS.add("AccountStatement") ;    
     }
    boolean crl =sp.getBoolean("CRL", false);
    if(crl == true){
    MOBILE_OS.add("Cash Remitance Limit") ; 
     }
    boolean cse =sp.getBoolean("CSE", false);
    if(cse == true){
    MOBILE_OS.add("Cheque Status Enquiry") ;    
    }
     boolean ic =sp.getBoolean("inwchq", false);
    if(ic == true){
     MOBILE_OS.add("Inward Cheque") ;   
    }
    boolean uca =sp.getBoolean("Unclramt", false);
    if(uca == true){
    MOBILE_OS.add("uncleared Amount") ; 
    }
    boolean dt =sp.getBoolean("depdet", false);  
    if(dt == true)
    {
    MOBILE_OS.add("Deposit Details") ;  
    }    
    
    setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));    
    
        }
    
    }
    protected void onListItemClick(ListView l, View v, int position, long id) {
    
        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
    
    }   
    }
    

    MobileArrayAdapter

      import java.util.ArrayList;
      import kvbank.kvb_ebook.R;
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
       import android.view.ViewGroup;
       import android.widget.ArrayAdapter;
       import android.widget.ImageView;
        import android.widget.TextView;
    
    public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList<String> values;
    
    public MobileArrayAdapter(Context context, ArrayList<String> mOBILE_OS) {
        super(context, R.layout.list_mobile, mOBILE_OS);
        this.context = context;
        this.values = mOBILE_OS;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values.get(position));
    
        // Change icon based on name
        String s = values.get(position);
    
    
        if (s.equals("AccountDetails")) {
            imageView.setImageResource(R.drawable.acctinf);
        } else if (s.equals("AccountStatement")) {
            imageView.setImageResource(R.drawable.acctstat);
        } else if (s.equals("AccountSummary")) {
            imageView.setImageResource(R.drawable.acctsum);
        } else if(s.equals("Cash Remitance Limit")){
            imageView.setImageResource(R.drawable.cashret1);
        } else if(s.equals("Cheque Status Enquiry")){
            imageView.setImageResource(R.drawable.chqbkenq);
        } else if(s.equals("Inward Cheque")){
            imageView.setImageResource(R.drawable.inwchq);
        } else if(s.equals("uncleared Amount")){
            imageView.setImageResource(R.drawable.uncl1);
        } else if(s.equals("Deposit Details")){
            imageView.setImageResource(R.drawable.tddet);
        } else if(s.equals("Deposit Summary")){
            imageView.setImageResource(R.drawable.tdsum);
        } else if(s.equals("Deposit Statement")){
            imageView.setImageResource(R.drawable.dep_stmt);
        } else if(s.equals("Loan Details")){
            imageView.setImageResource(R.drawable.lndet);
        } else if(s.equals("Loan Summary")){
            imageView.setImageResource(R.drawable.lnsumm1);
        } else if(s.equals("Loan Statement")){
            imageView.setImageResource(R.drawable.lnsumm1);
        } else if(s.equals("Alert Maintenance")){
            imageView.setImageResource(R.drawable.alertreg);
        } else if (s.equals("Forex Rates")){
            imageView.setImageResource(R.drawable.forex1);
        }
        else if(s.equals("Cheque Book Request"))
        {
        imageView.setImageResource(R.drawable.chqreq)   ;
        }
    
    
    
        return rowView;
    }
    }
    

1 个答案:

答案 0 :(得分:1)

创建一个xml文件 dummy.xml 将以下代码粘贴到该文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<Button
    android:id="@+id/btn_static"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="STATIC"/>
</LinearLayout>

更改 dummy.java ,如下所示:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

import kvbank.kvb_epassbook.util.MobileArrayAdapter;

public class dummy extends Activity {
//String[] MOBILE_OS = new String[15] ;
ArrayList<String> MOBILE_OS = new ArrayList<String>();
Button btn_static;
ListView mylist;
MobileArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.dummy);

    mylist = (ListView)findViewById(R.id.mylist);

    //This is your Button use it any where you want..!!

    btn_static = (Button)findViewById(R.id.btn_static);

    final SharedPreferences sp = PreferenceManager
            .getDefaultSharedPreferences(this);

    int count1 = sp.getInt("count", -1);
    String cc = String.valueOf(count1);
    System.out.println(cc);

    if ((count1 == 0) || (count1 == -1)) {
        Intent i = new Intent(this, favorites.class);
        startActivity(i);

        Toast.makeText(getApplicationContext(), "No favorites Set!!! =)",
                Toast.LENGTH_LONG).show();
    } else {


        SharedPreferences.Editor editor = sp.edit();

        boolean ad = sp.getBoolean("accdet", false);
        if (ad == true) {
            MOBILE_OS.add("AccountDetails");
        }
        boolean as = sp.getBoolean("accsumm", false);
        if (as == true) {
            MOBILE_OS.add("AccountSummary");
        }
        boolean ast = sp.getBoolean("accstmt", false);
        if (ast == true) {
            MOBILE_OS.add("AccountStatement");
        }
        boolean crl = sp.getBoolean("CRL", false);
        if (crl == true) {
            MOBILE_OS.add("Cash Remitance Limit");
        }
        boolean cse = sp.getBoolean("CSE", false);
        if (cse == true) {
            MOBILE_OS.add("Cheque Status Enquiry");
        }
        boolean ic = sp.getBoolean("inwchq", false);
        if (ic == true) {
            MOBILE_OS.add("Inward Cheque");
        }
        boolean uca = sp.getBoolean("Unclramt", false);
        if (uca == true) {
            MOBILE_OS.add("uncleared Amount");
        }
        boolean dt = sp.getBoolean("depdet", false);
        if (dt == true) {
            MOBILE_OS.add("Deposit Details");
        }

        adapter = new MobileArrayAdapter(this, MOBILE_OS);

        mylist.setAdapter(adapter);

        mylist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                String selectedValue = (String) adapter.getItem(position);
                Toast.makeText(dummy.this, selectedValue, Toast.LENGTH_SHORT).show();

                return false;

            }
        });

    }

}
}