我的自定义listview活动性明智持有人按钮出现问题,点击

时间:2019-03-21 04:29:35

标签: java android listview

实际上,我正在尝试使用自定义列表视图制作应用程序。

在此应用程序中,当主要活动将初始化时,将出现一个列表视图, 然后,当用户单击列表项然后明智地定位时,将打开不同的活动...然后在不同的活动中将出现相同的新listView,到目前为止,我已经完成了。

但是当单击列表项形成不同的活动之后,该活动将打开另一个活动,但每个主要活动位置都有相同的活动。

**

现在请帮助我,如何根据活动明智地控制holder.ListButton ...

**

这是我的CustomBaseAdapter源代码...

    package com.sbappstore.romjanbanglacalendar;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;

import com.blogspot.atifsoftwares.animatoolib.Animatoo;

import java.lang.annotation.Target;

public class CustomBaseAdapter extends BaseAdapter {

    String[] Name_of_District;
    Context context;
    private static LayoutInflater inflater = null;

    //Constructor
    public CustomBaseAdapter(MainActivity mainActivity, String[] Names_of_District){

        Name_of_District = Names_of_District;
        context = mainActivity;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(DhakaDistrict dhakaDistrict, String[] dhakaDistrictNames) {
        Name_of_District = dhakaDistrictNames;
        context = dhakaDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(BorishalDistrict borishalDistrict, String[] borishaDistrictNames) {
        Name_of_District = borishaDistrictNames;
        context = borishalDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(ChattogramDistrict chattogramDistrict, String[] chattogramDistrictNames) {
        Name_of_District = chattogramDistrictNames;
        context = chattogramDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(KhulnaDistrict khulnaDistrict, String[] khulnaDistrictNames) {
        Name_of_District = khulnaDistrictNames;
        context = khulnaDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(MoymonsinghDistrict moymonsinghDistrict, String[] moymonsinghDistrictNames) {
        Name_of_District = moymonsinghDistrictNames;
        context = moymonsinghDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(ProyojoniyoDua proyojoniyoDua, String[] dua) {
        Name_of_District = dua;
        context = proyojoniyoDua;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(QuranHadith quranHadith, String[] hadithQuran) {
        Name_of_District = hadithQuran;
        context = quranHadith;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(RajshahiDistrict rajshahiDistrict, String[] rajshahiDistrictNames) {
        Name_of_District = rajshahiDistrictNames;
        context = rajshahiDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(RangporDistrict rangporDistrict, String[] rongpurDistrictNames) {
        Name_of_District = rongpurDistrictNames;
        context = rangporDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(RomjanerKhaddavash romjanerKhaddavash, String[] khaddavvash) {
        Name_of_District = khaddavvash;
        context = romjanerKhaddavash;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(SylhetDistrict sylhetDistrict, String[] sylhetDistrictNames) {
        Name_of_District = sylhetDistrictNames;
        context = sylhetDistrict;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //Constructor
    public CustomBaseAdapter(ArticleMasyala articleMasyala, String[] masayla) {
        Name_of_District = masayla;
        context = articleMasyala;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return Name_of_District.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public class MyHolder{
        Button ListButton;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final MyHolder holder = new MyHolder();
        final View view;

        view = inflater.inflate(R.layout.custom_list, null);
        holder.ListButton = (Button) view.findViewById(R.id.ListButton);
        holder.ListButton.setText(Name_of_District[position]);


        holder.ListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View customView) {
               int id = customView.getId();

                if (position==0) {
                    //Toast.makeText(context,"Position 0", Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context,DhakaDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==1) {
                    context.startActivity(new Intent(context,ChattogramDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==2) {
                    context.startActivity(new Intent(context,SylhetDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==3) {
                    context.startActivity(new Intent(context,BorishalDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==4) {
                    context.startActivity(new Intent(context,MoymonsinghDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==5) {
                    context.startActivity(new Intent(context,RajshahiDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==6) {
                    context.startActivity(new Intent(context,KhulnaDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==7) {
                    context.startActivity(new Intent(context,RangporDistrict.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==8) {
                    context.startActivity(new Intent(context,ProyojoniyoDua.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==9) {
                    context.startActivity(new Intent(context,QuranHadith.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==10) {
                    context.startActivity(new Intent(context,ArticleMasyala.class));
                    Animatoo.animateSlideLeft(context);
                }
                if (position==11) {
                    context.startActivity(new Intent(context,RomjanerKhaddavash.class));
                    Animatoo.animateSlideLeft(context);
                }
            }
        });

        return view;
    }
}

这是我的main_activity.java

<pre><code>
    package com.sbappstore.romjanbanglacalendar;

import android.os.Bundle;

import android.widget.ListView;

public class MainActivity extends ToolbarAdapter {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        listView = (ListView) findViewById(R.id.listViewId);
        String[] districtNames = getResources().getStringArray(R.array.District_Names);

        CustomBaseAdapter adapter = new CustomBaseAdapter(this,districtNames);
        listView.setAdapter(adapter);

    }
}
<pre><code>

其他所有活动,例如波纹管...

<pre><code>
    package com.sbappstore.romjanbanglacalendar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.ListView;

public class DhakaDistrict extends ToolbarAdapter {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dhaka_district);

        getSupportActionBar().setTitle("ঢাকা বিভাগ");

        listView = (ListView) findViewById(R.id.listViewId);
        String[] dhakaDistrictNames = getResources().getStringArray(R.array.Dhaka_District_Names);

        CustomBaseAdapter adapter = new CustomBaseAdapter(this,dhakaDistrictNames);
        listView.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:0)

首先,使用RecyclerViewListView很老,RecyclerView的结构更好。

第二,请勿将职位用作ID。就您而言,将没有问题。但是位置可以更改,因此很危险。

下面的代码是我的建议,它不是完整的代码,而只是着重于如何使用ViewHolder模式。

public class MyHolder {
    // DO NOT use position as ID. I suggest you to use district name as ID.
    String districtName; 
    Button ListButton;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    MyHolder holder;

    if (convertView == null) {
        holder = new MyHolder();
        convertView = inflater.inflate(R.layout.custom_list, null);
        holder.ListButton = (Button) view.findViewById(R.id.ListButton);
        holder.ListButton.setText(Name_of_District[position]);

        holder.districtName = ""; // may be Name_of_District[position]

        // Set your onClick listener here. Use districtName is ID. 
        holder.ListButton.setOnClickListener();

        convertView.setTag(holder);
    } else {
        holder = (MyHolder)convertView.getTag()
    }

    // Write rest of code here

    return convertView;
}