2016-11-02 8 views
-2

私はAndroid開発の初心者です。 最近私はアプリを作って、自分のアクティビティでカスタムリストビューを使う方法を知らない。私はR.layout.support_simple_spinner_dropdown_item, to my R.layout.listView,私は今何をすべきかので、このアプリのクラッシュを交換するときイムは、このラインこのアクティビティでカスタムリストビューを使用するには

ListAdapter listAdapter = new ArrayAdapter<String>(CalculationHistoryListActivity.this,R.layout.support_simple_spinner_dropdown_item,totalCalculation); 

で立ち往生?

私はこの段階で立ち往生しています。 は、これは私がリストビュー

import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Date; 

public class CalculationHistoryListActivity extends AppCompatActivity { 
    // Remove the below line after defining your own ad unit ID. 
    //private static final String TOAST_TEXT = "Test ads are being shown. " 
     //  + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID."; 

    Save saveData; 
    ListView listView; 
    ArrayList<com.jungleofmagic.chabiri.ListView> arrayList; 
    BaseAdapter baseAdapter; 


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


     // Load an ad into the AdMob banner view. 
     AdView adView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder() 
       .setRequestAgent("android_studio:ad_template").build(); 
     adView.loadAd(adRequest); 

     // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID. 
     // Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show(); 

     listView = (ListView) findViewById(R.id.list); 
     saveData = new Save(CalculationHistoryListActivity.this); 

    } 



    public void run(View view){ 
     //cursor type database 
     Cursor database = saveData.display(); 
     if (database.getCount()==0){ 
      Toast.makeText(getBaseContext(),"No Data Found",Toast.LENGTH_LONG).show(); 
      return; 
     } 
     database.moveToFirst(); 
     //save string like a array 
     final String[] totalCalculation = new String[database.getCount()]; 
     int n = 0; 
     do { 
      totalCalculation[n] = database.getString(0).toString(); 
      n = n+1; 

     }while (database.moveToNext()); 


     ListAdapter listAdapter = new ArrayAdapter<String>(CalculationHistoryListActivity.this,R.layout.support_simple_spinner_dropdown_item,totalCalculation); 
     listView.setAdapter(listAdapter); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_calculation_history_list, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

を使用したい私のMainActivityであるあなたは男の私に

に役立つことを願っていますし、これは私自身のListView xmlファイルである

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

    <TextView 
     android:text="Total" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/total_money" 
     android:textSize="30dp" 
     android:fontFamily="serif" 
     android:paddingLeft="10dp" 
     android:layout_weight="1" /> 

    <TextView 
     android:text="00:00AM dd:MM:yyyy" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/date" 
     android:fontFamily="serif" 
     android:textSize="18dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

これは私自身のリストビューのJavaファイル

import java.util.Date; 

/** 
* Created by AIB Nihan on 11/1/2016. 
*/ 

public class ListView { 
    public ListView(Date date) { 
     this.date = date; 
    } 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    Date date; 
} 

これはクラッシュログ

11-02 16:44:04.006 3983-3983/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.jungleofmagic.chabiri, PID: 3983 
               java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 
                at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) 
                at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
                at android.widget.AbsListView.obtainView(AbsListView.java:2263) 
                at android.widget.ListView.makeAndAddView(ListView.java:1790) 
                at android.widget.ListView.fillDown(ListView.java:691) 
                at android.widget.ListView.fillFromTop(ListView.java:752) 
                at android.widget.ListView.layoutChildren(ListView.java:1630) 
                at android.widget.AbsListView.onLayout(AbsListView.java:2091) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                at android.widget.ScrollView.onLayout(ScrollView.java:1468) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                at android.view.View.layout(View.java:14871) 
                at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1994) 
                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751) 
                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1007) 
                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5677) 
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
                at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
                at android.view.Choreographer.doFrame(Choreographer.java:544) 
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
                at android.os.Handler.handleCallback(Handler.java:733) 
                at android.os.Handler.dispatchMessage(Handler.java:95) 
                at android.os.Looper.loop(Looper.java:136) 
                at android.app.ActivityThread.main(ActivityThread.java:5021) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                at dalvik.system.NativeStart.main(Native Method) 
                Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView 
                at and 

ので、男のは、どのように私は私の最初のアクティビティに自分のリストビューを使用するように教えてください

+0

どこにいらっしゃいましたか?何が問題なの?明確に教えてください –

+0

あなたのご意見ありがとうございます実際にこの行にはまった ListAdapter listAdapter = new ArrayAdapter (CalculationHistoryListActivity.this、R.layout.support_simple_spinner_dropdown_item、totalCalculation); R.layout.support_simple_spinner_dropdown_itemをR.layout.listViewに置き換えると、このアプリがクラッシュするので、今何をするのですか? –

+0

クラッシュログとは何ですか? –

答えて

1

パスこのようなArrayadapterコンストラクタのカスタムレイアウトファイルからTextview IDを取得し、それが動作しているかどうかを教えてください。

ListAdapter listAdapter = new ArrayAdapter<String>(CalculationHistoryListActivity.this,R.l‌​ayout.support_simple‌​_spinner_dropdown_it‌​em, R.id.your_textvie‌​w_id, totalCalculatio‌​n); 
+0

It's Work: )ありがとう –

関連する問題