我正在尝试创建一个从数据库获取数据的应用程序,并将它们列在Listview行中。我想让这些项目可以点击,所以我会通过点击它们来跳转另一个活动。但我无法点击Listview的项目。我将不胜感激任何帮助。提前谢谢。
我的适配器
public class OzelAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Urun> urunListesi;
Urun myUrun;
public OzelAdapter(Activity activity, List<Urun> urunList) {
mInflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
//gösterilecek listeyi de alalım
urunListesi = urunList;
}
@Override
public int getCount() {
return urunListesi.size();
}
@Override
public Urun getItem(int position) {
//şöyle de olabilir: public Object getItem(int position)
return urunListesi.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View satirView=null;
if(satirView==null) {
satirView = mInflater.inflate(R.layout.anasayfalist, null);
}
TextView textView =
(TextView) satirView.findViewById(R.id.stokName);
TextView textViewcount =
(TextView) satirView.findViewById(R.id.stokCount);
Urun asset = urunListesi.get(position);
textView.setText(asset.getName());
textViewcount.setText("Toplam Stok: "+asset.getQuantity());
return satirView;
}
}
我当前的list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
>
<ScrollView
android:layout_width="match_parent"
android:clickable="false"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<TextView
android:id="@+id/stokName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:focusable="false"
android:text="TextView"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="@+id/stokCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/stokName"
android:focusable="false"
android:text="TextView"
android:textSize="16dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
我的activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.stoktakip.StokSayfasi">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_collapseMode="pin" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</android.support.design.widget.CoordinatorLayout>
我的活动
public class StokSayfasi extends AppCompatActivity {
private FirebaseAuth auth;
private ViewPager viewPager;
private OzelAdapter adapter;
public ListView listemiz;
public Urun myUrun;
private List<String>urunler=new ArrayList<>();
private List<Integer>adetler=new ArrayList<>();
public List<Urun>urunList=new ArrayList<>();
public int count;
DatabaseReference rootRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stok_sayfasi);
listemiz=(ListView) findViewById(R.id.listView1);
registerForContextMenu(listemiz);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.app_name));
setSupportActionBar(toolbar);
prepareStockData();
adapter = new OzelAdapter(this,urunList);
listemiz.setAdapter(adapter);
listemiz.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
myUrun=urunList.get(i);
startActivity(new Intent(StokSayfasi.this, DetayliListe.class));
finish();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.choose_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.urungir:
new Intent(StokSayfasi.this, DetayliListe.class);
return true;
case R.id.uruncikar:
// edit stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.urun_girdisi:
startActivity(new Intent(StokSayfasi.this,UrunGirdisi.class));
return true;
case R.id.urun_ciktisi:
startActivity(new Intent(StokSayfasi.this,UrunCiktisi.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void prepareStockData(){
rootRef= FirebaseDatabase.getInstance().getReference("Ürünler");
rootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
urunList.clear();
for (DataSnapshot childSnapShot : dataSnapshot.getChildren()){
count=0;
urunler.add((String)childSnapShot.child("name").getValue());
count=count+((Long)childSnapShot.child("quantity").getValue()).intValue();
int childs=(int)childSnapShot.getChildrenCount();
int exit=0;
for (DataSnapshot childishSnapShot : childSnapShot.getChildren()){
try {
count = count + ((Long) childishSnapShot.child("quantity").getValue()).intValue();
}
catch (NullPointerException e){
}
}
adetler.add(count);
}
for(int i=0 ; i<urunler.size();i++){
Urun u1=new Urun(urunler.get(i),adetler.get(i));
urunList.add(u1);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
编辑:
我将onClick行添加到我的list.xml中,并编写了一个方法,当我点击listview项目时向我显示一条消息。问题是我可以在我的屏幕上看到toast通知,但listview clicklistener从未在方法中执行过。对不起,我是Android的新手,我想我可能在list.xml中做错了。
点击方法:
public void myMethod(View view){
Toast.makeText(this, "Try this.", Toast.LENGTH_SHORT).show();
listemiz.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
myUrun=urunList.get(i);
startActivity(new Intent(StokSayfasi.this,DetayliListe.class));
}
});
}
答案 0 :(得分:2)
您可以直接在getView()
中添加clickListener @Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View satirView=null;
if(satirView==null) {
satirView = mInflater.inflate(R.layout.anasayfalist, null);
}
TextView textView =
(TextView) satirView.findViewById(R.id.stokName);
TextView textViewcount =
(TextView) satirView.findViewById(R.id.stokCount);
Urun asset = urunListesi.get(position);
textView.setText(asset.getName());
textViewcount.setText("Toplam Stok: "+asset.getQuantity());
satirView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return satirView;
}
并将list.xml的高度更改为wrap_content
答案 1 :(得分:0)
您可以在适配器的getView方法中为LinearLayout设置onclick侦听器。或者在onitemClickListener上调试代码以找到问题
答案 2 :(得分:0)
您还可以使用listview setOnItemnClickListener方法进行项目单击
检查以下代码:
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
在xml中使用此代码并检查。希望这会奏效。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></ListView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_collapseMode="pin" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>